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