Ejemplo n.º 1
0
        /// <summary>
        /// Subscribe for message topics
        /// </summary>
        /// <param name="topics">List of topics to subscribe</param>
        /// <param name="qosLevels">QOS levels related to topics</param>
        /// <returns>Granted QoS Levels in SUBACK message from broker</returns>
        public byte[] Subscribe(string[] topics, byte[] qosLevels)
        {
            int  attempts     = 0;
            bool acknowledged = false;

            MqttMsgSubscribe subscribe =
                new MqttMsgSubscribe(topics, qosLevels);

            MqttMsgSuback suback = null;

            do
            {
                try
                {
                    // try subscribe
                    suback       = (MqttMsgSuback)this.SendReceive(subscribe.GetBytes());
                    acknowledged = true;
                }
                catch (MqttTimeoutException)
                {
                    // no SUBACK message received in time, retry with duplicate flag
                    attempts++;
                    subscribe.DupFlag = true;
                    // delay before retry
                    if (attempts < MQTT_ATTEMPTS_RETRY)
                    {
                        Thread.Sleep(MQTT_DELAY_RETRY);
                    }
                }
            } while ((attempts < MQTT_ATTEMPTS_RETRY) && !acknowledged);

            // return granted QoS Levels or null
            return(acknowledged ? suback.GrantedQoSLevels : null);
        }
