Esempio n. 1
0
        public Connect(string userName, string password, string clientIdentifier)
            : base(MessageType.Connect)
        {
            m_header       = new VariableHeader <ConnectHeaderData>();
            VariableHeader = m_header;

            if (!userName.IsNullOrEmpty())
            {
                UserName = userName;
                m_header.HeaderData.HasUserName = true;
            }

            if (!password.IsNullOrEmpty())
            {
                Password = password;
                m_header.HeaderData.HasPassword = true;
            }

            Validate
            .Begin()
            .ParameterIsNotNull(clientIdentifier, "clientIdentifier")
            .IsGreaterThanOrEqualTo(clientIdentifier.Length, 1)
            .IsLessThanOrEqualTo(clientIdentifier.Length, 23)
            .Check();

            ClientIdentifier = clientIdentifier;
        }
Esempio n. 2
0
        public byte[] Serialize()
        {
            var data = new List <byte>();

            var payload = this.Payload;
            var length  = payload == null ? 0 : payload.Length;

            byte[] variableHeader = null;
            if (VariableHeader != null)
            {
                variableHeader = VariableHeader.Serialize();
                length        += variableHeader.Length;
            }

            FixedHeader.RemainingLength = length;

            data.AddRange(FixedHeader.Serialize());
            if (variableHeader != null)
            {
                data.AddRange(variableHeader);
            }

            if (payload != null)
            {
                data.AddRange(payload);
            }

            return(data.ToArray());
        }
Esempio n. 3
0
        internal Publish(FixedHeader header, byte[] payload)
            : base(header)
        {
            var payloadLength = payload.Length;

            m_header       = new VariableHeader <PublishHeaderData>();
            VariableHeader = m_header;

            // get topic length
            var topicLength = System.Net.IPAddress.NetworkToHostOrder(BitConverter.ToInt16(payload, 0));

            payloadLength -= 2;

            // get topic name
            m_header.HeaderData.TopicName = Encoding.UTF8.GetString(payload, 2, topicLength);
            payloadLength -= topicLength;

            if (header.QoS > 0)
            {
                // pull message ID
                m_header.HeaderData.MessageID = (ushort)System.Net.IPAddress.NetworkToHostOrder(BitConverter.ToInt16(payload, payload.Length - payloadLength));
                payloadLength -= 2;
            }

            m_data = new byte[payloadLength];
            Buffer.BlockCopy(payload, payload.Length - payloadLength, m_data, 0, m_data.Length);
        }
Esempio n. 4
0
        public Unsubscribe(string[] topics, ushort messageID)
            : base(MessageType.Subscribe, QoS.AcknowledgeDelivery, false, false)
        {
            Validate
            .Begin()
            .ParameterIsNotNull(topics, "topics")
            .IsGreaterThanOrEqualTo(1, topics.Length)
            .Check();

            m_header = new VariableHeader <MessageIDHeaderData>();
            m_header.HeaderData.MessageID = messageID;
            VariableHeader = m_header;

            m_topics = topics;
        }
Esempio n. 5
0
        public Subscribe(Subscription[] subscriptions, ushort messageID)
            : base(MessageType.Subscribe, QoS.AcknowledgeDelivery, false, false)
        {
            Validate
            .Begin()
            .ParameterIsNotNull(subscriptions, "subscriptions")
            .IsGreaterThanOrEqualTo(1, subscriptions.Length)
            .Check();

            m_header = new VariableHeader <MessageIDHeaderData>();
            m_header.HeaderData.MessageID = messageID;
            VariableHeader = m_header;

            m_subscriptions = subscriptions;
        }
Esempio n. 6
0
        public Publish(string topicName, byte[] messageData, ushort messageID, QoS qos, bool retain)
            : base(MessageType.Publish, qos, retain, false)
        {
            m_header       = new VariableHeader <PublishHeaderData>();
            VariableHeader = m_header;

            Validate
            .Begin()
            .IsNotNullOrEmpty(topicName)
            .ParameterIsNotNull(messageData, "messageData")
            .Check();

            m_header.HeaderData.TopicName = topicName;
            if (messageID != 0)
            {
                m_header.HeaderData.MessageID = messageID;
            }

            m_data = messageData;
        }