public void DisconnectBasicDecodeTestv5()
 {
     // Arrange
     byte[]     encodedCorrect = new byte[] { 0 };
     MokChannel mokChannel     = new MokChannel(encodedCorrect);
     // Act
     MqttMsgDisconnect disconnect = MqttMsgDisconnect.Parse(224, MqttProtocolVersion.Version_5, mokChannel);
     // Assert
 }
        public void DisconnectBasicDecodeErrorCodeTestv5()
        {
            // Arrange
            byte[]     encodedCorrect = new byte[] { 2, 143, 0 };
            MokChannel mokChannel     = new MokChannel(encodedCorrect);
            // Act
            MqttMsgDisconnect disconnect = MqttMsgDisconnect.Parse(224, MqttProtocolVersion.Version_5, mokChannel);

            // Assert
            Assert.Equal((byte)disconnect.ResonCode, (byte)MqttReasonCode.TopicFilterInvalid);
        }
        public void DisconnectAdvanceDecodeTestv5()
        {
            // Arrange
            byte[] encodedCorrect = new byte[] { 62, 143, 60, 17, 0, 0, 48, 57, 31, 0, 13, 73, 110, 118, 97, 108, 105, 100, 32, 116, 111, 112, 105, 99,
                                                 38, 0, 6, 115, 116, 97, 116, 117, 115, 0, 3, 49, 48, 49, 28, 0, 22, 110, 101, 119, 115, 101, 114, 118, 101,
                                                 114, 46, 115, 111, 109, 116, 104, 105, 110, 103, 46, 110, 101, 116 };
            MokChannel mokChannel = new MokChannel(encodedCorrect);
            // Act
            MqttMsgDisconnect disconnect = MqttMsgDisconnect.Parse(224, MqttProtocolVersion.Version_5, mokChannel);

            // Assert
            Assert.Equal((byte)disconnect.ResonCode, (byte)MqttReasonCode.TopicFilterInvalid);
            Assert.Equal(disconnect.SessionExpiryInterval, 12345);
            Assert.Equal(disconnect.Reason, "Invalid topic");
            Assert.Equal(disconnect.ServerReference, "newserver.somthing.net");
            var prop = new UserProperty("status", "101");

            Assert.Equal(((UserProperty)disconnect.UserProperties[0]).Name, prop.Name);
            Assert.Equal(((UserProperty)disconnect.UserProperties[0]).Value, prop.Value);
        }
Пример #4
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();
            }
            }
        }
Пример #5
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);
            }
        }
Пример #7
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);
            }
            }
        }