Example #1
0
        MqttCommand ICommandReader.Read(NetworkConnection connection)
        {
            FixedHeader header;
            byte[] data = null;

            header = FixedHeader.Load(connection);

            if (header.RemainingLength > 0)
            {
                if (connection.Available >= header.RemainingLength)
                {
                    data = connection.Stream.ReadBytesOrFail(header.RemainingLength);
                }
                else
                {
                    data = connection.Stream.ReadBytesOrFailAsync(header.RemainingLength).Await<byte[]>().Result;
                }
            }

            MqttCommand cmd = MqttCommand.Create(header, data);

            System.Diagnostics.Debug.WriteLine("RECV {0}", cmd);

            return cmd;
        }
Example #2
0
        void ICommandWriter.Send(NetworkConnection connection, MqttCommand command)
        {
            byte[] bytes = command.ToByteArray();

            System.Diagnostics.Debug.WriteLine("SEND {0} => {1}", command, BitConverter.ToString(bytes).Replace("-", " "));

            connection.Stream.Write(bytes, 0, bytes.Length);
        }
Example #3
0
        MqttCommand ICommandReader.Read(NetworkConnection connection)
        {
            var header = FixedHeader.Load(connection);

            byte[] data = connection.ReadBytesOrFailAsync(header.RemainingLength).Await().Result;

            return MqttCommand.Create(header, data);
        }
Example #4
0
        public void Start(TcpClient client, Action<MqttCommand> onIncomingMessage)
        {
            _connection= new NetworkConnection(client);

            _recvThread = new Thread(() =>
                {
                    ReceiveLoop(onIncomingMessage);
                });

            _recvThread.Start();
        }
Example #5
0
        internal NamedConnection Run(NetworkConnection connection)
        {
            MqttCommand command = _reader.Read(connection);
            if (command.CommandMessage != Types.CommandMessage.CONNECT)
            {
                throw new ProtocolException(command.CommandMessage, "Expected CONNECT");
            }

            Connect connect = (Connect)command;

            _writer.Send(connection, new ConnAck());

            return new NamedConnection(connect.ClientIdentifier, connection);
        }
Example #6
0
        public static FixedHeader Load(NetworkConnection connection)
        {
            byte firstByte = connection.ReadBytesOrFailAsync(1).Await().Result[0];

            var header = new FixedHeader
                {
                    Message = (CommandMessage) ((firstByte & 0xF0) >> 4),
                    Duplicate = (firstByte & 0x8) == 0x8,
                    QualityOfService = (QualityOfService) ((firstByte & 0x6) >> 1),
                    Retain = (firstByte & 0x1) == 0x1,
                    RemainingLength = VariableLengthInteger.Load(connection)
                };

            return header;
        }
Example #7
0
        internal static int Load(NetworkConnection connection)
        {
            int result = 0;
            int multiplier = 1;
            int digit = 0;
            int bytesRead = 0;

            do
            {
                digit = connection.Stream.ReadBytesOrFailAsync(1).Await<byte[]>().Result[0];
                result += (digit & 127) * multiplier;
                multiplier *= 128;
                bytesRead++;
            } while ((digit & 128) != 0 && (bytesRead < 4));

            return result;
        }
Example #8
0
        public static FixedHeader Load(NetworkConnection connection)
        {
            byte firstByte;

            if (connection.Available >= 1)
            {
                firstByte = connection.Stream.ReadByteOrFail();
            }
            else
            {
                firstByte = connection.Stream.ReadBytesOrFailAsync(1).Await<byte[]>().Result[0];
            }

            FixedHeader header = new FixedHeader();
            header.Message = (CommandMessage)((firstByte & 0xF0) >> 4);
            header.Duplicate = (firstByte & 0x8) == 0x8;
            header.QualityOfService = (QualityOfService)((firstByte & 0x6) >> 1);
            header.Retain = (firstByte & 0x1) == 0x1;

            header.RemainingLength = VariableLengthInteger.Load(connection);

            return header;
        }
Example #9
0
 public NamedConnection(string clientId, NetworkConnection connection)
 {
     ClientId = clientId;
     Connection = connection;
 }