MqttMsgPingResp.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 PINGRESP message from client to broker
  18. /// </summary>
  19. public class MqttMsgPingResp : MqttMsgBase
  20. {
  21. /// <summary>
  22. /// Constructor
  23. /// </summary>
  24. public MqttMsgPingResp()
  25. {
  26. type = MQTT_MSG_PINGRESP_TYPE;
  27. }
  28. /// <summary>
  29. /// Parse bytes for a PINGRESP message
  30. /// </summary>
  31. /// <param name="fixedHeaderFirstByte">First fixed header byte</param>
  32. /// <param name="protocolVersion">Protocol Version</param>
  33. /// <param name="channel">Channel connected to the broker</param>
  34. /// <returns>PINGRESP message instance</returns>
  35. public static MqttMsgPingResp Parse(byte fixedHeaderFirstByte, byte protocolVersion, IMqttNetworkChannel channel)
  36. {
  37. MqttMsgPingResp msg = new MqttMsgPingResp();
  38. if (protocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1_1)
  39. {
  40. // [v3.1.1] check flag bits
  41. if ((fixedHeaderFirstByte & MSG_FLAG_BITS_MASK) != MQTT_MSG_PINGRESP_FLAG_BITS)
  42. throw new MqttClientException(MqttClientErrorCode.InvalidFlagBits);
  43. }
  44. // already know remaininglength is zero (MQTT specification),
  45. // so it isn't necessary to read other data from socket
  46. //int remainingLength = decodeRemainingLength(channel);
  47. 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. index++;
  60. buffer[index] = 0x00;
  61. return buffer;
  62. }
  63. public override string ToString()
  64. {
  65. #if TRACE
  66. return GetTraceString(
  67. "PINGRESP",
  68. null,
  69. null);
  70. #else
  71. return base.ToString();
  72. #endif
  73. }
  74. }
  75. }