MqttMsgPingResp.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 PINGRESP message from client to broker
  19. /// </summary>
  20. public class MqttMsgPingResp : MqttMsgBase
  21. {
  22. /// <summary>
  23. /// Constructor
  24. /// </summary>
  25. public MqttMsgPingResp()
  26. {
  27. this.type = MQTT_MSG_PINGRESP_TYPE;
  28. }
  29. /// <summary>
  30. /// Parse bytes for a PINGRESP 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>PINGRESP message instance</returns>
  36. public static MqttMsgPingResp Parse(byte fixedHeaderFirstByte, byte protocolVersion, IMqttNetworkChannel channel)
  37. {
  38. MqttMsgPingResp msg = new MqttMsgPingResp();
  39. if (protocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1_1)
  40. {
  41. // [v3.1.1] check flag bits
  42. if ((fixedHeaderFirstByte & MSG_FLAG_BITS_MASK) != MQTT_MSG_PINGRESP_FLAG_BITS)
  43. throw new MqttClientException(MqttClientErrorCode.InvalidFlagBits);
  44. }
  45. // already know remaininglength is zero (MQTT specification),
  46. // so it isn't necessary to read other data from socket
  47. int remainingLength = MqttMsgBase.decodeRemainingLength(channel);
  48. return msg;
  49. }
  50. public override byte[] GetBytes(byte protocolVersion)
  51. {
  52. byte[] buffer = new byte[2];
  53. int index = 0;
  54. // first fixed header byte
  55. if (protocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1_1)
  56. buffer[index++] = (MQTT_MSG_PINGRESP_TYPE << MSG_TYPE_OFFSET) | MQTT_MSG_PINGRESP_FLAG_BITS; // [v.3.1.1]
  57. else
  58. buffer[index++] = (MQTT_MSG_PINGRESP_TYPE << MSG_TYPE_OFFSET);
  59. buffer[index++] = 0x00;
  60. return buffer;
  61. }
  62. public override string ToString()
  63. {
  64. #if TRACE
  65. return this.GetTraceString(
  66. "PINGRESP",
  67. null,
  68. null);
  69. #else
  70. return base.ToString();
  71. #endif
  72. }
  73. }
  74. }