Exemplo n.º 1
0
        private void HandlePINGREQType(uint clientIndex, MqttMsgPingReq request, bool isWebSocketClient)
        {
            MqttMsgPingResp response = MsgBuilder.BuildPingResp();

            byte[] resp = response.GetBytes();
            Send(clientIndex, resp, resp.Length, isWebSocketClient);
        }
Exemplo n.º 2
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();
            }
            }
        }
Exemplo n.º 3
0
 private void HandlePINGRESPType(uint clientIndex, MqttMsgPingResp mqttMsgSubscribe, bool isWebSocketClient)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 4
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();
                }
            }
        }
Exemplo n.º 5
0
        public static MqttMsgPingResp BuildPingResp()
        {
            MqttMsgPingResp request = new MqttMsgPingResp();

            return(request);
        }
Exemplo n.º 6
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);
            }
            }
        }
Exemplo n.º 7
0
        /* private void StartKeepAlive()
         * {
         *   Send(MsgBuilder.BuildPingReq());
         *   keepAliveTimer = new CTimer(KeepAliveTimerCallback, false, long.Parse(KeepAlivePeriod.ToString()));
         * }*/


        /*private void KeepAliveTimerCallback(object userSpecific)
         * {
         *  bool hasPingResponseBeenReceived = (bool)userSpecific;
         *  if (hasPingResponseBeenReceived)
         *  {
         *
         *  }
         *  else
         *  {
         *      OnErrorOccured("The server didn't respond on time ,disconnecting");
         *      Disconnect(false);
         *  }
         * }
         *
         * private void TimoutTimerCallBack(object userSpecific)
         * {
         *
         * }*/

        private void HandlePINGRESPType(MqttMsgPingResp mqttMsgPingResp)
        {
            /* keepAliveTimer.Stop();
             * keepAliveTimer.Dispose();
             * keepAliveTimer = new CTimer(KeepAliveTimerCallback, true, long.Parse(KeepAlivePeriod.ToString()));*/
        }
Exemplo n.º 8
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);
            }
            }
        }
Exemplo n.º 9
0
        public static void PingResp(MqttClientConnection clientConnection)
        {
            MqttMsgPingResp pingresp = new MqttMsgPingResp();

            Send(clientConnection, pingresp);
        }
Exemplo n.º 10
0
 private void HandlePINGRESPType(MqttMsgPingResp mqttMsgPingResp)
 {
 }