Example #1
0
        MqttBufferReader ReaderFactory(byte[] data)
        {
            var reader = new MqttBufferReader();

            reader.SetBuffer(data, 0, data.Length);
            return(reader);
        }
Example #2
0
        public void Encode_Four_Byte_Integer()
        {
            var writer = new MqttBufferWriter(4, 4);
            
            for (uint value = 0; value < 268435455; value++)
            {
                writer.WriteVariableByteInteger(value);
                
                var buffer = writer.GetBuffer();
                
                var reader = new MqttBufferReader();
                reader.SetBuffer(buffer, 0, writer.Length);
                var checkValue = reader.ReadVariableByteInteger();

                Assert.AreEqual(value, checkValue);
                
                writer.Reset(0);
            }
        }
Example #3
0
        public void Write_And_Read_Multiple_Times()
        {
            var writer = new MqttBufferWriter(4096, 65535);

            writer.WriteString("A relative short string.");
            writer.WriteBinaryData(new byte[1234]);
            writer.WriteByte(0x01);
            writer.WriteByte(0x02);
            writer.WriteVariableByteInteger(5647382);
            writer.WriteString("A relative short string.");
            writer.WriteVariableByteInteger(8574489);
            writer.WriteBinaryData(new byte[48]);
            writer.WriteByte(2);
            writer.WriteByte(0x02);
            writer.WriteString("fjgffiogfhgfhoihgoireghreghreguhreguireoghreouighreouighreughreguiorehreuiohruiorehreuioghreug");
            writer.WriteBinaryData(new byte[3]);

            var readPayload = new ArraySegment <byte>(writer.GetBuffer(), 0, writer.Length).ToArray();

            var reader = new MqttBufferReader();

            reader.SetBuffer(readPayload, 0, readPayload.Length);

            for (var i = 0; i < 100000; i++)
            {
                reader.Seek(0);

                reader.ReadString();
                reader.ReadBinaryData();
                reader.ReadByte();
                reader.ReadByte();
                reader.ReadVariableByteInteger();
                reader.ReadString();
                reader.ReadVariableByteInteger();
                reader.ReadBinaryData();
                reader.ReadByte();
                reader.ReadByte();
                reader.ReadString();
                reader.ReadBinaryData();
            }
        }
Example #4
0
        public void Use_All_Data_Types()
        {
            var writer = new MqttBufferWriter(4096, 65535);

            writer.WriteString("AString");
            writer.WriteByte(0x1);
            writer.WriteByte(0x0);
            writer.WriteByte(0x1);
            writer.WriteVariableByteInteger(1234U);
            writer.WriteVariableByteInteger(9876U);

            var buffer = writer.GetBuffer();

            var reader = new MqttBufferReader();

            reader.SetBuffer(buffer, 0, writer.Length);

            Assert.AreEqual("AString", reader.ReadString());
            Assert.IsTrue(reader.ReadByte() == 1);
            Assert.IsTrue(reader.ReadByte() == 0);
            Assert.IsTrue(reader.ReadByte() == 1);
            Assert.AreEqual(1234U, reader.ReadVariableByteInteger());
            Assert.AreEqual(9876U, reader.ReadVariableByteInteger());
        }
Example #5
0
        public void Read_100_000_Messages()
        {
            var reader = new MqttBufferReader();

            reader.SetBuffer(_readPayload, 0, _readPayload.Length);

            for (var i = 0; i < 100000; i++)
            {
                reader.Seek(0);

                reader.ReadString();
                reader.ReadBinaryData();
                reader.ReadByte();
                reader.ReadByte();
                reader.ReadVariableByteInteger();
                reader.ReadString();
                reader.ReadVariableByteInteger();
                reader.ReadBinaryData();
                reader.ReadByte();
                reader.ReadByte();
                reader.ReadString();
                reader.ReadBinaryData();
            }
        }
        MqttPacket DecodeAuthPacket(ArraySegment <byte> body)
        {
            ThrowIfBodyIsEmpty(body);

            _bufferReader.SetBuffer(body.Array, body.Offset, body.Count);

            var packet = new MqttAuthPacket();

            if (_bufferReader.EndOfStream)
            {
                packet.ReasonCode = MqttAuthenticateReasonCode.Success;
                return(packet);
            }

            packet.ReasonCode = (MqttAuthenticateReasonCode)_bufferReader.ReadByte();

            var propertiesReader = new MqttV5PropertiesReader(_bufferReader);

            while (propertiesReader.MoveNext())
            {
                if (propertiesReader.CurrentPropertyId == MqttPropertyId.AuthenticationMethod)
                {
                    packet.AuthenticationMethod = propertiesReader.ReadAuthenticationMethod();
                }
                else if (propertiesReader.CurrentPropertyId == MqttPropertyId.AuthenticationData)
                {
                    packet.AuthenticationData = propertiesReader.ReadAuthenticationData();
                }
                else if (propertiesReader.CurrentPropertyId == MqttPropertyId.ReasonString)
                {
                    packet.ReasonString = propertiesReader.ReadReasonString();
                }
                else
                {
                    propertiesReader.ThrowInvalidPropertyIdException(typeof(MqttAuthPacket));
                }
            }

            packet.UserProperties = propertiesReader.CollectedUserProperties;

            return(packet);
        }
Example #7
0
        MqttPacket DecodeConnAckPacket(ArraySegment <byte> body)
        {
            ThrowIfBodyIsEmpty(body);

            _bufferReader.SetBuffer(body.Array, body.Offset, body.Count);

            var packet = new MqttConnAckPacket();

            _bufferReader.ReadByte(); // Reserved.
            packet.ReturnCode = (MqttConnectReturnCode)_bufferReader.ReadByte();

            return(packet);
        }