public static byte[] AddMqttHeader(this MqttBufferWriter writer, MqttControlPacketType header, byte[] body)
 {
     writer.WriteByte(MqttBufferWriter.BuildFixedHeader(header));
     writer.WriteVariableByteInteger((uint)body.Length);
     writer.WriteBinary(body, 0, body.Length);
     return(writer.GetBuffer());
 }
Пример #2
0
        public MqttPacketBuffer Encode(MqttPacket packet)
        {
            if (packet == null)
            {
                throw new ArgumentNullException(nameof(packet));
            }

            // Leave enough head space for max header size (fixed + 4 variable remaining length = 5 bytes)
            _bufferWriter.Reset(5);
            _bufferWriter.Seek(5);

            var fixedHeader     = EncodePacket(packet, _bufferWriter);
            var remainingLength = (uint)(_bufferWriter.Length - 5);

            var publishPacket = packet as MqttPublishPacket;

            if (publishPacket?.Payload != null)
            {
                remainingLength += (uint)publishPacket.Payload.Length;
            }

            var remainingLengthSize = MqttBufferWriter.GetLengthOfVariableInteger(remainingLength);

            var headerSize   = FixedHeaderSize + remainingLengthSize;
            var headerOffset = 5 - headerSize;

            // Position cursor on correct offset on beginning of array (has leading 0x0)
            _bufferWriter.Seek(headerOffset);
            _bufferWriter.WriteByte(fixedHeader);
            _bufferWriter.WriteVariableByteInteger(remainingLength);

            var buffer = _bufferWriter.GetBuffer();

            var firstSegment = new ArraySegment <byte>(buffer, headerOffset, _bufferWriter.Length - headerOffset);

            if (publishPacket?.Payload != null)
            {
                var payloadSegment = new ArraySegment <byte>(publishPacket.Payload, 0, publishPacket.Payload.Length);
                return(new MqttPacketBuffer(firstSegment, payloadSegment));
            }

            return(new MqttPacketBuffer(firstSegment));
        }
Пример #3
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);
            }
        }
Пример #4
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();
            }
        }
Пример #5
0
        public void GlobalSetup()
        {
            TestEnvironment.EnableLogger = false;

            var writer = new MqttBufferWriter(4096, 65535);

            writer.WriteString("A relative short string.");
            writer.WriteBinaryData(_demoPayload);
            writer.WriteByte(0x01);
            writer.WriteByte(0x02);
            writer.WriteVariableByteInteger(5647382);
            writer.WriteString("A relative short string.");
            writer.WriteVariableByteInteger(8574489);
            writer.WriteBinaryData(_demoPayload);
            writer.WriteByte(2);
            writer.WriteByte(0x02);
            writer.WriteString("fjgffiogfhgfhoihgoireghreghreguhreguireoghreouighreouighreughreguiorehreuiohruiorehreuioghreug");
            writer.WriteBinaryData(_demoPayload);

            _readPayload = new ArraySegment <byte>(writer.GetBuffer(), 0, writer.Length).ToArray();
        }
Пример #6
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());
        }