unsuback.go 940 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package packets
  2. import (
  3. "fmt"
  4. "io"
  5. )
  6. // UnsubackPacket is an internal representation of the fields of the
  7. // Unsuback MQTT packet
  8. type UnsubackPacket struct {
  9. FixedHeader
  10. MessageID uint16
  11. }
  12. func (ua *UnsubackPacket) String() string {
  13. return fmt.Sprintf("%s MessageID: %d", ua.FixedHeader, ua.MessageID)
  14. }
  15. func (ua *UnsubackPacket) Write(w io.Writer) error {
  16. var err error
  17. ua.FixedHeader.RemainingLength = 2
  18. packet := ua.FixedHeader.pack()
  19. packet.Write(encodeUint16(ua.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 (ua *UnsubackPacket) Unpack(b io.Reader) error {
  26. var err error
  27. ua.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 (ua *UnsubackPacket) Details() Details {
  33. return Details{Qos: 0, MessageID: ua.MessageID}
  34. }