MqttMsgUnsubscribe.cs 8.4 KB

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