Exemplo n.º 1
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);
        }
Exemplo n.º 2
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()
            //    .IsNotNull(clientIdentifier)
            //    .IsGreaterThanOrEqualTo(clientIdentifier.Length, 1)
            //    .IsLessThanOrEqualTo(clientIdentifier.Length, 23)
            //    .Check();
            Validate
            .Begin()
            .IsNotNull(clientIdentifier)
            .IsGreaterThanOrEqualTo(clientIdentifier.Length, 1)
            .IsLessThanOrEqualTo(clientIdentifier.Length, 64)
            .Check();
            ClientIdentifier = clientIdentifier;
        }
Exemplo n.º 3
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());
        }
Exemplo n.º 4
0
        private Message DeserializeAndDispatchMessage(byte[] headerdata, byte[] payload)
        {
            var header = FixedHeader.Deserialize(headerdata);

            TracingDebug("Received: " + header.MessageType.ToString());
            switch (header.MessageType)
            {
            case MessageType.ConnectAck:
                var connect = new ConnectAck(header, payload);
                IsConnected = true;
                autoResetConnEvent.Set();
                return(connect);

            case MessageType.PingResponse:
                return(new PingResponse(header, payload));

            case MessageType.SubscribeAck:
                break;

            case MessageType.UnsubscribeAck:
                break;

            case MessageType.Publish:
                var pub     = new Publish(header, payload);
                var handler = MessageReceived;
                TracingDebug("Topic=" + pub.Topic + ",content=" + System.Text.Encoding.UTF8.GetString(pub.Payload));
                if (handler != null)
                {
                    //handler(pub.Topic, pub.QoS, pub.Payload);
                    handler(pub.Topic, System.Text.Encoding.UTF8.GetString(pub.Payload));
                }

                if (header.QoS == QoS.AcknowledgeDelivery)
                {
                    var ackMsg   = new PublishAck();
                    var m_header = new VariableHeader <MessageIDHeaderData>();
                    m_header.HeaderData.MessageID = (ushort)(pub.m_header.HeaderData.MessageID);
                    ackMsg.VariableHeader         = m_header;
                    //ackMsg.MessageIdentifier = pubMsg.MessageIdentifier;
                    Send(ackMsg);
                    TracingDebug("Send PublishAck: MessageID=" + m_header.HeaderData.MessageID);
                }
                break;

            case MessageType.PublishAck:
                // TODO: handle this
                break;

            case MessageType.Connect:
                // server connecting to us
                throw new NotSupportedException();

            default:
                throw new NotSupportedException();
            }

            return(null);
        }
Exemplo n.º 5
0
        public Subscribe(Subscription[] subscriptions, ushort messageID)
            : base(MessageType.Subscribe, QoS.AcknowledgeDelivery, false, false)
        {
            Validate
            .Begin()
            .IsNotNull(subscriptions)

            .IsGreaterThanOrEqualTo(subscriptions.Length, 1)
            .Check();

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

            m_subscriptions = subscriptions;
        }
Exemplo n.º 6
0
        public Unsubscribe(string[] topics, ushort messageID)
            : base(MessageType.Unsubscribe, QoS.AcknowledgeDelivery, false, false)

        {
            Validate
            .Begin()
            .IsNotNull(topics)

            .IsGreaterThanOrEqualTo(topics.Length, 1)
            .Check();

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

            m_topics = topics;
        }
Exemplo n.º 7
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)
            .IsNotNull(messageData)
            .Check();

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

            m_data = messageData;
        }