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