Esempio n. 1
0
        private byte[] Read()
        {
            var            header     = new MqttFixedHeader();
            var            headerByte = new byte[1];
            int            receivedSize;
            INetworkStream stream = GetStream();

            // Read the fixed header
            do
            {
                receivedSize = stream.Receive(headerByte, 0, 1);
            } while (receivedSize > 0 && header.AppendByte(headerByte[0]));

            if (!header.IsComplete)
            {
                _logger.LogMessage("Socket", LogLevel.Error, "Header data invalid for incoming message.");
                throw new IOException("Unable to receive the MQTT fixed header.");
            }

            _logger.LogMessage("Socket", LogLevel.Verbose, "Begin reading payload for incoming message type: " + header.MessageType);

            // Create a buffer and read the remaining message
            var completeBuffer = header.CreateMessageBuffer();

            receivedSize = 0;
            while (receivedSize < header.RemainingLength)
            {
                receivedSize += stream.Receive(completeBuffer, header.HeaderSize + receivedSize, header.RemainingLength - receivedSize);
            }

            return(completeBuffer);
        }
Esempio n. 2
0
        private byte[] ReadFromInputStreamAsync(Stream stream, string clientUid, CancellationToken token)
        {
            var header     = new MqttFixedHeader();
            var headerByte = new byte[1];
            int receivedSize;

            // Read the fixed header
            do
            {
                receivedSize = stream.Read(headerByte, 0, headerByte.Length);
            } while (receivedSize > 0 && header.AppendByte(headerByte[0]));

            if (!header.IsComplete)
            {
                _logger.LogMessage("Socket", LogLevel.Error,
                                   string.Format("Read header operation could not read header, aborting."));
                return(null);
            }

            _logger.LogMessage("Socket", LogLevel.Verbose,
                               string.Format("Received message header type '{0}' from client {1}.", header.MessageType, clientUid));
            //_logger.LogMessage("Socket", LogLevel.Warning,
            //    string.Format("Received message header=0x{0:X}, Remaining length={1}.", header.Buffer[0], header.RemainingLength));

            // Create a buffer and read the remaining message
            var completeBuffer = header.CreateMessageBuffer();

            receivedSize = 0;
            while (receivedSize < header.RemainingLength)
            {
                receivedSize += stream.Read(completeBuffer, header.HeaderSize + receivedSize, header.RemainingLength - receivedSize);
            }
            //_logger.LogMessage("Socket", LogLevel.Warning,
            //    string.Format("                              Bytes read=      {0}.", receivedSize));

            if (token.IsCancellationRequested)
            {
                // Operation was cancelled
                _logger.LogMessage("Socket", LogLevel.Verbose, string.Format("Read header operation cancelled."));
                return(null);
            }

            return(completeBuffer);
        }
Esempio n. 3
0
        private async Task <byte[]> ReadFromInputStreamAsync(StreamSocket socket, CancellationToken token)
        {
            var  header = new MqttFixedHeader();
            uint bytesRead;

            using (var reader = new DataReader(socket.InputStream))
            {
                reader.InputStreamOptions = InputStreamOptions.Partial;

                // Read the fixed header
                var headerByte = new byte[1];
                do
                {
                    bytesRead = await reader.LoadAsync(1);

                    if (bytesRead > 0)
                    {
                        reader.ReadBytes(headerByte);
                    }
                } while (bytesRead > 0 && header.AppendByte(headerByte[0]));
                reader.DetachStream();

                if (token.IsCancellationRequested)
                {
                    // Operation was cancelled
                    return(null);
                }
                if (!header.IsComplete)
                {
                    _logger.LogMessage("Socket", LogLevel.Verbose, "Read header operation could not read header, aborting.");
                    return(null);
                }

                _logger.LogMessage("Socket", LogLevel.Verbose, string.Format("Received message header type '{0}' from server.", header.MessageType));
            }

            var msgBuffer = header.CreateMessageBuffer();

            //_logger.LogMessage("Socket", LogLevel.Verbose,
            //    string.Format("Received message header=0x{0:X}, Remaining length={1}.", header.Buffer[0], header.RemainingLength));

            using (var reader = new DataReader(socket.InputStream))
            {
                if (header.RemainingLength > 0)
                {
                    // Create a buffer and read the remaining message
                    bytesRead = await reader.LoadAsync((uint)header.RemainingLength);

                    //_logger.LogMessage("Socket", LogLevel.Verbose,
                    //    string.Format("                              Bytes read=      {0}.", bytesRead));
                    if (bytesRead > 0)
                    {
                        var remainingBuffer = new byte[reader.UnconsumedBufferLength];
                        reader.ReadBytes(remainingBuffer);

                        // Merge the fixed header and remaining buffers together
                        Array.ConstrainedCopy(remainingBuffer, 0, msgBuffer, header.HeaderSize, remainingBuffer.Length);
                    }
                }
                reader.DetachStream();

                if (token.IsCancellationRequested)
                {
                    // Operation was cancelled
                    return(null);
                }

                return(msgBuffer);
            }
        }