Exemplo n.º 1
0
        private MqttBasePacket DeserializeConnAck(MqttPacketBodyReader body)
        {
            ThrowIfBodyIsEmpty(body);

            var packet = new MqttConnAckPacket();

            var acknowledgeFlags = body.ReadByte();

            if (ProtocolVersion == MqttProtocolVersion.V311)
            {
                packet.IsSessionPresent = (acknowledgeFlags & 0x1) > 0;
            }

            packet.ConnectReturnCode = (MqttConnectReturnCode)body.ReadByte();

            return(packet);
        }
Exemplo n.º 2
0
        private static MqttBasePacket DeserializeSubAck(MqttPacketBodyReader body)
        {
            ThrowIfBodyIsEmpty(body);

            var packet = new MqttSubAckPacket
            {
                PacketIdentifier = body.ReadUInt16()
            };

            while (!body.EndOfStream)
            {
                packet.SubscribeReturnCodes.Add((MqttSubscribeReturnCode)body.ReadByte());
            }

            return(packet);
        }
Exemplo n.º 3
0
        private static MqttBasePacket DeserializeSubscribe(MqttPacketBodyReader body)
        {
            ThrowIfBodyIsEmpty(body);

            var packet = new MqttSubscribePacket
            {
                PacketIdentifier = body.ReadUInt16()
            };

            while (!body.EndOfStream)
            {
                packet.TopicFilters.Add(new TopicFilter(
                                            body.ReadStringWithLengthPrefix(),
                                            (MqttQualityOfServiceLevel)body.ReadByte()));
            }

            return(packet);
        }
Exemplo n.º 4
0
        private static MqttBasePacket DeserializeConnect(MqttPacketBodyReader body)
        {
            ThrowIfBodyIsEmpty(body);

            var protocolName = body.ReadStringWithLengthPrefix();

            MqttProtocolVersion protocolVersion;

            if (protocolName == "MQTT")
            {
                var protocolLevel = body.ReadByte();
                if (protocolLevel != 4)
                {
                    throw new MqttProtocolViolationException($"Protocol level ({protocolLevel}) not supported for MQTT 3.1.1.");
                }

                protocolVersion = MqttProtocolVersion.V311;
            }
            else if (protocolName == "MQIsdp")
            {
                var protocolLevel = body.ReadByte();
                if (protocolLevel != 3)
                {
                    throw new MqttProtocolViolationException($"Protocol level ({protocolLevel}) not supported for MQTT 3.1.");
                }

                protocolVersion = MqttProtocolVersion.V310;
            }
            else
            {
                throw new MqttProtocolViolationException($"Protocol name ({protocolName}) is not supported.");
            }

            var connectFlags = body.ReadByte();

            if ((connectFlags & 0x1) > 0)
            {
                throw new MqttProtocolViolationException("The first bit of the Connect Flags must be set to 0.");
            }

            var packet = new MqttConnectPacket
            {
                ProtocolVersion = protocolVersion,
                CleanSession    = (connectFlags & 0x2) > 0
            };

            var willFlag     = (connectFlags & 0x4) > 0;
            var willQoS      = (connectFlags & 0x18) >> 3;
            var willRetain   = (connectFlags & 0x20) > 0;
            var passwordFlag = (connectFlags & 0x40) > 0;
            var usernameFlag = (connectFlags & 0x80) > 0;

            packet.KeepAlivePeriod = body.ReadUInt16();
            packet.ClientId        = body.ReadStringWithLengthPrefix();

            if (willFlag)
            {
                packet.WillMessage = new MqttMessage
                {
                    Topic   = body.ReadStringWithLengthPrefix(),
                    Payload = body.ReadWithLengthPrefix().ToArray(),
                    QualityOfServiceLevel = (MqttQualityOfServiceLevel)willQoS,
                    Retain = willRetain
                };
            }

            if (usernameFlag)
            {
                packet.Username = body.ReadStringWithLengthPrefix();
            }

            if (passwordFlag)
            {
                packet.Password = body.ReadStringWithLengthPrefix();
            }

            ValidateConnectPacket(packet);
            return(packet);
        }