public void UnbscribeBasicDecodeTestv5()
        {
            // Arrange
            byte[]     encodedCorrect = new byte[] { 19, 0, 42, 0, 0, 6, 116, 112, 111, 105, 99, 49, 0, 6, 116, 111, 112, 105, 99, 50 };
            MokChannel mokChannel     = new(encodedCorrect);
            // Act
            MqttMsgUnsubscribe unsubscribe = MqttMsgUnsubscribe.Parse(162, MqttProtocolVersion.Version_5, mokChannel);

            // Assert
            Assert.Equal((ushort)42, unsubscribe.MessageId);
            Assert.Equal(unsubscribe.Topics.Length, 2);
            Assert.Equal(unsubscribe.Topics, new string[] { "tpoic1", "topic2" });
        }
        public void UnbscribeAdvanceDecodeTestv5()
        {
            // Arrange
            byte[] encodedCorrect = new byte[] { 54, 0, 42, 35, 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, 0, 6, 116, 112, 111, 105, 99, 49,
                                                 0, 6, 116, 111, 112, 105, 99, 50 };
            MokChannel mokChannel = new(encodedCorrect);
            // Act
            MqttMsgUnsubscribe unsubscribe = MqttMsgUnsubscribe.Parse(162, MqttProtocolVersion.Version_5, mokChannel);

            // Assert
            Assert.Equal((ushort)42, unsubscribe.MessageId);
            Assert.Equal(unsubscribe.Topics.Length, 2);
            Assert.Equal(unsubscribe.Topics, new string[] { "tpoic1", "topic2" });
            var prop = new UserProperty("Prop", "only one this time for fun");

            Assert.Equal(((UserProperty)unsubscribe.UserProperties[0]).Name, prop.Name);
            Assert.Equal(((UserProperty)unsubscribe.UserProperties[0]).Value, prop.Value);
        }
Esempio n. 3
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();
            }
            }
        }
Esempio n. 4
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);
            }
            }
        }
        public void ProcessReceivedMessage(MqttRawMessage rawMessage)
        {
            if (!rawMessage.Connection.IsRunning)
            {
                return;
            }

            // update last message received ticks
            rawMessage.Connection.LastCommunicationTime = Environment.TickCount;

            // extract message type from received byte
            var msgType         = (byte)((rawMessage.MessageType & MqttMsgBase.MSG_TYPE_MASK) >> MqttMsgBase.MSG_TYPE_OFFSET);
            var protocolVersion = (byte)rawMessage.Connection.ProtocolVersion;

            switch (msgType)
            {
            case MqttMsgBase.MQTT_MSG_CONNECT_TYPE:
                var connect = MqttMsgConnect.Parse(
                    rawMessage.MessageType,
                    protocolVersion,
                    rawMessage.PayloadBuffer);
                rawMessage.Connection.EnqueueInternalEvent(new InternalEvent(connect));
                break;

            case MqttMsgBase.MQTT_MSG_PINGREQ_TYPE:
                var pingReqest = MqttMsgPingReq.Parse(rawMessage.MessageType, protocolVersion);
                outgoingMessageHandler.PingResp(rawMessage.Connection);
                break;

            case MqttMsgBase.MQTT_MSG_SUBSCRIBE_TYPE:
                var subscribe = MqttMsgSubscribe.Parse(
                    rawMessage.MessageType,
                    protocolVersion,
                    rawMessage.PayloadBuffer,
                    rawMessage.PayloadLength);
                rawMessage.Connection.EnqueueInternalEvent(new InternalEvent(subscribe));
                break;

            case MqttMsgBase.MQTT_MSG_PUBLISH_TYPE:
                var publish = MqttMsgPublish.Parse(
                    rawMessage.MessageType,
                    protocolVersion,
                    rawMessage.PayloadBuffer,
                    rawMessage.PayloadLength);
                EnqueueInflight(rawMessage.Connection, publish, MqttMsgFlow.ToAcknowledge);
                break;

            case MqttMsgBase.MQTT_MSG_PUBACK_TYPE:
                // enqueue PUBACK message received (for QoS Level 1) into the internal queue
                var puback = MqttMsgPuback.Parse(
                    rawMessage.MessageType,
                    protocolVersion,
                    rawMessage.PayloadBuffer);
                EnqueueInternal(rawMessage.Connection, puback);
                break;

            case MqttMsgBase.MQTT_MSG_PUBREC_TYPE:
                // enqueue PUBREC message received (for QoS Level 2) into the internal queue
                var pubrec = MqttMsgPubrec.Parse(
                    rawMessage.MessageType,
                    protocolVersion,
                    rawMessage.PayloadBuffer);
                EnqueueInternal(rawMessage.Connection, pubrec);
                break;

            case MqttMsgBase.MQTT_MSG_PUBREL_TYPE:
                // enqueue PUBREL message received (for QoS Level 2) into the internal queue
                var pubrel = MqttMsgPubrel.Parse(
                    rawMessage.MessageType,
                    protocolVersion,
                    rawMessage.PayloadBuffer);
                EnqueueInternal(rawMessage.Connection, pubrel);
                break;

            case MqttMsgBase.MQTT_MSG_PUBCOMP_TYPE:
                // enqueue PUBCOMP message received (for QoS Level 2) into the internal queue
                var pubcomp = MqttMsgPubcomp.Parse(
                    rawMessage.MessageType,
                    protocolVersion,
                    rawMessage.PayloadBuffer);
                EnqueueInternal(rawMessage.Connection, pubcomp);
                break;

            case MqttMsgBase.MQTT_MSG_UNSUBSCRIBE_TYPE:
                var unsubscribe = MqttMsgUnsubscribe.Parse(
                    rawMessage.MessageType,
                    protocolVersion,
                    rawMessage.PayloadBuffer,
                    rawMessage.PayloadLength);
                rawMessage.Connection.EnqueueInternalEvent(new InternalEvent(unsubscribe));
                break;

            case MqttMsgBase.MQTT_MSG_DISCONNECT_TYPE:
                var disconnect = MqttMsgDisconnect.Parse(rawMessage.MessageType, protocolVersion);
                rawMessage.Connection.EnqueueInternalEvent(new InternalEvent(disconnect));
                break;

            case MqttMsgBase.MQTT_MSG_CONNACK_TYPE:
            case MqttMsgBase.MQTT_MSG_PINGRESP_TYPE:
            case MqttMsgBase.MQTT_MSG_SUBACK_TYPE:
            case MqttMsgBase.MQTT_MSG_UNSUBACK_TYPE:
                throw new MqttClientException(MqttClientErrorCode.WrongBrokerMessage);

            default:
                throw new MqttClientException(MqttClientErrorCode.WrongBrokerMessage);
            }
        }
Esempio n. 6
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);
            }
            }
        }