123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- using System;
- using uPLibrary.Networking.M2Mqtt.Exceptions;
- namespace uPLibrary.Networking.M2Mqtt.Messages
- {
-
-
-
- public class MqttMsgSuback : MqttMsgBase
- {
- #region Properties...
-
-
-
- public byte[] GrantedQoSLevels
- {
- get { return this.grantedQosLevels; }
- set { this.grantedQosLevels = value; }
- }
- #endregion
-
- byte[] grantedQosLevels;
-
-
-
- public MqttMsgSuback()
- {
- this.type = MQTT_MSG_SUBACK_TYPE;
- }
-
-
-
-
-
-
-
- public static MqttMsgSuback Parse(byte fixedHeaderFirstByte, byte protocolVersion, IMqttNetworkChannel channel)
- {
- byte[] buffer;
- int index = 0;
- MqttMsgSuback msg = new MqttMsgSuback();
- if (protocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1_1)
- {
-
- if ((fixedHeaderFirstByte & MSG_FLAG_BITS_MASK) != MQTT_MSG_SUBACK_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++]);
-
- msg.grantedQosLevels = new byte[remainingLength - MESSAGE_ID_SIZE];
- int qosIdx = 0;
- do
- {
- msg.grantedQosLevels[qosIdx++] = buffer[index++];
- } while (index < remainingLength);
- 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;
- int grantedQosIdx = 0;
- for (grantedQosIdx = 0; grantedQosIdx < this.grantedQosLevels.Length; grantedQosIdx++)
- {
- payloadSize++;
- }
- 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_SUBACK_TYPE << MSG_TYPE_OFFSET) | MQTT_MSG_SUBACK_FLAG_BITS;
- else
- buffer[index++] = (byte)(MQTT_MSG_SUBACK_TYPE << MSG_TYPE_OFFSET);
-
-
- index = this.encodeRemainingLength(remainingLength, buffer, index);
-
- buffer[index++] = (byte)((this.messageId >> 8) & 0x00FF);
- buffer[index++] = (byte)(this.messageId & 0x00FF);
-
- for (grantedQosIdx = 0; grantedQosIdx < this.grantedQosLevels.Length; grantedQosIdx++)
- {
- buffer[index++] = this.grantedQosLevels[grantedQosIdx];
- }
- return buffer;
- }
- public override string ToString()
- {
- #if TRACE
- return this.GetTraceString(
- "SUBACK",
- new object[] { "messageId", "grantedQosLevels" },
- new object[] { this.messageId, this.grantedQosLevels });
- #else
- return base.ToString();
- #endif
- }
- }
- }
|