MqttMsgPuback.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 uPLibrary.Networking.M2Mqtt.Exceptions;
  14. namespace uPLibrary.Networking.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. this.type = MQTT_MSG_PUBACK_TYPE;
  27. }
  28. public override byte[] GetBytes(byte protocolVersion)
  29. {
  30. int fixedHeaderSize = 0;
  31. int varHeaderSize = 0;
  32. int payloadSize = 0;
  33. int remainingLength = 0;
  34. byte[] buffer;
  35. int index = 0;
  36. // message identifier
  37. varHeaderSize += MESSAGE_ID_SIZE;
  38. remainingLength += (varHeaderSize + payloadSize);
  39. // first byte of fixed header
  40. fixedHeaderSize = 1;
  41. int temp = remainingLength;
  42. // increase fixed header size based on remaining length
  43. // (each remaining length byte can encode until 128)
  44. do
  45. {
  46. fixedHeaderSize++;
  47. temp = temp / 128;
  48. } while (temp > 0);
  49. // allocate buffer for message
  50. buffer = new byte[fixedHeaderSize + varHeaderSize + payloadSize];
  51. // first fixed header byte
  52. if (protocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1_1)
  53. buffer[index++] = (MQTT_MSG_PUBACK_TYPE << MSG_TYPE_OFFSET) | MQTT_MSG_PUBACK_FLAG_BITS; // [v.3.1.1]
  54. else
  55. buffer[index++] = (MQTT_MSG_PUBACK_TYPE << MSG_TYPE_OFFSET);
  56. // encode remaining length
  57. index = this.encodeRemainingLength(remainingLength, buffer, index);
  58. // get message identifier
  59. buffer[index++] = (byte)((this.messageId >> 8) & 0x00FF); // MSB
  60. buffer[index++] = (byte)(this.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 = MqttMsgBase.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. msg.messageId |= (buffer[index++]);
  89. return msg;
  90. }
  91. public override string ToString()
  92. {
  93. #if TRACE
  94. return this.GetTraceString(
  95. "PUBACK",
  96. new object[] { "messageId" },
  97. new object[] { this.messageId });
  98. #else
  99. return base.ToString();
  100. #endif
  101. }
  102. }
  103. }