123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- using System;
- using uPLibrary.Networking.M2Mqtt.Exceptions;
- namespace uPLibrary.Networking.M2Mqtt.Messages
- {
-
-
-
- public class MqttMsgUnsuback : MqttMsgBase
- {
-
-
-
- public MqttMsgUnsuback()
- {
- this.type = MQTT_MSG_UNSUBACK_TYPE;
- }
-
-
-
-
-
-
-
- public static MqttMsgUnsuback Parse(byte fixedHeaderFirstByte, byte protocolVersion, IMqttNetworkChannel channel)
- {
- byte[] buffer;
- int index = 0;
- MqttMsgUnsuback msg = new MqttMsgUnsuback();
- if (protocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1_1)
- {
-
- if ((fixedHeaderFirstByte & MSG_FLAG_BITS_MASK) != MQTT_MSG_UNSUBACK_FLAG_BITS)
- throw new MqttClientException(MqttClientErrorCode.InvalidFlagBits);
- }
-
- int remainingLength = MqttMsgBase.decodeRemainingLength(channel);
- buffer = new byte[remainingLength];
-
- channel.Receive(buffer);
-
- msg.messageId = (ushort)((buffer[index++] << 8) & 0xFF00);
- msg.messageId |= (buffer[index++]);
- return msg;
- }
- public override byte[] GetBytes(byte protocolVersion)
- {
- int fixedHeaderSize = 0;
- int varHeaderSize = 0;
- int payloadSize = 0;
- int remainingLength = 0;
- byte[] buffer;
- int index = 0;
-
- varHeaderSize += MESSAGE_ID_SIZE;
- remainingLength += (varHeaderSize + payloadSize);
-
- fixedHeaderSize = 1;
- int temp = remainingLength;
-
-
- do
- {
- fixedHeaderSize++;
- temp = temp / 128;
- } while (temp > 0);
-
- buffer = new byte[fixedHeaderSize + varHeaderSize + payloadSize];
-
- if (protocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1_1)
- buffer[index++] = (MQTT_MSG_UNSUBACK_TYPE << MSG_TYPE_OFFSET) | MQTT_MSG_UNSUBACK_FLAG_BITS;
- else
- buffer[index++] = (byte)(MQTT_MSG_UNSUBACK_TYPE << MSG_TYPE_OFFSET);
-
-
- index = this.encodeRemainingLength(remainingLength, buffer, index);
-
- buffer[index++] = (byte)((this.messageId >> 8) & 0x00FF);
- buffer[index++] = (byte)(this.messageId & 0x00FF);
- return buffer;
- }
- public override string ToString()
- {
- #if TRACE
- return this.GetTraceString(
- "UNSUBACK",
- new object[] { "messageId" },
- new object[] { this.messageId });
- #else
- return base.ToString();
- #endif
- }
- }
- }
|