Example #1
0
 public PingReq(FixedHeader header)
     : base(header)
 {
     if (header.RemainingLength != 0)
     {
         throw new ProtocolException(CommandMessage, "PingReq does not have any payload data");
     }
 }
Example #2
0
        public UnSubAck(FixedHeader header, byte[] data)
            : base(header)
        {
            if (header.RemainingLength != 2 && data.Length != 2)
            {
                throw new ProtocolException(CommandMessage, "Remaining length must be 2");
            }

            using (var stream = new MemoryStream(data))
            {
                MessageId = MessageId.FromStream(stream);
            }
        }
Example #3
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 #4
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 #5
0
        public static FixedHeader Load(NetworkConnection connection)
        {
            byte 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 #6
0
 public Connect(FixedHeader header, byte[] data)
     : base(header)
 {
     if (data != null)
     {
         using (var stream = new MemoryStream(data))
         {
             Details = V3ConnectVariableHeader.FromStream(stream);
             LoadPayload(stream);
         }
     }
     else
     {
         Details = new V3ConnectVariableHeader(180, new ConnectFlags());
     }
 }
Example #7
0
        public ConnAck(FixedHeader header, byte[] data)
            : base(header)
        {
            if (data != null)
            {
                if (header.RemainingLength != data.Length)
                {
                    throw new ProtocolException(CommandMessage, "The declared and actual data lengths did not match");
                }

                if (header.RemainingLength != 2)
                {
                    throw new ProtocolException(CommandMessage, "The declared data length must be 2");
                }

                Result = (ConnectionAckResult)data[1];
            }
        }
Example #8
0
        public Publish(FixedHeader header, byte[] data)
            : base(header)
        {
            if (header.RemainingLength > 0)
            {
                using(var stream = new MemoryStream(data))
                {
                    Topic = MqString.FromStream(stream);

                    if (Header.QualityOfService == QualityOfService.AtLeastOnce ||
                        Header.QualityOfService == QualityOfService.ExactlyOnce)
                    {
                        MessageId = MessageId.FromStream(stream);
                    }

                    Message = stream.Position < stream.Length ? stream.ReadRest() : new byte[0];
                }
            }
        }
Example #9
0
        public Subscribe(FixedHeader header, byte[] data)
            : base(header)
        {
            if (header.RemainingLength > 0)
            {
                using (var stream = new MemoryStream(data))
                {
                    if (Header.QualityOfService != QualityOfService.AtMostOnce)
                    {
                        MessageId = MessageId.FromStream(stream);

                        while (stream.Position < stream.Length)
                        {
                            Subscriptions.Add(new Subscription(MQString.FromStream(stream), (QualityOfService)stream.ReadByteOrFail()));
                        }
                    }
                }
            }
        }
Example #10
0
        public Unsubscribe(FixedHeader header, byte[] data)
            : base(header)
        {
            if (header.RemainingLength > 0)
            {
                using (var stream = new MemoryStream(data))
                {
                    if (Header.QualityOfService != QualityOfService.AtMostOnce)
                    {
                        MessageId = MessageId.FromStream(stream);

                        while (stream.Position < stream.Length)
                        {
                            _topics.Add(MQString.FromStream(stream));
                        }
                    }
                }
            }
        }
Example #11
0
        public SubAck(FixedHeader header, byte[] data)
            : base(header)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }

            using (var stream = new MemoryStream(data))
            {
                _grants = new List<QualityOfService>();

                MessageId = MessageId.FromStream(stream);

                while (stream.Position < stream.Length)
                {
                    byte qosByte = stream.ReadBytesOrFailAsync(1).Await().Result[0];
                    qosByte = (byte)(qosByte & 0x03); // 00000011
                    _grants.Add((QualityOfService)qosByte);
                }
            }
        }
Example #12
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 #13
0
 public static MqttCommand Create(FixedHeader header, byte[] data)
 {
     switch (header.Message)
     {
         case CommandMessage.CONNACK:
             return new ConnAck(header, data);
         case CommandMessage.CONNECT:
             return new Connect(header, data);
         case CommandMessage.DISCONNECT:
             return new Disconnect(header);
         case CommandMessage.PINGREQ:
             return new PingReq(header);
         case CommandMessage.PINGRESP:
             return new PingResp(header);
         case CommandMessage.PUBACK:
             return new PubAck(header, data);
         case CommandMessage.PUBCOMP:
             return new PubComp(header, data);
         case CommandMessage.PUBLISH:
             return new Publish(header, data);
         case CommandMessage.PUBREC:
             return new PubRec(header, data);
         case CommandMessage.PUBREL:
             return new PubRel(header, data);
         case CommandMessage.SUBACK:
             return new SubAck(header, data);
         case CommandMessage.SUBSCRIBE:
             return new Subscribe(header, data);
         case CommandMessage.UNSUBACK:
             return new UnSubAck(header, data);
         case CommandMessage.UNSUBSCRIBE:
             return new Unsubscribe(header, data);
         default:
             throw new InvalidOperationException("Unknown command message");
     }
 }
Example #14
0
 protected MqttCommand(FixedHeader header)
 {
     MessageId = MessageId.Any;
     Header = header;
 }