예제 #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);
        }
예제 #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;
            }
            //delete by 赵雪峰 20160908
            //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;
        }
예제 #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());
        }
예제 #4
0
        public Subscribe(Subscription[] subscriptions, ushort messageID)
            : base(MessageType.Subscribe, QoS.AcknowledgeDelivery, false, false)
        {
            Validate
            .Begin()
            .IsNotNull(subscriptions)
            //.IsGreaterThanOrEqualTo(1, subscriptions.Length) modify by 赵雪峰 20160910
            .IsGreaterThanOrEqualTo(subscriptions.Length, 1)
            .Check();

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

            m_subscriptions = subscriptions;
        }
예제 #5
0
        public Unsubscribe(string[] topics, ushort messageID)
            : base(MessageType.Unsubscribe, QoS.AcknowledgeDelivery, false, false)
            //: base(MessageType.Subscribe, QoS.AcknowledgeDelivery, false, false)modify by 赵雪峰 20160910
        {
            Validate
            .Begin()
            .IsNotNull(topics)
            //.IsGreaterThanOrEqualTo(1, topics.Length) modify by 赵雪峰 20160910
            .IsGreaterThanOrEqualTo(topics.Length, 1)
            .Check();

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

            m_topics = topics;
        }
예제 #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)
            .IsNotNull(messageData)
            .Check();

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

            m_data = messageData;
        }
예제 #7
0
        private Message DeserializeAndDispatchMessage(byte[] headerdata, byte[] payload)
        {
            try
            {
                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);
                    if (handler != null)
                    {
                        //handler(pub.Topic, pub.QoS, pub.Payload);
                        handler(pub.Topic, 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);
            }
            catch (Exception e)
            {
                TracingDebug("DeserializeAndDispatchMessage:" + e.Message);
                return(null);
            }
        }