MqttMsgPubcomp.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. Copyright (c) 2013, 2014 Paolo Patierno
  3. All rights reserved. This program and the accompanying materials
  4. are made available under the terms of the Eclipse Public License v1.0
  5. and Eclipse Distribution License v1.0 which accompany this distribution.
  6. The Eclipse Public License is available at
  7. http://www.eclipse.org/legal/epl-v10.html
  8. and the Eclipse Distribution License is available at
  9. http://www.eclipse.org/org/documents/edl-v10.php.
  10. Contributors:
  11. Paolo Patierno - initial API and implementation and/or initial documentation
  12. */
  13. namespace uPLibrary.Networking.M2Mqtt.Messages
  14. {
  15. /// <summary>
  16. /// Class for PUBCOMP message from broker to client
  17. /// </summary>
  18. public class MqttMsgPubcomp : MqttMsgBase
  19. {
  20. #region Properties...
  21. /// <summary>
  22. /// Message identifier for the acknowledged publish message
  23. /// </summary>
  24. public ushort MessageId
  25. {
  26. get { return this.messageId; }
  27. set { this.messageId = value; }
  28. }
  29. #endregion
  30. // message identifier
  31. private ushort messageId;
  32. /// <summary>
  33. /// Constructor
  34. /// </summary>
  35. public MqttMsgPubcomp()
  36. {
  37. this.type = MQTT_MSG_PUBCOMP_TYPE;
  38. }
  39. public override byte[] GetBytes()
  40. {
  41. int fixedHeaderSize = 0;
  42. int varHeaderSize = 0;
  43. int payloadSize = 0;
  44. int remainingLength = 0;
  45. byte[] buffer;
  46. int index = 0;
  47. // message identifier
  48. varHeaderSize += MESSAGE_ID_SIZE;
  49. remainingLength += (varHeaderSize + payloadSize);
  50. // first byte of fixed header
  51. fixedHeaderSize = 1;
  52. int temp = remainingLength;
  53. // increase fixed header size based on remaining length
  54. // (each remaining length byte can encode until 128)
  55. do
  56. {
  57. fixedHeaderSize++;
  58. temp = temp / 128;
  59. } while (temp > 0);
  60. // allocate buffer for message
  61. buffer = new byte[fixedHeaderSize + varHeaderSize + payloadSize];
  62. // first fixed header byte
  63. buffer[index++] = (MQTT_MSG_PUBCOMP_TYPE << MSG_TYPE_OFFSET);
  64. // encode remaining length
  65. index = this.encodeRemainingLength(remainingLength, buffer, index);
  66. // get message identifier
  67. buffer[index++] = (byte)((this.messageId >> 8) & 0x00FF); // MSB
  68. buffer[index++] = (byte)(this.messageId & 0x00FF); // LSB
  69. return buffer;
  70. }
  71. /// <summary>
  72. /// Parse bytes for a PUBCOMP message
  73. /// </summary>
  74. /// <param name="fixedHeaderFirstByte">First fixed header byte</param>
  75. /// <param name="channel">Channel connected to the broker</param>
  76. /// <returns>PUBCOMP message instance</returns>
  77. public static MqttMsgPubcomp Parse(byte fixedHeaderFirstByte, IMqttNetworkChannel channel)
  78. {
  79. byte[] buffer;
  80. int index = 0;
  81. MqttMsgPubcomp msg = new MqttMsgPubcomp();
  82. // get remaining length and allocate buffer
  83. int remainingLength = MqttMsgBase.decodeRemainingLength(channel);
  84. buffer = new byte[remainingLength];
  85. // read bytes from socket...
  86. channel.Receive(buffer);
  87. // message id
  88. msg.messageId = (ushort)((buffer[index++] << 8) & 0xFF00);
  89. msg.messageId |= (buffer[index++]);
  90. return msg;
  91. }
  92. public override string ToString()
  93. {
  94. #if TRACE
  95. return this.GetTraceString(
  96. "PUBCOMP",
  97. new object[] { "messageId" },
  98. new object[] { this.messageId });
  99. #else
  100. return base.ToString();
  101. #endif
  102. }
  103. }
  104. }