Esempio n. 1
0
 // ReSharper disable once ParameterOnlyUsedForPreconditionCheck.Local
 private static void ThrowIfBodyIsEmpty(MqttPacketBodyReader body)
 {
     if (body == null || body.Length == 0)
     {
         throw new MqttProtocolViolationException("Data from the body is required but not present.");
     }
 }
Esempio n. 2
0
        private static MqttBasePacket DeserializePubAck(MqttPacketBodyReader body)
        {
            ThrowIfBodyIsEmpty(body);

            return(new MqttPubAckPacket
            {
                PacketIdentifier = body.ReadUInt16()
            });
        }
Esempio n. 3
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);
        }
Esempio n. 4
0
        private static MqttBasePacket DeserializeUnsubscribe(MqttPacketBodyReader body)
        {
            ThrowIfBodyIsEmpty(body);

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

            while (!body.EndOfStream)
            {
                packet.TopicFilters.Add(body.ReadStringWithLengthPrefix());
            }

            return(packet);
        }
Esempio n. 5
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);
        }
Esempio n. 6
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);
        }
Esempio n. 7
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 MqttApplicationMessage
                {
                    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);
        }