connack.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package packets
  2. import (
  3. "bytes"
  4. "fmt"
  5. "github.com/pborman/uuid"
  6. "io"
  7. )
  8. //ConnackPacket is an internal representation of the fields of the
  9. //Connack MQTT packet
  10. type ConnackPacket struct {
  11. FixedHeader
  12. TopicNameCompression byte
  13. ReturnCode byte
  14. uuid uuid.UUID
  15. }
  16. func (ca *ConnackPacket) String() string {
  17. str := fmt.Sprintf("%s\n", ca.FixedHeader)
  18. str += fmt.Sprintf("returncode: %d", ca.ReturnCode)
  19. return str
  20. }
  21. func (ca *ConnackPacket) Write(w io.Writer) error {
  22. var body bytes.Buffer
  23. var err error
  24. body.WriteByte(ca.TopicNameCompression)
  25. body.WriteByte(ca.ReturnCode)
  26. ca.FixedHeader.RemainingLength = 2
  27. packet := ca.FixedHeader.pack()
  28. packet.Write(body.Bytes())
  29. _, err = packet.WriteTo(w)
  30. return err
  31. }
  32. //Unpack decodes the details of a ControlPacket after the fixed
  33. //header has been read
  34. func (ca *ConnackPacket) Unpack(b io.Reader) {
  35. ca.TopicNameCompression = decodeByte(b)
  36. ca.ReturnCode = decodeByte(b)
  37. }
  38. //Details returns a Details struct containing the Qos and
  39. //MessageID of this ControlPacket
  40. func (ca *ConnackPacket) Details() Details {
  41. return Details{Qos: 0, MessageID: 0}
  42. }
  43. //UUID returns the unique ID assigned to the ControlPacket when
  44. //it was originally received. Note: this is not related to the
  45. //MessageID field for MQTT packets
  46. func (ca *ConnackPacket) UUID() uuid.UUID {
  47. return ca.uuid
  48. }