MqttMsgSuback.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 System;
  14. using M2Mqtt.Exceptions;
  15. using M2Mqtt.Messages;
  16. namespace M2Mqtt.Messages
  17. {
  18. /// <summary>
  19. /// Class for SUBACK message from broker to client
  20. /// </summary>
  21. public class MqttMsgSuback : MqttMsgBase
  22. {
  23. #region Properties...
  24. /// <summary>
  25. /// List of granted QOS Levels
  26. /// </summary>
  27. public byte[] GrantedQoSLevels
  28. {
  29. get { return grantedQosLevels; }
  30. set { grantedQosLevels = value; }
  31. }
  32. #endregion
  33. // granted QOS levels
  34. byte[] grantedQosLevels;
  35. /// <summary>
  36. /// Constructor
  37. /// </summary>
  38. public MqttMsgSuback()
  39. {
  40. type = MQTT_MSG_SUBACK_TYPE;
  41. }
  42. /// <summary>
  43. /// Parse bytes for a SUBACK message
  44. /// </summary>
  45. /// <param name="fixedHeaderFirstByte">First fixed header byte</param>
  46. /// <param name="protocolVersion">Protocol Version</param>
  47. /// <param name="channel">Channel connected to the broker</param>
  48. /// <returns>SUBACK message instance</returns>
  49. public static MqttMsgSuback Parse(byte fixedHeaderFirstByte, byte protocolVersion, IMqttNetworkChannel channel)
  50. {
  51. byte[] buffer;
  52. int index = 0;
  53. MqttMsgSuback msg = new MqttMsgSuback();
  54. if (protocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1_1)
  55. {
  56. // [v3.1.1] check flag bits
  57. if ((fixedHeaderFirstByte & MSG_FLAG_BITS_MASK) != MQTT_MSG_SUBACK_FLAG_BITS)
  58. throw new MqttClientException(MqttClientErrorCode.InvalidFlagBits);
  59. }
  60. // get remaining length and allocate buffer
  61. int remainingLength = MqttMsgBase.decodeRemainingLength(channel);
  62. buffer = new byte[remainingLength];
  63. // read bytes from socket...
  64. channel.Receive(buffer);
  65. // message id
  66. msg.messageId = (ushort)((buffer[index++] << 8) & 0xFF00);
  67. msg.messageId |= (buffer[index++]);
  68. // payload contains QoS levels granted
  69. msg.grantedQosLevels = new byte[remainingLength - MESSAGE_ID_SIZE];
  70. int qosIdx = 0;
  71. do
  72. {
  73. msg.grantedQosLevels[qosIdx++] = buffer[index++];
  74. } while (index < remainingLength);
  75. return msg;
  76. }
  77. public override byte[] GetBytes(byte protocolVersion)
  78. {
  79. int fixedHeaderSize = 0;
  80. int varHeaderSize = 0;
  81. int payloadSize = 0;
  82. int remainingLength = 0;
  83. byte[] buffer;
  84. int index = 0;
  85. // message identifier
  86. varHeaderSize += MESSAGE_ID_SIZE;
  87. int grantedQosIdx = 0;
  88. for (grantedQosIdx = 0; grantedQosIdx < grantedQosLevels.Length; grantedQosIdx++)
  89. {
  90. payloadSize++;
  91. }
  92. remainingLength += (varHeaderSize + payloadSize);
  93. // first byte of fixed header
  94. fixedHeaderSize = 1;
  95. int temp = remainingLength;
  96. // increase fixed header size based on remaining length
  97. // (each remaining length byte can encode until 128)
  98. do
  99. {
  100. fixedHeaderSize++;
  101. temp = temp / 128;
  102. } while (temp > 0);
  103. // allocate buffer for message
  104. buffer = new byte[fixedHeaderSize + varHeaderSize + payloadSize];
  105. // first fixed header byte
  106. if (protocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1_1)
  107. buffer[index++] = (MQTT_MSG_SUBACK_TYPE << MSG_TYPE_OFFSET) | MQTT_MSG_SUBACK_FLAG_BITS; // [v.3.1.1]
  108. else
  109. buffer[index++] = (byte)(MQTT_MSG_SUBACK_TYPE << MSG_TYPE_OFFSET);
  110. // encode remaining length
  111. index = encodeRemainingLength(remainingLength, buffer, index);
  112. // message id
  113. buffer[index++] = (byte)((messageId >> 8) & 0x00FF); // MSB
  114. buffer[index++] = (byte)(messageId & 0x00FF); // LSB
  115. // payload contains QoS levels granted
  116. for (grantedQosIdx = 0; grantedQosIdx < grantedQosLevels.Length; grantedQosIdx++)
  117. {
  118. buffer[index++] = grantedQosLevels[grantedQosIdx];
  119. }
  120. return buffer;
  121. }
  122. public override string ToString()
  123. {
  124. #if TRACE
  125. return GetTraceString(
  126. "SUBACK",
  127. new object[] { "messageId", "grantedQosLevels" },
  128. new object[] { messageId, grantedQosLevels });
  129. #else
  130. return base.ToString();
  131. #endif
  132. }
  133. }
  134. }