MqttMsgUnsuback.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. using uPLibrary.Networking.M2Mqtt.Exceptions;
  15. namespace uPLibrary.Networking.M2Mqtt.Messages
  16. {
  17. /// <summary>
  18. /// Class for UNSUBACK message from broker to client
  19. /// </summary>
  20. public class MqttMsgUnsuback : MqttMsgBase
  21. {
  22. /// <summary>
  23. /// Constructor
  24. /// </summary>
  25. public MqttMsgUnsuback()
  26. {
  27. this.type = MQTT_MSG_UNSUBACK_TYPE;
  28. }
  29. /// <summary>
  30. /// Parse bytes for a UNSUBACK message
  31. /// </summary>
  32. /// <param name="fixedHeaderFirstByte">First fixed header byte</param>
  33. /// <param name="protocolVersion">Protocol Version</param>
  34. /// <param name="channel">Channel connected to the broker</param>
  35. /// <returns>UNSUBACK message instance</returns>
  36. public static MqttMsgUnsuback Parse(byte fixedHeaderFirstByte, byte protocolVersion, IMqttNetworkChannel channel)
  37. {
  38. byte[] buffer;
  39. int index = 0;
  40. MqttMsgUnsuback msg = new MqttMsgUnsuback();
  41. if (protocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1_1)
  42. {
  43. // [v3.1.1] check flag bits
  44. if ((fixedHeaderFirstByte & MSG_FLAG_BITS_MASK) != MQTT_MSG_UNSUBACK_FLAG_BITS)
  45. throw new MqttClientException(MqttClientErrorCode.InvalidFlagBits);
  46. }
  47. // get remaining length and allocate buffer
  48. int remainingLength = MqttMsgBase.decodeRemainingLength(channel);
  49. buffer = new byte[remainingLength];
  50. // read bytes from socket...
  51. channel.Receive(buffer);
  52. // message id
  53. msg.messageId = (ushort)((buffer[index++] << 8) & 0xFF00);
  54. msg.messageId |= (buffer[index++]);
  55. return msg;
  56. }
  57. public override byte[] GetBytes(byte protocolVersion)
  58. {
  59. int fixedHeaderSize = 0;
  60. int varHeaderSize = 0;
  61. int payloadSize = 0;
  62. int remainingLength = 0;
  63. byte[] buffer;
  64. int index = 0;
  65. // message identifier
  66. varHeaderSize += MESSAGE_ID_SIZE;
  67. remainingLength += (varHeaderSize + payloadSize);
  68. // first byte of fixed header
  69. fixedHeaderSize = 1;
  70. int temp = remainingLength;
  71. // increase fixed header size based on remaining length
  72. // (each remaining length byte can encode until 128)
  73. do
  74. {
  75. fixedHeaderSize++;
  76. temp = temp / 128;
  77. } while (temp > 0);
  78. // allocate buffer for message
  79. buffer = new byte[fixedHeaderSize + varHeaderSize + payloadSize];
  80. // first fixed header byte
  81. if (protocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1_1)
  82. buffer[index++] = (MQTT_MSG_UNSUBACK_TYPE << MSG_TYPE_OFFSET) | MQTT_MSG_UNSUBACK_FLAG_BITS; // [v.3.1.1]
  83. else
  84. buffer[index++] = (byte)(MQTT_MSG_UNSUBACK_TYPE << MSG_TYPE_OFFSET);
  85. // encode remaining length
  86. index = this.encodeRemainingLength(remainingLength, buffer, index);
  87. // message id
  88. buffer[index++] = (byte)((this.messageId >> 8) & 0x00FF); // MSB
  89. buffer[index++] = (byte)(this.messageId & 0x00FF); // LSB
  90. return buffer;
  91. }
  92. public override string ToString()
  93. {
  94. #if TRACE
  95. return this.GetTraceString(
  96. "UNSUBACK",
  97. new object[] { "messageId" },
  98. new object[] { this.messageId });
  99. #else
  100. return base.ToString();
  101. #endif
  102. }
  103. }
  104. }