MqttMsgUnsuback.cs 4.1 KB

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