private async Task <ReceivedMqttPacket> ReceiveAsync(CancellationToken cancellationToken)
        {
            var readFixedHeaderResult = await _packetReader.ReadFixedHeaderAsync(_fixedHeaderBuffer, cancellationToken).ConfigureAwait(false);

            if (cancellationToken.IsCancellationRequested)
            {
                return(null);
            }

            try
            {
                if (readFixedHeaderResult.ConnectionClosed)
                {
                    return(null);
                }

                ReadingPacketStartedCallback?.Invoke();

                var fixedHeader = readFixedHeaderResult.FixedHeader;
                if (fixedHeader.RemainingLength == 0)
                {
                    return(new ReceivedMqttPacket(fixedHeader.Flags, null, 2));
                }

                var body       = new byte[fixedHeader.RemainingLength];
                var bodyOffset = 0;
                var chunkSize  = Math.Min(ReadBufferSize, fixedHeader.RemainingLength);

                do
                {
                    var bytesLeft = body.Length - bodyOffset;
                    if (chunkSize > bytesLeft)
                    {
                        chunkSize = bytesLeft;
                    }

                    var readBytes = await _channel.ReadAsync(body, bodyOffset, chunkSize, cancellationToken).ConfigureAwait(false);

                    if (cancellationToken.IsCancellationRequested)
                    {
                        return(null);
                    }

                    if (readBytes == 0)
                    {
                        return(null);
                    }

                    bodyOffset += readBytes;
                } while (bodyOffset < body.Length);

                var bodyReader = new MqttPacketBodyReader(body, 0, body.Length);
                return(new ReceivedMqttPacket(fixedHeader.Flags, bodyReader, fixedHeader.TotalLength));
            }
            finally
            {
                ReadingPacketCompletedCallback?.Invoke();
            }
        }
Exemple #2
0
        public void Encode_Four_Byte_Integer()
        {
            for (uint i = 0; i < 268435455; i++)
            {
                var buffer = MqttPacketWriter.EncodeVariableLengthInteger(i);
                var reader = new MqttPacketBodyReader(buffer.Array, buffer.Offset, buffer.Count);

                var checkValue = reader.ReadVariableLengthInteger();

                Assert.AreEqual(i, checkValue);
            }
        }
Exemple #3
0
        public void Encode_Four_Byte_Integer()
        {
            for (uint i = 0; i < 268435455; i++)
            {
                var writer = new MqttPacketWriter();
                writer.WriteVariableLengthInteger(i);
                var buffer = writer.GetBuffer();

                var reader     = new MqttPacketBodyReader(buffer, 0, writer.Length);
                var checkValue = reader.ReadVariableLengthInteger();

                Assert.AreEqual(i, checkValue);
            }
        }
Exemple #4
0
 public MqttReceivedPacket(byte fixedHeader, MqttPacketBodyReader body)
 {
     FixedHeader = fixedHeader;
     Body        = body;
 }
Exemple #5
0
        async Task <ReceivedMqttPacket> ReceiveAsync(CancellationToken cancellationToken)
        {
            if (cancellationToken.IsCancellationRequested)
            {
                return(null);
            }

            var readFixedHeaderResult = await ReadFixedHeaderAsync(cancellationToken).ConfigureAwait(false);

            if (cancellationToken.IsCancellationRequested)
            {
                return(null);
            }

            if (readFixedHeaderResult.ConnectionClosed)
            {
                return(null);
            }

            try
            {
                IsReadingPacket = true;

                var fixedHeader = readFixedHeaderResult.FixedHeader;
                if (fixedHeader.RemainingLength == 0)
                {
                    return(new ReceivedMqttPacket(fixedHeader.Flags, new MqttPacketBodyReader(new byte[0], 0, 0), 2));
                }

                var bodyLength = fixedHeader.RemainingLength;
                var body       = new byte[bodyLength];

                var bodyOffset = 0;
                var chunkSize  = Math.Min(ReadBufferSize, bodyLength);

                do
                {
                    var bytesLeft = body.Length - bodyOffset;
                    if (chunkSize > bytesLeft)
                    {
                        chunkSize = bytesLeft;
                    }

                    var readBytes = await _channel.ReadAsync(body, bodyOffset, chunkSize, cancellationToken).ConfigureAwait(false);

                    if (cancellationToken.IsCancellationRequested)
                    {
                        return(null);
                    }

                    if (readBytes == 0)
                    {
                        return(null);
                    }

                    bodyOffset += readBytes;
                } while (bodyOffset < bodyLength);

                _packetInspectorHandler.FillReceiveBuffer(body);

                var bodyReader = new MqttPacketBodyReader(body, 0, bodyLength);
                return(new ReceivedMqttPacket(fixedHeader.Flags, bodyReader, fixedHeader.TotalLength));
            }
            finally
            {
                IsReadingPacket = false;
            }
        }