Beispiel #1
0
 internal void ClientReceiveDataAsync()
 {
     if (EnableSSL)
     {
         SSLClient.ReceiveDataAsync(ClientReceiveSSLCallback);
     }
     else
     {
         NoSSLClient.ReceiveDataAsync(ClientReceiveNoSSLCallback);
     }
 }
 public void clientConnectCallback(SecureTCPClient client)
 {
     CrestronConsole.PrintLine("clientConnectCallback: ClientStatus is: " + client.ClientStatus);
     if (client.ClientStatus == SocketStatus.SOCKET_STATUS_CONNECTED)
     {
         CrestronConsole.PrintLine("clientConnectCallback: Connected to " + client.AddressClientConnectedTo + " on port " + client.PortNumber);
         // Once connected, begin waiting for packets from the server.
         // This call to ReceiveDataAsync is necessary to receive the FIN packet from the server in the event
         // that the TLS handshake fails and the connection cannot be made. If you do not call ReceiveDataAsync here,
         // the client will remain "connected" from its perspective, though no connection has been made.
         client.ReceiveDataAsync(clientReceiveCallback);
     }
     else
     {
         PrintAndLog("clientConnectCallback: No connection could be made with the server.");
     }
 }
 public void clientReceiveCallback(SecureTCPClient client, int bytes_recvd)
 {
     if (bytes_recvd <= 0) // 0 or negative byte count indicates the connection has been closed
     {
         PrintAndLog("clientReceiveCallback: Could not receive message- connection closed");
     }
     else
     {
         try
         {
             CrestronConsole.PrintLine("Received " + bytes_recvd + " bytes from " + client.AddressClientConnectedTo + " port " + client.PortNumber);
             string received = ASCIIEncoding.ASCII.GetString(client.IncomingDataBuffer, 0, client.IncomingDataBuffer.Length);
             CrestronConsole.PrintLine("Server says: " + received);
         }
         catch (Exception e)
         {
             PrintAndLog("Exception in clientReceiveCallback: " + e.Message);
         }
         // Wait for another message
         client.ReceiveDataAsync(clientReceiveCallback);
     }
 }
Beispiel #4
0
        /// <summary>
        /// Receive callback
        /// </summary>
        /// <param name="client"></param>
        /// <param name="numBytes"></param>
        void Receive(SecureTCPClient client, int numBytes)
        {
            if (numBytes > 0)
            {
                string str     = string.Empty;
                var    handler = TextReceivedQueueInvoke;
                try
                {
                    var bytes = client.IncomingDataBuffer.Take(numBytes).ToArray();
                    str = Encoding.GetEncoding(28591).GetString(bytes, 0, bytes.Length);
                    Debug.Console(2, this, "Client Received:\r--------\r{0}\r--------", str);
                    if (!string.IsNullOrEmpty(checkHeartbeat(str)))
                    {
                        if (SharedKeyRequired && str == "SharedKey:")
                        {
                            Debug.Console(2, this, "Server asking for shared key, sending");
                            SendText(SharedKey + "\n");
                        }
                        else if (SharedKeyRequired && str == "Shared Key Match")
                        {
                            StopWaitForSharedKeyTimer();


                            Debug.Console(2, this, "Shared key confirmed. Ready for communication");
                            OnClientReadyForcommunications(true); // Successful key exchange
                        }
                        else
                        {
                            //var bytesHandler = BytesReceived;
                            //if (bytesHandler != null)
                            //    bytesHandler(this, new GenericCommMethodReceiveBytesArgs(bytes));
                            var textHandler = TextReceived;
                            if (textHandler != null)
                            {
                                textHandler(this, new GenericTcpServerCommMethodReceiveTextArgs(str));
                            }
                            if (handler != null)
                            {
                                MessageQueue.TryToEnqueue(new GenericTcpServerCommMethodReceiveTextArgs(str));
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Debug.Console(1, this, "Error receiving data: {1}. Error: {0}", ex.Message, str);
                }
                if (client.ClientStatus == SocketStatus.SOCKET_STATUS_CONNECTED)
                {
                    client.ReceiveDataAsync(Receive);
                }

                //Check to see if there is a subscription to the TextReceivedQueueInvoke event. If there is start the dequeue thread.
                if (handler != null)
                {
                    var gotLock = DequeueLock.TryEnter();
                    if (gotLock)
                    {
                        CrestronInvoke.BeginInvoke((o) => DequeueEvent());
                    }
                }
            }
            else //JAG added this as I believe the error return is 0 bytes like the server. See help when hover on ReceiveAsync
            {
                client.DisconnectFromServer();
            }
        }
 private void HandleCONNACKType(MqttMsgConnack mqttMsgConnack)
 {
     SubscribeToTopics();
     //StartKeepAlive();
     tcpClient.ReceiveDataAsync(ReceiveCallback);
 }