puback.go 931 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package packets
  2. import (
  3. "fmt"
  4. "io"
  5. )
  6. // PubackPacket is an internal representation of the fields of the
  7. // Puback MQTT packet
  8. type PubackPacket struct {
  9. FixedHeader
  10. MessageID uint16
  11. }
  12. func (pa *PubackPacket) String() string {
  13. return fmt.Sprintf("%s MessageID: %d", pa.FixedHeader, pa.MessageID)
  14. }
  15. func (pa *PubackPacket) Write(w io.Writer) error {
  16. var err error
  17. pa.FixedHeader.RemainingLength = 2
  18. packet := pa.FixedHeader.pack()
  19. packet.Write(encodeUint16(pa.MessageID))
  20. _, err = packet.WriteTo(w)
  21. return err
  22. }
  23. // Unpack decodes the details of a ControlPacket after the fixed
  24. // header has been read
  25. func (pa *PubackPacket) Unpack(b io.Reader) error {
  26. var err error
  27. pa.MessageID, err = decodeUint16(b)
  28. return err
  29. }
  30. // Details returns a Details struct containing the Qos and
  31. // MessageID of this ControlPacket
  32. func (pa *PubackPacket) Details() Details {
  33. return Details{Qos: pa.Qos, MessageID: pa.MessageID}
  34. }