示例#1
0
        public void Setup()
        {
            var message = new MqttApplicationMessageBuilder()
                          .WithTopic("A")
                          .Build();

            _packet = message.ToPublishPacket();
            var serializer       = new MqttPacketSerializer();
            var serializedPacket = Join(serializer.Serialize(_packet));

            _iterations = 10000;

            _stream = new MemoryStream(_iterations * serializedPacket.Length);

            for (var i = 0; i < _iterations; i++)
            {
                _stream.Write(serializedPacket, 0, serializedPacket.Length);
            }

            _stream.Position = 0;

            var channel = new TestMqttChannel(_stream);

            _channelAdapter = new MqttChannelAdapter(channel, serializer, new MqttNetLogger().CreateChildLogger(nameof(MqttChannelAdapter)));
        }
示例#2
0
        public void TestTryDeserialize()
        {
            var serializer = new MqttPacketSerializer();

            var buffer = serializer.Serialize(new MqttPublishPacket()
            {
                Topic = "a", Payload = new byte[5]
            });

            var sequence = new ReadOnlySequence <byte>(buffer.Array, buffer.Offset, buffer.Count);

            var            part = sequence;
            MqttBasePacket packet;
            var            consumed = part.Start;
            var            observed = part.Start;
            var            result   = false;

            part   = sequence.Slice(sequence.Start, 0); // empty message should fail
            result = serializer.TryDeserialize(part, out packet, out consumed, out observed);
            Assert.IsFalse(result);


            part   = sequence.Slice(sequence.Start, 1); // partial fixed header should fail
            result = serializer.TryDeserialize(part, out packet, out consumed, out observed);
            Assert.IsFalse(result);

            part   = sequence.Slice(sequence.Start, 4); // partial body should fail
            result = serializer.TryDeserialize(part, out packet, out consumed, out observed);
            Assert.IsFalse(result);

            part   = sequence; // complete msg should work
            result = serializer.TryDeserialize(part, out packet, out consumed, out observed);
            Assert.IsTrue(result);
        }
        private static void SerializeAndCompare(MqttBasePacket packet, string expectedBase64Value, MqttProtocolVersion protocolVersion = MqttProtocolVersion.V311)
        {
            var serializer = new MqttPacketSerializer {
                ProtocolVersion = protocolVersion
            };
            var chunks = serializer.Serialize(packet);

            Assert.AreEqual(expectedBase64Value, Convert.ToBase64String(Join(chunks)));
        }
        private static void DeserializeAndCompare(MqttBasePacket packet, string expectedBase64Value)
        {
            var serializer = new MqttPacketSerializer();

            var buffer1 = serializer.Serialize(packet);

            using (var headerStream = new MemoryStream(Join(buffer1)))
            {
                var header = MqttPacketReader.ReadHeaderFromSource(headerStream, CancellationToken.None);

                using (var bodyStream = new MemoryStream(Join(buffer1), (int)headerStream.Position, header.BodyLength))
                {
                    var deserializedPacket = serializer.Deserialize(header, bodyStream.ToArray());
                    var buffer2            = serializer.Serialize(deserializedPacket);

                    Assert.AreEqual(expectedBase64Value, Convert.ToBase64String(Join(buffer2)));
                }
            }
        }
示例#5
0
        public void Setup()
        {
            var message = new MqttApplicationMessageBuilder()
                          .WithTopic("A")
                          .Build();

            _packet           = message.ToPublishPacket();
            _serializer       = new MqttPacketSerializer();
            _serializedPacket = _serializer.Serialize(_packet);
        }
示例#6
0
        private static void DeserializeAndCompare(MqttBasePacket packet, string expectedBase64Value, MqttProtocolVersion protocolVersion = MqttProtocolVersion.V311)
        {
            var serializer = new MqttPacketSerializer {
                ProtocolVersion = protocolVersion
            };

            var buffer1 = serializer.Serialize(packet);

            using (var headerStream = new MemoryStream(Join(buffer1)))
            {
                var header = MqttPacketReader.ReadHeaderAsync(headerStream, CancellationToken.None).GetAwaiter().GetResult();

                using (var bodyStream = new MemoryStream(Join(buffer1), (int)headerStream.Position, header.BodyLength))
                {
                    var deserializedPacket = serializer.Deserialize(header, bodyStream);
                    var buffer2            = serializer.Serialize(deserializedPacket);

                    Assert.AreEqual(expectedBase64Value, Convert.ToBase64String(Join(buffer2)));
                }
            }
        }
