Exemplo n.º 1
0
        private void HandleCONNECTType(uint clientIndex, MqttMsgConnect packet, bool isWebSocketClient)
        {
            try
            {
                byte returnCode = ConnectVerify(packet);
                SendConnack(clientIndex, packet.CleanSession, returnCode, isWebSocketClient);
                if (returnCode.Equals(MqttMsgConnack.CONN_ACCEPTED))
                {
                    if (packet.CleanSession)
                    {
                        MqttClient c = new MqttClient(clientIndex, packet, isWebSocketClient);
                        sessionManager.Destroy(packet.ClientId);
                        sessionManager.Create(packet.ClientId, c);
                        clients.Add(c);
                        if (!c.IsWebSocketClient)
                        {
                            tcpServer.Receive(clientIndex);
                        }
                        else
                        {
                            wsServer.Receive(clientIndex);
                        }
                    }
                    else
                    {
                        throw new NotImplementedException("Clean session 0 is not implemented");
                    }
                }
                else
                {
                    DisconnectClient(clientIndex, false, isWebSocketClient);
                    throw new MqttConnectionException("Connection rejected" + returnCode, new ArgumentException());
                }
            }
            catch (Exception e)
            {
                CrestronLogger.WriteToLog("\n MQTTSERVER - CONNECT Error Message : " + e.Message, 7);

                if (!isWebSocketClient)
                {
                    tcpServer.RejectConnection(clientIndex);
                }
                else
                {
                    wsServer.RejectConnection(clientIndex);
                }
            }
        }