MqttMsgPubrel.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. using M2Mqtt.Exceptions;
  14. using M2Mqtt.Messages;
  15. namespace M2Mqtt.Messages
  16. {
  17. /// <summary>
  18. /// Class for PUBREL message from client top broker
  19. /// </summary>
  20. public class MqttMsgPubrel : MqttMsgBase
  21. {
  22. /// <summary>
  23. /// Constructor
  24. /// </summary>
  25. public MqttMsgPubrel()
  26. {
  27. type = MQTT_MSG_PUBREL_TYPE;
  28. // PUBREL message use QoS Level 1 (not "officially" in 3.1.1)
  29. qosLevel = QOS_LEVEL_AT_LEAST_ONCE;
  30. }
  31. public override byte[] GetBytes(byte protocolVersion)
  32. {
  33. int fixedHeaderSize = 0;
  34. int varHeaderSize = 0;
  35. int payloadSize = 0;
  36. int remainingLength = 0;
  37. byte[] buffer;
  38. int index = 0;
  39. // message identifier
  40. varHeaderSize += MESSAGE_ID_SIZE;
  41. remainingLength += (varHeaderSize + payloadSize);
  42. // first byte of fixed header
  43. fixedHeaderSize = 1;
  44. int temp = remainingLength;
  45. // increase fixed header size based on remaining length
  46. // (each remaining length byte can encode until 128)
  47. do
  48. {
  49. fixedHeaderSize++;
  50. temp = temp / 128;
  51. } while (temp > 0);
  52. // allocate buffer for message
  53. buffer = new byte[fixedHeaderSize + varHeaderSize + payloadSize];
  54. // first fixed header byte
  55. if (protocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1_1)
  56. buffer[index++] = (MQTT_MSG_PUBREL_TYPE << MSG_TYPE_OFFSET) | MQTT_MSG_PUBREL_FLAG_BITS; // [v.3.1.1]
  57. else
  58. {
  59. buffer[index] = (byte)((MQTT_MSG_PUBREL_TYPE << MSG_TYPE_OFFSET) |
  60. (qosLevel << QOS_LEVEL_OFFSET));
  61. buffer[index] |= dupFlag ? (byte)(1 << DUP_FLAG_OFFSET) : (byte)0x00;
  62. index++;
  63. }
  64. // encode remaining length
  65. index = encodeRemainingLength(remainingLength, buffer, index);
  66. // get next message identifier
  67. buffer[index++] = (byte)((messageId >> 8) & 0x00FF); // MSB
  68. buffer[index++] = (byte)(messageId & 0x00FF); // LSB
  69. return buffer;
  70. }
  71. /// <summary>
  72. /// Parse bytes for a PUBREL message
  73. /// </summary>
  74. /// <param name="fixedHeaderFirstByte">First fixed header byte</param>
  75. /// <param name="protocolVersion">Protocol Version</param>
  76. /// <param name="channel">Channel connected to the broker</param>
  77. /// <returns>PUBREL message instance</returns>
  78. public static MqttMsgPubrel Parse(byte fixedHeaderFirstByte, byte protocolVersion, IMqttNetworkChannel channel)
  79. {
  80. byte[] buffer;
  81. int index = 0;
  82. MqttMsgPubrel msg = new MqttMsgPubrel();
  83. if (protocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1_1)
  84. {
  85. // [v3.1.1] check flag bits
  86. if ((fixedHeaderFirstByte & MSG_FLAG_BITS_MASK) != MQTT_MSG_PUBREL_FLAG_BITS)
  87. throw new MqttClientException(MqttClientErrorCode.InvalidFlagBits);
  88. }
  89. // get remaining length and allocate buffer
  90. int remainingLength = MqttMsgBase.decodeRemainingLength(channel);
  91. buffer = new byte[remainingLength];
  92. // read bytes from socket...
  93. channel.Receive(buffer);
  94. if (protocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1)
  95. {
  96. // only 3.1.0
  97. // read QoS level from fixed header (would be QoS Level 1)
  98. msg.qosLevel = (byte)((fixedHeaderFirstByte & QOS_LEVEL_MASK) >> QOS_LEVEL_OFFSET);
  99. // read DUP flag from fixed header
  100. msg.dupFlag = (((fixedHeaderFirstByte & DUP_FLAG_MASK) >> DUP_FLAG_OFFSET) == 0x01);
  101. }
  102. // message id
  103. msg.messageId = (ushort)((buffer[index++] << 8) & 0xFF00);
  104. msg.messageId |= (buffer[index++]);
  105. return msg;
  106. }
  107. public override string ToString()
  108. {
  109. #if TRACE
  110. return GetTraceString(
  111. "PUBREL",
  112. new object[] { "messageId" },
  113. new object[] { messageId });
  114. #else
  115. return base.ToString();
  116. #endif
  117. }
  118. }
  119. }