MqttMsgPubrel.cs 4.9 KB

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