mqtt/M2Mqtt/Messages/MqttMsgPingResp.cs

80 lines
2.5 KiB
C#
Raw Normal View History

/*
2018-04-01 19:31:18 +02:00
Copyright (c) 2013, 2014 Paolo Patierno
2018-04-01 19:31:18 +02:00
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License v1.0
and Eclipse Distribution License v1.0 which accompany this distribution.
2018-04-01 19:31:18 +02:00
The Eclipse Public License is available at
http://www.eclipse.org/legal/epl-v10.html
and the Eclipse Distribution License is available at
http://www.eclipse.org/org/documents/edl-v10.php.
2018-04-01 19:31:18 +02:00
Contributors:
Paolo Patierno - initial API and implementation and/or initial documentation
*/
using System;
2018-04-01 19:31:18 +02:00
using uPLibrary.Networking.M2Mqtt.Exceptions;
2019-11-26 15:34:16 +01:00
namespace uPLibrary.Networking.M2Mqtt.Messages {
/// <summary>
/// Class for PINGRESP message from client to broker
/// </summary>
public class MqttMsgPingResp : MqttMsgBase {
/// <summary>
2019-11-26 15:34:16 +01:00
/// Constructor
/// </summary>
2019-11-26 15:34:16 +01:00
public MqttMsgPingResp() => this.Type = MQTT_MSG_PINGRESP_TYPE;
/// <summary>
/// Parse bytes for a PINGRESP message
/// </summary>
/// <param name="fixedHeaderFirstByte">First fixed header byte</param>
/// <param name="protocolVersion">Protocol Version</param>
/// <param name="channel">Channel connected to the broker</param>
/// <returns>PINGRESP message instance</returns>
public static MqttMsgPingResp Parse(Byte fixedHeaderFirstByte, Byte protocolVersion, IMqttNetworkChannel channel) {
MqttMsgPingResp msg = new MqttMsgPingResp();
if (protocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1_1) {
// [v3.1.1] check flag bits
if ((fixedHeaderFirstByte & MSG_FLAG_BITS_MASK) != MQTT_MSG_PINGRESP_FLAG_BITS) {
throw new MqttClientException(MqttClientErrorCode.InvalidFlagBits);
}
}
// already know remaininglength is zero (MQTT specification),
// so it isn't necessary to read other data from socket
_ = DecodeRemainingLength(channel);
return msg;
}
public override Byte[] GetBytes(Byte protocolVersion) {
Byte[] buffer = new Byte[2];
Int32 index = 0;
// first fixed header byte
buffer[index++] = protocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1_1
? (Byte)((MQTT_MSG_PINGRESP_TYPE << MSG_TYPE_OFFSET) | MQTT_MSG_PINGRESP_FLAG_BITS)
: (Byte)(MQTT_MSG_PINGRESP_TYPE << MSG_TYPE_OFFSET);
buffer[index++] = 0x00;
return buffer;
}
public override String ToString() =>
2018-04-01 19:31:18 +02:00
#if TRACE
2019-11-26 15:34:16 +01:00
this.GetTraceString(
"PINGRESP",
null,
null);
2018-04-01 19:31:18 +02:00
#else
2019-11-26 15:34:16 +01:00
base.ToString();
2018-04-01 19:31:18 +02:00
#endif
2019-11-26 15:34:16 +01:00
}
}