MqttMsgUnsuback.cs 3.9 KB

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