MqttMsgUnsubscribe.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. // if NOT .Net Micro Framework
  15. #if (!MF_FRAMEWORK_VERSION_V4_2 && !MF_FRAMEWORK_VERSION_V4_3)
  16. using System.Collections.Generic;
  17. #endif
  18. using System.Collections;
  19. using System.Text;
  20. using uPLibrary.Networking.M2Mqtt.Exceptions;
  21. namespace uPLibrary.Networking.M2Mqtt.Messages
  22. {
  23. /// <summary>
  24. /// Class for UNSUBSCRIBE message from client to broker
  25. /// </summary>
  26. public class MqttMsgUnsubscribe : MqttMsgBase
  27. {
  28. #region Properties...
  29. /// <summary>
  30. /// List of topics to unsubscribe
  31. /// </summary>
  32. public string[] Topics
  33. {
  34. get { return this.topics; }
  35. set { this.topics = value; }
  36. }
  37. #endregion
  38. // topics to unsubscribe
  39. string[] topics;
  40. /// <summary>
  41. /// Constructor
  42. /// </summary>
  43. public MqttMsgUnsubscribe()
  44. {
  45. this.type = MQTT_MSG_UNSUBSCRIBE_TYPE;
  46. }
  47. /// <summary>
  48. /// Constructor
  49. /// </summary>
  50. /// <param name="topics">List of topics to unsubscribe</param>
  51. public MqttMsgUnsubscribe(string[] topics)
  52. {
  53. this.type = MQTT_MSG_UNSUBSCRIBE_TYPE;
  54. this.topics = topics;
  55. // UNSUBSCRIBE message uses QoS Level 1 (not "officially" in 3.1.1)
  56. this.qosLevel = QOS_LEVEL_AT_LEAST_ONCE;
  57. }
  58. /// <summary>
  59. /// Parse bytes for a UNSUBSCRIBE message
  60. /// </summary>
  61. /// <param name="fixedHeaderFirstByte">First fixed header byte</param>
  62. /// <param name="protocolVersion">Protocol Version</param>
  63. /// <param name="channel">Channel connected to the broker</param>
  64. /// <returns>UNSUBSCRIBE message instance</returns>
  65. public static MqttMsgUnsubscribe Parse(byte fixedHeaderFirstByte, byte protocolVersion, IMqttNetworkChannel channel)
  66. {
  67. byte[] buffer;
  68. int index = 0;
  69. byte[] topicUtf8;
  70. int topicUtf8Length;
  71. MqttMsgUnsubscribe msg = new MqttMsgUnsubscribe();
  72. if (protocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1_1)
  73. {
  74. // [v3.1.1] check flag bits
  75. if ((fixedHeaderFirstByte & MSG_FLAG_BITS_MASK) != MQTT_MSG_UNSUBSCRIBE_FLAG_BITS)
  76. throw new MqttClientException(MqttClientErrorCode.InvalidFlagBits);
  77. }
  78. // get remaining length and allocate buffer
  79. int remainingLength = MqttMsgBase.decodeRemainingLength(channel);
  80. buffer = new byte[remainingLength];
  81. // read bytes from socket...
  82. int received = channel.Receive(buffer);
  83. if (protocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1)
  84. {
  85. // only 3.1.0
  86. // read QoS level from fixed header
  87. msg.qosLevel = (byte)((fixedHeaderFirstByte & QOS_LEVEL_MASK) >> QOS_LEVEL_OFFSET);
  88. // read DUP flag from fixed header
  89. msg.dupFlag = (((fixedHeaderFirstByte & DUP_FLAG_MASK) >> DUP_FLAG_OFFSET) == 0x01);
  90. // retain flag not used
  91. msg.retain = false;
  92. }
  93. // message id
  94. msg.messageId = (ushort)((buffer[index++] << 8) & 0xFF00);
  95. msg.messageId |= (buffer[index++]);
  96. // payload contains topics
  97. // NOTE : before, I don't know how many topics will be in the payload (so use List)
  98. // if .Net Micro Framework
  99. #if (MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3)
  100. IList tmpTopics = new ArrayList();
  101. // else other frameworks (.Net, .Net Compact, Mono, Windows Phone)
  102. #else
  103. IList<String> tmpTopics = new List<String>();
  104. #endif
  105. do
  106. {
  107. // topic name
  108. topicUtf8Length = ((buffer[index++] << 8) & 0xFF00);
  109. topicUtf8Length |= buffer[index++];
  110. topicUtf8 = new byte[topicUtf8Length];
  111. Array.Copy(buffer, index, topicUtf8, 0, topicUtf8Length);
  112. index += topicUtf8Length;
  113. tmpTopics.Add(new String(Encoding.UTF8.GetChars(topicUtf8)));
  114. } while (index < remainingLength);
  115. // copy from list to array
  116. msg.topics = new string[tmpTopics.Count];
  117. for (int i = 0; i < tmpTopics.Count; i++)
  118. {
  119. msg.topics[i] = (string)tmpTopics[i];
  120. }
  121. return msg;
  122. }
  123. public override byte[] GetBytes(byte protocolVersion)
  124. {
  125. int fixedHeaderSize = 0;
  126. int varHeaderSize = 0;
  127. int payloadSize = 0;
  128. int remainingLength = 0;
  129. byte[] buffer;
  130. int index = 0;
  131. // topics list empty
  132. if ((this.topics == null) || (this.topics.Length == 0))
  133. throw new MqttClientException(MqttClientErrorCode.TopicsEmpty);
  134. // message identifier
  135. varHeaderSize += MESSAGE_ID_SIZE;
  136. int topicIdx = 0;
  137. byte[][] topicsUtf8 = new byte[this.topics.Length][];
  138. for (topicIdx = 0; topicIdx < this.topics.Length; topicIdx++)
  139. {
  140. // check topic length
  141. if ((this.topics[topicIdx].Length < MIN_TOPIC_LENGTH) || (this.topics[topicIdx].Length > MAX_TOPIC_LENGTH))
  142. throw new MqttClientException(MqttClientErrorCode.TopicLength);
  143. topicsUtf8[topicIdx] = Encoding.UTF8.GetBytes(this.topics[topicIdx]);
  144. payloadSize += 2; // topic size (MSB, LSB)
  145. payloadSize += topicsUtf8[topicIdx].Length;
  146. }
  147. remainingLength += (varHeaderSize + payloadSize);
  148. // first byte of fixed header
  149. fixedHeaderSize = 1;
  150. int temp = remainingLength;
  151. // increase fixed header size based on remaining length
  152. // (each remaining length byte can encode until 128)
  153. do
  154. {
  155. fixedHeaderSize++;
  156. temp = temp / 128;
  157. } while (temp > 0);
  158. // allocate buffer for message
  159. buffer = new byte[fixedHeaderSize + varHeaderSize + payloadSize];
  160. // first fixed header byte
  161. if (protocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1_1)
  162. buffer[index++] = (MQTT_MSG_UNSUBSCRIBE_TYPE << MSG_TYPE_OFFSET) | MQTT_MSG_UNSUBSCRIBE_FLAG_BITS; // [v.3.1.1]
  163. else
  164. {
  165. buffer[index] = (byte)((MQTT_MSG_UNSUBSCRIBE_TYPE << MSG_TYPE_OFFSET) |
  166. (this.qosLevel << QOS_LEVEL_OFFSET));
  167. buffer[index] |= this.dupFlag ? (byte)(1 << DUP_FLAG_OFFSET) : (byte)0x00;
  168. index++;
  169. }
  170. // encode remaining length
  171. index = this.encodeRemainingLength(remainingLength, buffer, index);
  172. // check message identifier assigned
  173. if (this.messageId == 0)
  174. throw new MqttClientException(MqttClientErrorCode.WrongMessageId);
  175. buffer[index++] = (byte)((messageId >> 8) & 0x00FF); // MSB
  176. buffer[index++] = (byte)(messageId & 0x00FF); // LSB
  177. topicIdx = 0;
  178. for (topicIdx = 0; topicIdx < this.topics.Length; topicIdx++)
  179. {
  180. // topic name
  181. buffer[index++] = (byte)((topicsUtf8[topicIdx].Length >> 8) & 0x00FF); // MSB
  182. buffer[index++] = (byte)(topicsUtf8[topicIdx].Length & 0x00FF); // LSB
  183. Array.Copy(topicsUtf8[topicIdx], 0, buffer, index, topicsUtf8[topicIdx].Length);
  184. index += topicsUtf8[topicIdx].Length;
  185. }
  186. return buffer;
  187. }
  188. public override string ToString()
  189. {
  190. #if TRACE
  191. return this.GetTraceString(
  192. "UNSUBSCRIBE",
  193. new object[] { "messageId", "topics" },
  194. new object[] { this.messageId, this.topics });
  195. #else
  196. return base.ToString();
  197. #endif
  198. }
  199. }
  200. }