Exemplo n.º 1
0
        private static MqttBasePacket DeserializePublish(MqttPacketReader reader, MqttPacketHeader mqttPacketHeader)
        {
            var fixedHeader           = new ByteReader(mqttPacketHeader.FixedHeader);
            var retain                = fixedHeader.Read();
            var qualityOfServiceLevel = (MqttQualityOfServiceLevel)fixedHeader.Read(2);
            var dup = fixedHeader.Read();

            var topic = reader.ReadStringWithLengthPrefix();

            ushort packetIdentifier = 0;

            if (qualityOfServiceLevel > MqttQualityOfServiceLevel.AtMostOnce)
            {
                packetIdentifier = reader.ReadUInt16();
            }

            var packet = new MqttPublishPacket
            {
                Retain = retain,
                QualityOfServiceLevel = qualityOfServiceLevel,
                Dup              = dup,
                Topic            = topic,
                Payload          = reader.ReadRemainingData(),
                PacketIdentifier = packetIdentifier
            };

            return(packet);
        }
Exemplo n.º 2
0
        private static MqttBasePacket Deserialize(MqttPacketHeader header, MqttPacketReader reader)
        {
            switch (header.ControlPacketType)
            {
            case MqttControlPacketType.Connect: return(DeserializeConnect(reader));

            case MqttControlPacketType.ConnAck: return(DeserializeConnAck(reader));

            case MqttControlPacketType.Disconnect: return(new MqttDisconnectPacket());

            case MqttControlPacketType.Publish: return(DeserializePublish(reader, header));

            case MqttControlPacketType.PubAck: return(DeserializePubAck(reader));

            case MqttControlPacketType.PubRec: return(DeserializePubRec(reader));

            case MqttControlPacketType.PubRel: return(DeserializePubRel(reader));

            case MqttControlPacketType.PubComp: return(DeserializePubComp(reader));

            case MqttControlPacketType.PingReq: return(new MqttPingReqPacket());

            case MqttControlPacketType.PingResp: return(new MqttPingRespPacket());

            case MqttControlPacketType.Subscribe: return(DeserializeSubscribe(reader));

            case MqttControlPacketType.SubAck: return(DeserializeSubAck(reader));

            case MqttControlPacketType.Unsubscibe: return(DeserializeUnsubscribe(reader));

            case MqttControlPacketType.UnsubAck: return(DeserializeUnsubAck(reader));

            default: throw new MqttProtocolViolationException($"Packet type ({(int)header.ControlPacketType}) not supported.");
            }
        }
Exemplo n.º 3
0
        public MqttBasePacket Deserialize(MqttPacketHeader header, MemoryStream body)
        {
            if (header == null)
            {
                throw new ArgumentNullException(nameof(header));
            }
            if (body == null)
            {
                throw new ArgumentNullException(nameof(body));
            }

            using (var reader = new MqttPacketReader(header, body))
            {
                return(Deserialize(header, reader));
            }
        }
Exemplo n.º 4
0
        private static MqttBasePacket Deserialize(MqttPacketHeader header, MqttPacketReader reader)
        {
            switch (header.ControlPacketType)
            {
            case MqttControlPacketType.Connect:
            {
                return(DeserializeConnect(reader));
            }

            case MqttControlPacketType.ConnAck:
            {
                return(DeserializeConnAck(reader));
            }

            case MqttControlPacketType.Disconnect:
            {
                return(new MqttDisconnectPacket());
            }

            case MqttControlPacketType.Publish:
            {
                return(DeserializePublish(reader, header));
            }

            case MqttControlPacketType.PubAck:
            {
                return(new MqttPubAckPacket
                    {
                        PacketIdentifier = reader.ReadUInt16()
                    });
            }

            case MqttControlPacketType.PubRec:
            {
                return(new MqttPubRecPacket
                    {
                        PacketIdentifier = reader.ReadUInt16()
                    });
            }

            case MqttControlPacketType.PubRel:
            {
                return(new MqttPubRelPacket
                    {
                        PacketIdentifier = reader.ReadUInt16()
                    });
            }

            case MqttControlPacketType.PubComp:
            {
                return(new MqttPubCompPacket
                    {
                        PacketIdentifier = reader.ReadUInt16()
                    });
            }

            case MqttControlPacketType.PingReq:
            {
                return(new MqttPingReqPacket());
            }

            case MqttControlPacketType.PingResp:
            {
                return(new MqttPingRespPacket());
            }

            case MqttControlPacketType.Subscribe:
            {
                return(DeserializeSubscribe(reader));
            }

            case MqttControlPacketType.SubAck:
            {
                return(DeserializeSubAck(reader));
            }

            case MqttControlPacketType.Unsubscibe:
            {
                return(DeserializeUnsubscribe(reader));
            }

            case MqttControlPacketType.UnsubAck:
            {
                return(new MqttUnsubAckPacket
                    {
                        PacketIdentifier = reader.ReadUInt16()
                    });
            }

            default:
            {
                throw new MqttProtocolViolationException($"Packet type ({(int)header.ControlPacketType}) not supported.");
            }
            }
        }
Exemplo n.º 5
0
 public ReceivedMqttPacket(MqttPacketHeader header, byte[] body)
 {
     Header = header ?? throw new ArgumentNullException(nameof(header));
     Body   = body ?? throw new ArgumentNullException(nameof(body));
 }
Exemplo n.º 6
0
 public MqttPacketReader(MqttPacketHeader header, Stream bodyStream)
     : base(bodyStream, Encoding.UTF8, true)
 {
     _header = header;
 }