Ejemplo n.º 1
0
        internal override MqttMessage Decode(byte[] message)
        {
            SubscriptionAckMessage subackMessage = new SubscriptionAckMessage();

            int  index       = 0;
            byte fixedHeader = message[index];

            base.DecodeFixedHeader(fixedHeader);

            int remainingLength = base.DecodeRemainingLength(message);

            int temp = remainingLength; //increase the fixed header size

            do
            {
                index++;
                temp = temp / 128;
            } while (temp > 0);

            index++;

            byte[] buffer = new byte[remainingLength];
            Buffer.BlockCopy(message, index, buffer, 0, buffer.Length);

            ushort messageId = (ushort)((buffer[0] << 8) & 0xFF00);

            messageId |= buffer[1];

            this.MessageId = messageId;

            while (index < buffer.Length)
            {
                QualityOfServiceLevelType qosLevel = (QualityOfServiceLevelType)buffer[index++];
                this._qosLevels.Add(qosLevel);
            }


            return(subackMessage);
        }
Ejemplo n.º 2
0
        public static MqttMessage DecodeMessage(byte[] message)
        {
            byte fixedHeader = message[0];
            byte msgType     = (byte)(fixedHeader >> 0x04);

            MqttMessageType messageType = (MqttMessageType)msgType;

            MqttMessage mqttMessage = null;

            switch (messageType)
            {
            case MqttMessageType.CONNECT:
                mqttMessage = new ConnectMessage();
                break;

            case MqttMessageType.CONNACK:
                mqttMessage = new ConnectAckMessage();
                break;

            case MqttMessageType.PUBLISH:
                mqttMessage = new PublishMessage();
                break;

            case MqttMessageType.PUBACK:
                mqttMessage = new PublishAckMessage();
                break;

            case MqttMessageType.PUBREC:
                mqttMessage = new PublishAckMessage();
                break;

            case MqttMessageType.PUBREL:
                mqttMessage = new PublishAckMessage();
                break;

            case MqttMessageType.PUBCOMP:
                mqttMessage = new PublishAckMessage();
                break;

            case MqttMessageType.SUBSCRIBE:
                mqttMessage = new SubscribeMessage();
                break;

            case MqttMessageType.SUBACK:
                mqttMessage = new SubscriptionAckMessage();
                break;

            case MqttMessageType.UNSUBSCRIBE:
                mqttMessage = new UnsubscribeMessage();
                break;

            case MqttMessageType.UNSUBACK:
                mqttMessage = new UnsubscribeAckMessage();
                break;

            case MqttMessageType.PINGREQ:
                mqttMessage = new PingRequestMessage();
                break;

            case MqttMessageType.PINGRESP:
                mqttMessage = new PingResponseMessage();
                break;

            case MqttMessageType.DISCONNECT:
                mqttMessage = new DisconnectMessage();
                break;

            default:
                throw new InvalidOperationException("Unknown message type.");
            }

            mqttMessage.Decode(message);
            return(mqttMessage);
        }