Ejemplo n.º 2
0
        public static MqttMsgSuback BuildSubAck(ushort messageId, byte[] qosLevels)
        {
            MqttMsgSuback subAck = new MqttMsgSuback();

            subAck.MessageId        = messageId;
            subAck.GrantedQoSLevels = qosLevels;
            return(subAck);
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Wrapper method for raising subscribed topic event (SUBACK message)
 /// </summary>
 /// <param name="suback">SUBACK message received</param>
 private void OnMqttMsgSubscribed(MqttMsgSuback suback)
 {
     if (this.MqttMsgSubscribed != null)
     {
         this.MqttMsgSubscribed(this,
                                new MqttMsgSubscribedEventArgs(suback.MessageId, suback.GrantedQoSLevels));
     }
 }
Ejemplo n.º 4
0
        public static void Suback(MqttClientConnection clientConnection, ushort messageId, byte[] grantedQosLevels)
        {
            MqttMsgSuback suback = new MqttMsgSuback();

            suback.MessageId        = messageId;
            suback.GrantedQoSLevels = grantedQosLevels;

            Send(clientConnection, suback);
        }
Ejemplo n.º 5
0
        public void SubackBasicDecodingTestsv311()
        {
            // Arrange
            byte[]     encodedCorrect = new byte[] { 4, 0, 42, 1, 2 };
            MokChannel mokChannel     = new MokChannel(encodedCorrect);
            // Act
            MqttMsgSuback suback = MqttMsgSuback.Parse(144, MqttProtocolVersion.Version_3_1_1, mokChannel);

            // Assert
            Assert.Equal((ushort)42, suback.MessageId);
            Assert.Equal(suback.GrantedQoSLevels, new MqttQoSLevel[] { MqttQoSLevel.AtLeastOnce, MqttQoSLevel.ExactlyOnce });
        }
Ejemplo n.º 6
0
        public void SubackBasicDecodingTestsv5()
        {
            // Arrange
            byte[]     encodedCorrect = new byte[] { 5, 0, 42, 0, 1, 2 };
            MokChannel mokChannel     = new MokChannel(encodedCorrect);
            // Act
            MqttMsgSuback suback = MqttMsgSuback.Parse(144, MqttProtocolVersion.Version_5, mokChannel);

            // Assert
            Assert.Equal((ushort)42, suback.MessageId);
            Assert.Equal(suback.GrantedQoSLevels, new MqttQoSLevel[] { MqttQoSLevel.AtLeastOnce, MqttQoSLevel.ExactlyOnce });
            Assert.Equal(suback.ReasonCodes, new MqttReasonCode[] { MqttReasonCode.GrantedQoS1, MqttReasonCode.GrantedQoS2 });
            // And this should work as well as it's value comparison
            Assert.Equal(suback.GrantedQoSLevels, new MqttReasonCode[] { MqttReasonCode.GrantedQoS1, MqttReasonCode.GrantedQoS2 });
        }
Ejemplo n.º 7
0
        public void SubackAdvancedDecodingTestsv5()
        {
            // Arrange
            byte[] encodedCorrect = new byte[] { 93, 0, 42, 87, 31, 0, 49, 84, 104, 105, 115, 32, 105, 115, 32, 97, 32, 103, 111, 111, 100, 32, 114,
                                                 101, 97, 115, 111, 110, 58, 32, 121, 111, 117, 114, 32, 97, 114, 101, 32, 110, 111, 116, 32, 97, 117, 116,
                                                 104, 111, 114, 105, 122, 101, 100, 33, 33, 33, 38, 0, 4, 80, 114, 111, 112, 0, 26, 111, 110, 108, 121, 32,
                                                 111, 110, 101, 32, 116, 104, 105, 115, 32, 116, 105, 109, 101, 32, 102, 111, 114, 32, 102, 117, 110, 1,
                                                 2, 135 };
            MokChannel mokChannel = new MokChannel(encodedCorrect);
            // Act
            MqttMsgSuback suback = MqttMsgSuback.Parse(144, MqttProtocolVersion.Version_5, mokChannel);

            // Assert
            Assert.Equal((ushort)42, suback.MessageId);
            Assert.Equal(suback.GrantedQoSLevels, new MqttQoSLevel[] { MqttQoSLevel.AtLeastOnce, MqttQoSLevel.ExactlyOnce, (MqttQoSLevel)MqttReasonCode.NotAuthorized });
            Assert.Equal(suback.ReasonCodes, new MqttReasonCode[] { MqttReasonCode.GrantedQoS1, MqttReasonCode.GrantedQoS2, MqttReasonCode.NotAuthorized });
            // And this should work as well as it's value comparison
            Assert.Equal(suback.GrantedQoSLevels, new MqttReasonCode[] { MqttReasonCode.GrantedQoS1, MqttReasonCode.GrantedQoS2, MqttReasonCode.NotAuthorized });
            var prop = new UserProperty("Prop", "only one this time for fun");

            Assert.Equal(((UserProperty)suback.UserProperties[0]).Name, prop.Name);
            Assert.Equal(((UserProperty)suback.UserProperties[0]).Value, prop.Value);
        }
Ejemplo n.º 8
0
        public static MqttMsgBase DecodeControlPacket(byte[] data)
        {
            byte fixedHeaderFirstByte = (byte)(data[0] >> MqttMsgBase.MSG_TYPE_OFFSET);

            switch (fixedHeaderFirstByte)
            {
            case MqttMsgBase.MQTT_MSG_CONNECT_TYPE:
            {
                return(MqttMsgConnect.Parse(data));
            }

            case MqttMsgBase.MQTT_MSG_CONNACK_TYPE:
            {
                return(MqttMsgConnack.Parse(data));
            }

            case MqttMsgBase.MQTT_MSG_PUBLISH_TYPE:
            {
                return(MqttMsgPublish.Parse(data));
            }

            case MqttMsgBase.MQTT_MSG_PUBACK_TYPE:
            {
                return(MqttMsgPuback.Parse(data));
            }

            case MqttMsgBase.MQTT_MSG_PUBREC_TYPE:
            {
                return(MqttMsgPubrec.Parse(data));
            }

            case MqttMsgBase.MQTT_MSG_PUBREL_TYPE:
            {
                return(MqttMsgPubrel.Parse(data));
            }

            case MqttMsgBase.MQTT_MSG_PUBCOMP_TYPE:
            {
                return(MqttMsgPubcomp.Parse(data));
            }

            case MqttMsgBase.MQTT_MSG_SUBSCRIBE_TYPE:
            {
                return(MqttMsgSubscribe.Parse(data));
            }

            case MqttMsgBase.MQTT_MSG_SUBACK_TYPE:
            {
                return(MqttMsgSuback.Parse(data));
            }

            case MqttMsgBase.MQTT_MSG_UNSUBSCRIBE_TYPE:
            {
                return(MqttMsgUnsubscribe.Parse(data));
            }

            case MqttMsgBase.MQTT_MSG_UNSUBACK_TYPE:
            {
                return(MqttMsgUnsuback.Parse(data));
            }

            case MqttMsgBase.MQTT_MSG_PINGREQ_TYPE:
            {
                return(MqttMsgPingReq.Parse(data));
            }

            case MqttMsgBase.MQTT_MSG_PINGRESP_TYPE:
            {
                return(MqttMsgPingResp.Parse(data));
            }

            case MqttMsgBase.MQTT_MSG_DISCONNECT_TYPE:
            {
                CrestronLogger.WriteToLog("PACKETDECODER - Riconosciuto DISCONNNECT: ", 1);
                return(MqttMsgDisconnect.Parse(data));
            }

            default:
            {
                throw new FormatException();
            }
            }
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Thread for receiving messages from broker
        /// </summary>
        private void ReceiveThread()
        {
            int readBytes;

            byte[] fixedHeaderFirstByte = new byte[1];
            byte   msgType;

            while (this.isRunning)
            {
                try
                {
                    // read first byte (fixed header)
                    readBytes = this.socket.Receive(fixedHeaderFirstByte);

                    if (readBytes > 0)
                    {
                        // extract message type from received byte
                        msgType = (byte)((fixedHeaderFirstByte[0] & MqttMsgBase.MSG_TYPE_MASK) >> MqttMsgBase.MSG_TYPE_OFFSET);

                        switch (msgType)
                        {
                        // impossible, broker can't send CONNECT message
                        case MqttMsgBase.MQTT_MSG_CONNECT_TYPE:

                            throw new MqttClientException(MqttClientErrorCode.WrongBrokerMessage);

                        // CONNACK message received from broker
                        case MqttMsgBase.MQTT_MSG_CONNACK_TYPE:

                            this.msgReceived = MqttMsgConnack.Parse(fixedHeaderFirstByte[0], this.socket);
                            this.endReceiving.Set();
                            break;

                        // impossible, broker can't send PINGREQ message
                        case MqttMsgBase.MQTT_MSG_PINGREQ_TYPE:

                            throw new MqttClientException(MqttClientErrorCode.WrongBrokerMessage);

                        // CONNACK message received from broker
                        case MqttMsgBase.MQTT_MSG_PINGRESP_TYPE:

                            this.msgReceived = MqttMsgPingResp.Parse(fixedHeaderFirstByte[0], this.socket);
                            this.endReceiving.Set();
                            break;

                        // impossible, broker can't send SUBSCRIBE message
                        case MqttMsgBase.MQTT_MSG_SUBSCRIBE_TYPE:

                            throw new MqttClientException(MqttClientErrorCode.WrongBrokerMessage);

                        // SUBACK message received from broker
                        case MqttMsgBase.MQTT_MSG_SUBACK_TYPE:

                            this.msgReceived = MqttMsgSuback.Parse(fixedHeaderFirstByte[0], this.socket);
                            this.endReceiving.Set();

                            // raise subscribed topic event (SUBACK message received)
                            this.OnMqttMsgSubscribed((MqttMsgSuback)this.msgReceived);

                            break;

                        // PUBLISH message received from broker
                        case MqttMsgBase.MQTT_MSG_PUBLISH_TYPE:

                            MqttMsgPublish msgReceived = MqttMsgPublish.Parse(fixedHeaderFirstByte[0], this.socket);

                            // for QoS Level 1 and 2, client sends PUBACK message to broker
                            if ((this.msgReceived.QosLevel == MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE) ||
                                (this.msgReceived.QosLevel == MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE))
                            {
                                MqttMsgPuback puback = new MqttMsgPuback();
                                puback.MessageId = (msgReceived).MessageId;
                                this.Send(puback.GetBytes());
                            }

                            // raise PUBLISH message received event
                            this.OnMqttMsgPublishReceived(msgReceived);

                            break;

                        // PUBACK message received from broker
                        case MqttMsgBase.MQTT_MSG_PUBACK_TYPE:

                            this.msgReceived = MqttMsgPuback.Parse(fixedHeaderFirstByte[0], this.socket);
                            this.endReceiving.Set();

                            // raise published message event
                            // (PUBACK received for QoS Level 1)
                            this.OnMqttMsgPublished(((MqttMsgPuback)this.msgReceived).MessageId);

                            break;

                        // PUBREC message received from broker
                        case MqttMsgBase.MQTT_MSG_PUBREC_TYPE:

                            this.msgReceived = MqttMsgPubrec.Parse(fixedHeaderFirstByte[0], this.socket);
                            this.endReceiving.Set();
                            break;

                        // impossible, broker can't send PUBREL message
                        case MqttMsgBase.MQTT_MSG_PUBREL_TYPE:

                            throw new MqttClientException(MqttClientErrorCode.WrongBrokerMessage);

                        // PUBCOMP message received from broker
                        case MqttMsgBase.MQTT_MSG_PUBCOMP_TYPE:

                            this.msgReceived = MqttMsgPubcomp.Parse(fixedHeaderFirstByte[0], this.socket);
                            this.endReceiving.Set();

                            // raise published message event
                            // (PUBCOMP received for QoS Level 2)
                            this.OnMqttMsgPublished(((MqttMsgPuback)this.msgReceived).MessageId);

                            break;

                        // impossible, broker can't send UNSUBSCRIBE message
                        case MqttMsgBase.MQTT_MSG_UNSUBSCRIBE_TYPE:

                            throw new MqttClientException(MqttClientErrorCode.WrongBrokerMessage);

                        // UNSUBACK message received from broker
                        case MqttMsgBase.MQTT_MSG_UNSUBACK_TYPE:

                            this.msgReceived = MqttMsgUnsuback.Parse(fixedHeaderFirstByte[0], this.socket);
                            this.endReceiving.Set();

                            // raise unsubscribed topic event
                            this.OnMqttMsgUnsubscribed(((MqttMsgUnsuback)this.msgReceived).MessageId);

                            break;

                        default:

                            throw new MqttClientException(MqttClientErrorCode.WrongBrokerMessage);
                        }

                        this.exReceiving = null;
                    }
                }
                catch (Exception)
                {
                    this.exReceiving = new MqttCommunicationException();
                }
            }
        }
Ejemplo n.º 10
0
        public MqttMsgBase DecodeControlPacket(byte[] data)
        {
            byte fixedHeaderFirstByte = (byte)(data[0] >> MSG_TYPE_OFFSET);

            switch (fixedHeaderFirstByte)
            {
            case MqttMsgBase.MQTT_MSG_CONNECT_TYPE:
            {
                return(MqttMsgConnect.Parse(data));
            }

            case MqttMsgBase.MQTT_MSG_CONNACK_TYPE:
            {
                return(MqttMsgConnack.Parse(data));
            }

            case MqttMsgBase.MQTT_MSG_PUBLISH_TYPE:
            {
                return(MqttMsgPublish.Parse(data));
            }

            case MqttMsgBase.MQTT_MSG_PUBACK_TYPE:
            {
                return(MqttMsgPuback.Parse(data));
            }

            case MqttMsgBase.MQTT_MSG_PUBREC_TYPE:
            {
                return(MqttMsgPubrec.Parse(data));
            }

            case MqttMsgBase.MQTT_MSG_PUBREL_TYPE:
            {
                return(MqttMsgPubrel.Parse(data));
            }

            case MqttMsgBase.MQTT_MSG_PUBCOMP_TYPE:
            {
                return(MqttMsgPubcomp.Parse(data));
            }

            case MqttMsgBase.MQTT_MSG_SUBSCRIBE_TYPE:
            {
                return(MqttMsgSubscribe.Parse(data));
            }

            case MqttMsgBase.MQTT_MSG_SUBACK_TYPE:
            {
                return(MqttMsgSuback.Parse(data));
            }

            case MqttMsgBase.MQTT_MSG_UNSUBSCRIBE_TYPE:
            {
                return(MqttMsgUnsubscribe.Parse(data));
            }

            case MqttMsgBase.MQTT_MSG_UNSUBACK_TYPE:
            {
                return(MqttMsgUnsuback.Parse(data));
            }

            case MqttMsgBase.MQTT_MSG_PINGREQ_TYPE:
            {
                return(MqttMsgPingReq.Parse(data));
            }

            case MqttMsgBase.MQTT_MSG_PINGRESP_TYPE:
            {
                return(MqttMsgPingResp.Parse(data));
            }

            case MqttMsgBase.MQTT_MSG_DISCONNECT_TYPE:
            {
                return(MqttMsgDisconnect.Parse(data));
            }

            default:
            {
                throw new FormatException(" First byte shifted : " + fixedHeaderFirstByte);
            }
            }
        }
 private void OnMqttMsgSubscribed(MqttConnection connection, MqttMsgSuback suback)
 {
 }
Ejemplo n.º 12
0
 private void HandleSUBACKype(MqttMsgSuback mqttMsgSuback)
 {
     CrestronLogger.WriteToLog("MQTTCLIENT - HANDLESUBACK -", 6);
 }
Ejemplo n.º 13
0
        public MqttMsgBase DecodeControlPacket(byte[] data)
        {
            #if PACKET_DEBUG
            CrestronLogger.WriteToLog("MQTTCLIENT - RECEIVE - " + BitConverter.ToString(data), 2);
            #endif

            byte fixedHeaderFirstByte = (byte)(data[0] >> MSG_TYPE_OFFSET);

            switch (fixedHeaderFirstByte)
            {
            case MqttMsgBase.MQTT_MSG_CONNECT_TYPE:
            {
                return(MqttMsgConnect.Parse(data));
            }

            case MqttMsgBase.MQTT_MSG_CONNACK_TYPE:
            {
                return(MqttMsgConnack.Parse(data));
            }

            case MqttMsgBase.MQTT_MSG_PUBLISH_TYPE:
            {
                return(MqttMsgPublish.Parse(data));
            }

            case MqttMsgBase.MQTT_MSG_PUBACK_TYPE:
            {
                return(MqttMsgPuback.Parse(data));
            }

            case MqttMsgBase.MQTT_MSG_PUBREC_TYPE:
            {
                return(MqttMsgPubrec.Parse(data));
            }

            case MqttMsgBase.MQTT_MSG_PUBREL_TYPE:
            {
                return(MqttMsgPubrel.Parse(data));
            }

            case MqttMsgBase.MQTT_MSG_PUBCOMP_TYPE:
            {
                return(MqttMsgPubcomp.Parse(data));
            }

            case MqttMsgBase.MQTT_MSG_SUBSCRIBE_TYPE:
            {
                return(MqttMsgSubscribe.Parse(data));
            }

            case MqttMsgBase.MQTT_MSG_SUBACK_TYPE:
            {
                return(MqttMsgSuback.Parse(data));
            }

            case MqttMsgBase.MQTT_MSG_UNSUBSCRIBE_TYPE:
            {
                return(MqttMsgUnsubscribe.Parse(data));
            }

            case MqttMsgBase.MQTT_MSG_UNSUBACK_TYPE:
            {
                return(MqttMsgUnsuback.Parse(data));
            }

            case MqttMsgBase.MQTT_MSG_PINGREQ_TYPE:
            {
                return(MqttMsgPingReq.Parse(data));
            }

            case MqttMsgBase.MQTT_MSG_PINGRESP_TYPE:
            {
                return(MqttMsgPingResp.Parse(data));
            }

            case MqttMsgBase.MQTT_MSG_DISCONNECT_TYPE:
            {
                return(MqttMsgDisconnect.Parse(data));
            }

            default:
            {
                throw new FormatException(" First byte shifted : " + fixedHeaderFirstByte);
            }
            }
        }
 private static void OnMqttMsgSubscribed(MqttClientConnection clientConnection, MqttMsgSuback suback)
 {
 }