コード例 #1
0
        public void Reset_After_Usage()
        {
            var writer = new MqttBufferWriter(4096, 65535);

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

            writer.Reset(0);

            Assert.AreEqual(0, writer.Length);
        }
コード例 #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_100_000_Messages()
        {
            var writer = new MqttBufferWriter(4096, 65535);

            for (var i = 0; i < 100000; i++)
            {
                writer.WriteString("A relative short string.");
                writer.WriteByte(0x01);
                writer.WriteByte(0x02);
                writer.WriteVariableByteInteger(5647382);
                writer.WriteString("A relative short string.");
                writer.WriteVariableByteInteger(8574589);
                writer.WriteBinaryData(_demoPayload);
                writer.WriteByte(2);
                writer.WriteByte(0x02);
                writer.WriteString("fjgffiogfhgfhoihgoireghreghreguhreguireoghreouighreouighreughreguiorehreuiohruiorehreuioghreug");
                writer.WriteBinaryData(_demoPayload);

                writer.Reset(0);
            }
        }
コード例 #5
0
 public void Reset()
 {
     _bufferWriter.Reset(0);
     _bufferWriter.Cleanup();
 }