MqttMsgConnack.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 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. private const byte CONN_RETURN_CODE_BYTE_OFFSET = 1;
  32. private const byte CONN_RETURN_CODE_BYTE_SIZE = 1;
  33. #endregion
  34. #region Properties...
  35. /// <summary>
  36. /// Return Code
  37. /// </summary>
  38. public byte ReturnCode
  39. {
  40. get { return this.returnCode; }
  41. set { this.returnCode = value; }
  42. }
  43. #endregion
  44. // return code for CONNACK message
  45. private byte returnCode;
  46. /// <summary>
  47. /// Constructor
  48. /// </summary>
  49. public MqttMsgConnack()
  50. {
  51. this.type = MQTT_MSG_CONNACK_TYPE;
  52. }
  53. /// <summary>
  54. /// Parse bytes for a CONNACK message
  55. /// </summary>
  56. /// <param name="fixedHeaderFirstByte">First fixed header byte</param>
  57. /// <param name="channel">Channel connected to the broker</param>
  58. /// <returns>CONNACK message instance</returns>
  59. public static MqttMsgConnack Parse(byte fixedHeaderFirstByte, IMqttNetworkChannel channel)
  60. {
  61. byte[] buffer;
  62. MqttMsgConnack msg = new MqttMsgConnack();
  63. // get remaining length and allocate buffer
  64. int remainingLength = MqttMsgBase.decodeRemainingLength(channel);
  65. buffer = new byte[remainingLength];
  66. // read bytes from socket...
  67. channel.Receive(buffer);
  68. // ...and set return code from broker
  69. msg.returnCode = buffer[CONN_RETURN_CODE_BYTE_OFFSET];
  70. return msg;
  71. }
  72. public override byte[] GetBytes()
  73. {
  74. int fixedHeaderSize = 0;
  75. int varHeaderSize = 0;
  76. int payloadSize = 0;
  77. int remainingLength = 0;
  78. byte[] buffer;
  79. int index = 0;
  80. // topic name compression response and connect return code
  81. varHeaderSize += (TOPIC_NAME_COMP_RESP_BYTE_SIZE + CONN_RETURN_CODE_BYTE_SIZE);
  82. remainingLength += (varHeaderSize + payloadSize);
  83. // first byte of fixed header
  84. fixedHeaderSize = 1;
  85. int temp = remainingLength;
  86. // increase fixed header size based on remaining length
  87. // (each remaining length byte can encode until 128)
  88. do
  89. {
  90. fixedHeaderSize++;
  91. temp = temp / 128;
  92. } while (temp > 0);
  93. // allocate buffer for message
  94. buffer = new byte[fixedHeaderSize + varHeaderSize + payloadSize];
  95. // first fixed header byte
  96. buffer[index] = (byte)(MQTT_MSG_CONNACK_TYPE << MSG_TYPE_OFFSET);
  97. index++;
  98. // encode remaining length
  99. index = this.encodeRemainingLength(remainingLength, buffer, index);
  100. // topic name compression response (reserved values. not used);
  101. buffer[index++] = 0x00;
  102. // connect return code
  103. buffer[index++] = this.returnCode;
  104. return buffer;
  105. }
  106. public override string ToString()
  107. {
  108. #if TRACE
  109. return this.GetTraceString(
  110. "CONNACK",
  111. new object[] { "returnCode" },
  112. new object[] { this.returnCode });
  113. #else
  114. return base.ToString();
  115. #endif
  116. }
  117. }
  118. }