MqttMsgUnsubscribe.cs 7.9 KB

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