示例#7
0
        private static void DeserializeAndCompare(MqttBasePacket packet, string expectedBase64Value, MqttProtocolVersion protocolVersion = MqttProtocolVersion.V311)
        {
            var serializer = new MqttPacketSerializer {
                ProtocolVersion = protocolVersion
            };

            var buffer1 = serializer.Serialize(packet);

            using (var headerStream = new MemoryStream(Join(buffer1)))
            {
                var channel          = new TestMqttChannel(headerStream);
                var fixedHeader      = new byte[2];
                var singleByteBuffer = new byte[1];
                var header           = MqttPacketReader.ReadFixedHeaderAsync(channel, fixedHeader, singleByteBuffer, CancellationToken.None).GetAwaiter().GetResult();

                using (var bodyStream = new MemoryStream(Join(buffer1), (int)headerStream.Position, header.RemainingLength))
                {
                    var deserializedPacket = serializer.Deserialize(new ReceivedMqttPacket(header.Flags, new MqttPacketBodyReader(bodyStream.ToArray(), 0, (int)bodyStream.Length)));
                    var buffer2            = serializer.Serialize(deserializedPacket);

                    Assert.AreEqual(expectedBase64Value, Convert.ToBase64String(Join(buffer2)));
                }
            }
        }
示例#8
0
        public void Serialize_LargePacket()
        {
            var serializer = new MqttPacketSerializer {
                ProtocolVersion = MqttProtocolVersion.V311
            };

            const int payloadLength = 80000;

            var payload = new byte[payloadLength];

            var value = 0;

            for (var i = 0; i < payloadLength; i++)
            {
                if (value > 255)
                {
                    value = 0;
                }

                payload[i] = (byte)value;
            }

            var publishPacket = new MqttPublishPacket
            {
                Topic   = "abcdefghijklmnopqrstuvwxyz0123456789",
                Payload = payload
            };

            var buffer      = serializer.Serialize(publishPacket);
            var testChannel = new TestMqttChannel(new MemoryStream(buffer.Array, buffer.Offset, buffer.Count));

            var header = MqttPacketReader.ReadFixedHeaderAsync(
                testChannel,
                new byte[2],
                new byte[1],
                CancellationToken.None).GetAwaiter().GetResult();

            var eof = buffer.Offset + buffer.Count;

            var receivedPacket = new ReceivedMqttPacket(
                header.Flags,
                new MqttPacketBodyReader(buffer.Array, eof - header.RemainingLength, buffer.Count + buffer.Offset));

            var packet = (MqttPublishPacket)serializer.Deserialize(receivedPacket);

            Assert.AreEqual(publishPacket.Topic, packet.Topic);
            Assert.IsTrue(publishPacket.Payload.SequenceEqual(packet.Payload));
        }
示例#9
0
        private static T Roundtrip <T>(T packet, MqttProtocolVersion protocolVersion = MqttProtocolVersion.V311)
            where T : MqttBasePacket
        {
            var serializer = new MqttPacketSerializer {
                ProtocolVersion = protocolVersion
            };

            var buffer1 = serializer.Serialize(packet);

            using (var headerStream = new MemoryStream(Join(buffer1)))
            {
                var channel          = new TestMqttChannel(headerStream);
                var fixedHeader      = new byte[2];
                var singleByteBuffer = new byte[1];

                var header = MqttPacketReader.ReadFixedHeaderAsync(channel, fixedHeader, singleByteBuffer, CancellationToken.None).GetAwaiter().GetResult();

                using (var bodyStream = new MemoryStream(Join(buffer1), (int)headerStream.Position, header.RemainingLength))
                {
                    return((T)serializer.Deserialize(new ReceivedMqttPacket(header.Flags, new MqttPacketBodyReader(bodyStream.ToArray(), 0, (int)bodyStream.Length))));
                }
            }
        }