Ejemplo n.º 1
0
        public static FixedHeader Deserialize(byte[] data)
        {
            Validate
            .Begin()
            .IsNotNull(data)
            .IsGreaterThanOrEqualTo(data.Length, 2)
            .Check();

            var header = new FixedHeader();

            // ---- message type ----
            header.MessageType = (MessageType)(data[0] >> 4);
            // ---- duplicate ----
            header.DuplicateDelivery = (data[0] & DuplicateDeliveryBit) == DuplicateDeliveryBit;
            // ---- qos ----
            var qos = (QoS)((data[0] >> 1) & 3);

            header.QoS = qos;

            // ---- retain ----
            header.Retain = (data[0] & RetainBit) == RetainBit;

            // ---- remaining length ----
            header.RemainingLength = DecodeRemainingLength(data, 1);
            return(header);
        }
Ejemplo n.º 2
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);
        }
Ejemplo 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());
        }
Ejemplo n.º 4
0
        internal PingResponse(FixedHeader header, byte[] payload)
            : base(header.MessageType)
        {
            // NOTE: PingResponse has no variable header and no payload

            VariableHeader = null;
        }
Ejemplo n.º 5
0
        public Message(MessageType type, QoS qos, bool retain, bool duplicateDelivery)
        {
            FixedHeader = new FixedHeader();

            FixedHeader.MessageType       = type;
            FixedHeader.QoS               = qos;
            FixedHeader.Retain            = retain;
            FixedHeader.DuplicateDelivery = duplicateDelivery;
        }
Ejemplo n.º 6
0
        internal Message(FixedHeader header)
        {
            Validate
            .Begin()
            .IsNotNull(header)
            .Check();

            FixedHeader = header;
        }
Ejemplo n.º 7
0
 internal ConnectAck(FixedHeader header, byte[] payload)
     : base(header)
 {
     m_payload = payload;
 }
Ejemplo n.º 8
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);
            }
        }
Ejemplo n.º 9
0
        private void RxThreadProc()
        {
            try
            {
                if (IsConnected)
                {
#if !WindowsCE
                    m_stream.ReadTimeout = DefaultTimeout;
#endif
                }
            }
            catch (ObjectDisposedException)
            {
                IsConnected = false;
            }

            // start a ping timer on a period less than the above timeout (else we'll timeout and exit)
            m_pingTimer = new Timer(PingTimerProc, null, PingPeriod, PingPeriod);

            List <byte> header = new List <byte>(4);
            byte[]      buffer = null;
            while ((ConnectionState == ConnectionState.Connecting) || (ConnectionState == ConnectionState.Connected))
            {
                header.Clear();

                // the first byte gives us the type
                try
                {
                    var byte0 = m_stream.ReadByte();
                    if (byte0 == -1)
                    {
                        Thread.Sleep(500);
                        continue;
                    }

                    var messageType = (MessageType)(byte0 >> 4);
                    header.Add((byte)byte0);

                    byte lengthByte;
                    // now pull the "remaining length"
                    do
                    {
                        lengthByte = (byte)m_stream.ReadByte();
                        header.Add(lengthByte);
                    } while ((lengthByte & 0x80) != 0);

                    var length = FixedHeader.DecodeRemainingLength(header, 1);

                    buffer = null;
                    int read = 0;

                    if (length > 0)
                    {
                        // and pull the payload
                        buffer = new byte[length];
                        do
                        {
                            read += m_stream.Read(buffer, read, length - read);
                        } while (read < length);
                    }

                    // deserialize and dispatch
                    var response = DeserializeAndDispatchMessage(header.ToArray(), buffer);
                }
                catch (Exception ex)
                {
                    // happens during hang up, maybe on error
                    // needs testing
                    TracingDebug("!!! Exception in MQTTBroker.RxProc\r\n" + ex.Message);
                    //if (Debugger.IsAttached) Debugger.Break();
                    IsConnected = false;
                }
            }

            m_pingTimer.Dispose();
        }
Ejemplo n.º 10
0
        public Message(MessageType type)
        {
            FixedHeader = new FixedHeader();

            FixedHeader.MessageType = type;
        }