123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- using uPLibrary.Networking.M2Mqtt.Exceptions;
- namespace uPLibrary.Networking.M2Mqtt.Messages
- {
-
-
-
- public class MqttMsgPubrel : MqttMsgBase
- {
-
-
-
- public MqttMsgPubrel()
- {
- this.type = MQTT_MSG_PUBREL_TYPE;
-
- this.qosLevel = QOS_LEVEL_AT_LEAST_ONCE;
- }
- 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_PUBREL_TYPE << MSG_TYPE_OFFSET) | MQTT_MSG_PUBREL_FLAG_BITS;
- else
- {
- buffer[index] = (byte)((MQTT_MSG_PUBREL_TYPE << MSG_TYPE_OFFSET) |
- (this.qosLevel << QOS_LEVEL_OFFSET));
- buffer[index] |= this.dupFlag ? (byte)(1 << DUP_FLAG_OFFSET) : (byte)0x00;
- index++;
- }
-
-
- index = this.encodeRemainingLength(remainingLength, buffer, index);
-
- buffer[index++] = (byte)((this.messageId >> 8) & 0x00FF);
- buffer[index++] = (byte)(this.messageId & 0x00FF);
- return buffer;
- }
-
-
-
-
-
-
-
- public static MqttMsgPubrel Parse(byte fixedHeaderFirstByte, byte protocolVersion, IMqttNetworkChannel channel)
- {
- byte[] buffer;
- int index = 0;
- MqttMsgPubrel msg = new MqttMsgPubrel();
- if (protocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1_1)
- {
-
- if ((fixedHeaderFirstByte & MSG_FLAG_BITS_MASK) != MQTT_MSG_PUBREL_FLAG_BITS)
- throw new MqttClientException(MqttClientErrorCode.InvalidFlagBits);
- }
-
- int remainingLength = MqttMsgBase.decodeRemainingLength(channel);
- buffer = new byte[remainingLength];
-
- channel.Receive(buffer);
- if (protocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1)
- {
-
-
- msg.qosLevel = (byte)((fixedHeaderFirstByte & QOS_LEVEL_MASK) >> QOS_LEVEL_OFFSET);
-
- msg.dupFlag = (((fixedHeaderFirstByte & DUP_FLAG_MASK) >> DUP_FLAG_OFFSET) == 0x01);
- }
-
- msg.messageId = (ushort)((buffer[index++] << 8) & 0xFF00);
- msg.messageId |= (buffer[index++]);
- return msg;
- }
- public override string ToString()
- {
- #if TRACE
- return this.GetTraceString(
- "PUBREL",
- new object[] { "messageId" },
- new object[] { this.messageId });
- #else
- return base.ToString();
- #endif
- }
- }
- }
|