MqttMsgPubrel.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 PUBREL message from client top broker
  17. /// </summary>
  18. public class MqttMsgPubrel : 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 MqttMsgPubrel()
  36. {
  37. this.type = MQTT_MSG_PUBREL_TYPE;
  38. // PUBREL message use QoS Level 1
  39. this.qosLevel = QOS_LEVEL_AT_LEAST_ONCE;
  40. }
  41. public override byte[] GetBytes()
  42. {
  43. int fixedHeaderSize = 0;
  44. int varHeaderSize = 0;
  45. int payloadSize = 0;
  46. int remainingLength = 0;
  47. byte[] buffer;
  48. int index = 0;
  49. // message identifier
  50. varHeaderSize += MESSAGE_ID_SIZE;
  51. remainingLength += (varHeaderSize + payloadSize);
  52. // first byte of fixed header
  53. fixedHeaderSize = 1;
  54. int temp = remainingLength;
  55. // increase fixed header size based on remaining length
  56. // (each remaining length byte can encode until 128)
  57. do
  58. {
  59. fixedHeaderSize++;
  60. temp = temp / 128;
  61. } while (temp > 0);
  62. // allocate buffer for message
  63. buffer = new byte[fixedHeaderSize + varHeaderSize + payloadSize];
  64. // first fixed header byte
  65. buffer[index] = (byte)((MQTT_MSG_PUBREL_TYPE << MSG_TYPE_OFFSET) |
  66. (this.qosLevel << QOS_LEVEL_OFFSET));
  67. buffer[index] |= this.dupFlag ? (byte)(1 << DUP_FLAG_OFFSET) : (byte)0x00;
  68. index++;
  69. // encode remaining length
  70. index = this.encodeRemainingLength(remainingLength, buffer, index);
  71. // get next message identifier
  72. buffer[index++] = (byte)((this.messageId >> 8) & 0x00FF); // MSB
  73. buffer[index++] = (byte)(this.messageId & 0x00FF); // LSB
  74. return buffer;
  75. }
  76. /// <summary>
  77. /// Parse bytes for a PUBREL message
  78. /// </summary>
  79. /// <param name="fixedHeaderFirstByte">First fixed header byte</param>
  80. /// <param name="channel">Channel connected to the broker</param>
  81. /// <returns>PUBREL message instance</returns>
  82. public static MqttMsgPubrel Parse(byte fixedHeaderFirstByte, IMqttNetworkChannel channel)
  83. {
  84. byte[] buffer;
  85. int index = 0;
  86. MqttMsgPubrel msg = new MqttMsgPubrel();
  87. // get remaining length and allocate buffer
  88. int remainingLength = MqttMsgBase.decodeRemainingLength(channel);
  89. buffer = new byte[remainingLength];
  90. // read bytes from socket...
  91. channel.Receive(buffer);
  92. // read QoS level from fixed header (would be QoS Level 1)
  93. msg.qosLevel = (byte)((fixedHeaderFirstByte & QOS_LEVEL_MASK) >> QOS_LEVEL_OFFSET);
  94. // read DUP flag from fixed header
  95. msg.dupFlag = (((fixedHeaderFirstByte & DUP_FLAG_MASK) >> DUP_FLAG_OFFSET) == 0x01);
  96. // message id
  97. msg.messageId = (ushort)((buffer[index++] << 8) & 0xFF00);
  98. msg.messageId |= (buffer[index++]);
  99. return msg;
  100. }
  101. public override string ToString()
  102. {
  103. #if TRACE
  104. return this.GetTraceString(
  105. "PUBREL",
  106. new object[] { "messageId" },
  107. new object[] { this.messageId });
  108. #else
  109. return base.ToString();
  110. #endif
  111. }
  112. }
  113. }