Ejemplo n.º 1
0
        public void Setup()
        {
            _packet = new MqttPublishPacket
            {
                Topic = "A"
            };

            var serializer = new MqttPacketFormatterAdapter(MqttProtocolVersion.V311);

            var serializedPacket = Join(serializer.Encode(_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());
        }
        public void TestTryDeserialize()
        {
            var serializer = new MqttPacketFormatterAdapter(MqttProtocolVersion.V311, new MqttBufferWriter(4096, 65535));

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

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

            var        part = sequence;
            MqttPacket packet;
            var        consumed = part.Start;
            var        observed = part.Start;
            var        result   = false;
            var        read     = 0;

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

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

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

            part   = sequence; // complete msg should work
            result = serializer.TryDecode(part, out packet, out consumed, out observed, out read);
            Assert.IsTrue(result);
        }