pubrec.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. * Copyright (c) 2021 IBM Corp and others.
  3. *
  4. * All rights reserved. This program and the accompanying materials
  5. * are made available under the terms of the Eclipse Public License v2.0
  6. * and Eclipse Distribution License v1.0 which accompany this distribution.
  7. *
  8. * The Eclipse Public License is available at
  9. * https://www.eclipse.org/legal/epl-2.0/
  10. * and the Eclipse Distribution License is available at
  11. * http://www.eclipse.org/org/documents/edl-v10.php.
  12. *
  13. * Contributors:
  14. * Allan Stockdill-Mander
  15. */
  16. package packets
  17. import (
  18. "fmt"
  19. "io"
  20. )
  21. // PubrecPacket is an internal representation of the fields of the
  22. // Pubrec MQTT packet
  23. type PubrecPacket struct {
  24. FixedHeader
  25. MessageID uint16
  26. }
  27. func (pr *PubrecPacket) String() string {
  28. return fmt.Sprintf("%s MessageID: %d", pr.FixedHeader, pr.MessageID)
  29. }
  30. func (pr *PubrecPacket) Write(w io.Writer) error {
  31. var err error
  32. pr.FixedHeader.RemainingLength = 2
  33. packet := pr.FixedHeader.pack()
  34. packet.Write(encodeUint16(pr.MessageID))
  35. _, err = packet.WriteTo(w)
  36. return err
  37. }
  38. // Unpack decodes the details of a ControlPacket after the fixed
  39. // header has been read
  40. func (pr *PubrecPacket) Unpack(b io.Reader) error {
  41. var err error
  42. pr.MessageID, err = decodeUint16(b)
  43. return err
  44. }
  45. // Details returns a Details struct containing the Qos and
  46. // MessageID of this ControlPacket
  47. func (pr *PubrecPacket) Details() Details {
  48. return Details{Qos: pr.Qos, MessageID: pr.MessageID}
  49. }