MqttMsgConnack.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 M2Mqtt.Exceptions;
  14. namespace M2Mqtt.Messages
  15. {
  16. /// <summary>
  17. /// Class for CONNACK message from broker to client
  18. /// </summary>
  19. public class MqttMsgConnack : MqttMsgBase
  20. {
  21. #region Constants...
  22. // return codes for CONNACK message
  23. public const byte CONN_ACCEPTED = 0x00;
  24. public const byte CONN_REFUSED_PROT_VERS = 0x01;
  25. public const byte CONN_REFUSED_IDENT_REJECTED = 0x02;
  26. public const byte CONN_REFUSED_SERVER_UNAVAILABLE = 0x03;
  27. public const byte CONN_REFUSED_USERNAME_PASSWORD = 0x04;
  28. public const byte CONN_REFUSED_NOT_AUTHORIZED = 0x05;
  29. private const byte TOPIC_NAME_COMP_RESP_BYTE_OFFSET = 0;
  30. private const byte TOPIC_NAME_COMP_RESP_BYTE_SIZE = 1;
  31. // [v3.1.1] connect acknowledge flags replace "old" topic name compression respone (not used in 3.1)
  32. private const byte CONN_ACK_FLAGS_BYTE_OFFSET = 0;
  33. private const byte CONN_ACK_FLAGS_BYTE_SIZE = 1;
  34. // [v3.1.1] session present flag
  35. private const byte SESSION_PRESENT_FLAG_MASK = 0x01;
  36. private const byte SESSION_PRESENT_FLAG_OFFSET = 0x00;
  37. private const byte SESSION_PRESENT_FLAG_SIZE = 0x01;
  38. private const byte CONN_RETURN_CODE_BYTE_OFFSET = 1;
  39. private const byte CONN_RETURN_CODE_BYTE_SIZE = 1;
  40. #endregion
  41. #region Properties...
  42. // [v3.1.1] session present flag
  43. /// <summary>
  44. /// Session present flag
  45. /// </summary>
  46. public bool SessionPresent
  47. {
  48. get { return sessionPresent; }
  49. set { sessionPresent = value; }
  50. }
  51. /// <summary>
  52. /// Return Code
  53. /// </summary>
  54. public byte ReturnCode
  55. {
  56. get { return returnCode; }
  57. set { returnCode = value; }
  58. }
  59. #endregion
  60. // [v3.1.1] session present flag
  61. private bool sessionPresent;
  62. // return code for CONNACK message
  63. private byte returnCode;
  64. /// <summary>
  65. /// Constructor
  66. /// </summary>
  67. public MqttMsgConnack()
  68. {
  69. type = MQTT_MSG_CONNACK_TYPE;
  70. }
  71. /// <summary>
  72. /// Parse bytes for a CONNACK message
  73. /// </summary>
  74. /// <param name="fixedHeaderFirstByte">First fixed header byte</param>
  75. /// <param name="protocolVersion">Protocol Version</param>
  76. /// <param name="channel">Channel connected to the broker</param>
  77. /// <returns>CONNACK message instance</returns>
  78. public static MqttMsgConnack Parse(byte fixedHeaderFirstByte, byte protocolVersion, IMqttNetworkChannel channel)
  79. {
  80. byte[] buffer;
  81. MqttMsgConnack msg = new MqttMsgConnack();
  82. if (protocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1_1)
  83. {
  84. // [v3.1.1] check flag bits
  85. if ((fixedHeaderFirstByte & MSG_FLAG_BITS_MASK) != MQTT_MSG_CONNACK_FLAG_BITS)
  86. throw new MqttClientException(MqttClientErrorCode.InvalidFlagBits);
  87. }
  88. // get remaining length and allocate buffer
  89. int remainingLength = MqttMsgBase.decodeRemainingLength(channel);
  90. buffer = new byte[remainingLength];
  91. // read bytes from socket...
  92. channel.Receive(buffer);
  93. if (protocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1_1)
  94. {
  95. // [v3.1.1] ... set session present flag ...
  96. msg.sessionPresent = (buffer[CONN_ACK_FLAGS_BYTE_OFFSET] & SESSION_PRESENT_FLAG_MASK) != 0x00;
  97. }
  98. // ...and set return code from broker
  99. msg.returnCode = buffer[CONN_RETURN_CODE_BYTE_OFFSET];
  100. return msg;
  101. }
  102. public override byte[] GetBytes(byte ProtocolVersion)
  103. {
  104. int fixedHeaderSize = 0;
  105. int varHeaderSize = 0;
  106. int payloadSize = 0;
  107. int remainingLength = 0;
  108. byte[] buffer;
  109. int index = 0;
  110. if (ProtocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1_1)
  111. // flags byte and connect return code
  112. varHeaderSize += (CONN_ACK_FLAGS_BYTE_SIZE + CONN_RETURN_CODE_BYTE_SIZE);
  113. else
  114. // topic name compression response and connect return code
  115. varHeaderSize += (TOPIC_NAME_COMP_RESP_BYTE_SIZE + CONN_RETURN_CODE_BYTE_SIZE);
  116. remainingLength += (varHeaderSize + payloadSize);
  117. // first byte of fixed header
  118. fixedHeaderSize = 1;
  119. int temp = remainingLength;
  120. // increase fixed header size based on remaining length
  121. // (each remaining length byte can encode until 128)
  122. do
  123. {
  124. fixedHeaderSize++;
  125. temp = temp / 128;
  126. } while (temp > 0);
  127. // allocate buffer for message
  128. buffer = new byte[fixedHeaderSize + varHeaderSize + payloadSize];
  129. // first fixed header byte
  130. if (ProtocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1_1)
  131. buffer[index++] = (MQTT_MSG_CONNACK_TYPE << MSG_TYPE_OFFSET) | MQTT_MSG_CONNACK_FLAG_BITS; // [v.3.1.1]
  132. else
  133. buffer[index++] = (byte)(MQTT_MSG_CONNACK_TYPE << MSG_TYPE_OFFSET);
  134. // encode remaining length
  135. index = encodeRemainingLength(remainingLength, buffer, index);
  136. if (ProtocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1_1)
  137. // [v3.1.1] session present flag
  138. buffer[index++] = sessionPresent ? (byte)(1 << SESSION_PRESENT_FLAG_OFFSET) : (byte)0x00;
  139. else
  140. // topic name compression response (reserved values. not used);
  141. buffer[index++] = 0x00;
  142. // connect return code
  143. buffer[index++] = returnCode;
  144. return buffer;
  145. }
  146. public override string ToString()
  147. {
  148. #if TRACE
  149. return GetTraceString(
  150. "CONNACK",
  151. new object[] { "returnCode" },
  152. new object[] { returnCode });
  153. #else
  154. return base.ToString();
  155. #endif
  156. }
  157. }
  158. }