public void Stop() { if (tcpClient.ClientStatus == SocketStatus.SOCKET_STATUS_CONNECTED) { Send(MsgBuilder.BuildDisconnect()); tcpClient.DisconnectFromServer(); } //tcpClient.Dispose(); }
internal void ClientDisconnect() { if (EnableSSL) { SSLClient.DisconnectFromServer(); } else { NoSSLClient.DisconnectFromServer(); } }
/// <summary> /// /// </summary> public void Disconnect() { Debug.Console(2, "Disconnect Called"); DisconnectCalledByUser = true; if (IsConnected) { Client.DisconnectFromServer(); } if (RetryTimer != null) { RetryTimer.Stop(); RetryTimer = null; } Cleanup(); }
// This will end the client's TCP connection with the server by sending a FIN message, or // notify you that the client socket is already disconnected public void Disconnect(string args) { try { if (client != null) { SocketErrorCodes err = client.DisconnectFromServer(); PrintAndLog("Disconnect with error code: " + err); client.Dispose(); client = null; } else { CrestronConsole.ConsoleCommandResponse("Client is already disconnected."); } } catch (Exception e) { PrintAndLog("Error in Disconnect: " + e.Message); } }
/// <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(); } }
/// <summary> /// ControlSystem Constructor. Starting point for the SIMPL#Pro program. /// Use the constructor to: /// * Initialize the maximum number of threads (max = 400) /// * Register devices /// * Register event handlers /// * Add Console Commands /// /// Please be aware that the constructor needs to exit quickly; if it doesn't /// exit in time, the SIMPL#Pro program will exit. /// /// You cannot send / receive data in the constructor /// </summary> public ControlSystem() : base() { try { Thread.MaxNumberOfUserThreads = 20; //Subscribe to the controller events (System, Program, and Ethernet) CrestronEnvironment.SystemEventHandler += new SystemEventHandler(_ControllerSystemEventHandler); CrestronEnvironment.ProgramStatusEventHandler += new ProgramStatusEventHandler(_ControllerProgramEventHandler); CrestronEnvironment.EthernetEventHandler += new EthernetEventHandler(_ControllerEthernetEventHandler); UcEngine ucEngine = new UcEngine(0x03, this); //WebSocketClient webSocketclient = new WebSocketClient //{ // KeepAlive = false, // VerifyServerCertificate = false, // Port = 443, // //Host = "C4-54B203910FDC" // Host = "10.25.94.48" // //SSL = true //}; //WebSocketClientAsyncConnectCallback webSocketClientAsyncConnectCallback = del; //var err = webSocketclient.ConnectEx(); //ErrorLog.Info("URL: " +(int) err); //string send = "reboot"; //byte[] bytes = Encoding.ASCII.GetBytes(send); //System.Security.Cryptography.X509Certificates.X509Certificate x509Certificate = new X509Certificate(); //Crestron.SimplSharp.Cryptography.X509Certificates.X509Certificate x509Certificate1 = new Crestron.SimplSharp.Cryptography.X509Certificates.X509Certificate(bytes); //webSocketclient.SetClientCertificate(x509Certificate1); //webSocketclient.Connect(); ////webSocketClient.Host = "10.25.94.48"; //ErrorLog.Info("Client Connected: " + webSocketClient.Connected); //webSocketClient.Port = 443; //webSocketClient.SSL = true; //var connectCode = webSocketClient.Connect(); //ErrorLog.Info("Err Code: " + (int)connectCode); //string server = "10.25.94.48"; //TcpClient client = new TcpClient(server, 41797); //SslStream sslStream = new SslStream(client.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null); ////ErrorLog.Info("SSL: " + sslStream.SslProtocol); string send = "reboot\r"; byte[] bytes = Encoding.ASCII.GetBytes(send); string send1 = "reboot"; byte[] bytes1 = Encoding.ASCII.GetBytes(send1); //using (sslStream) //{ // sslStream.AuthenticateAsClient(server); // ErrorLog.Info("SSL: " + sslStream.SslProtocol); // ErrorLog.Info("Can Write: " + sslStream.CanWrite); // //sslStream.Begi // sslStream.Write(bytes); // ErrorLog.Info("Can Write: " + sslStream.CanWrite); // This is where you read and send data //} //client.Close(); SecureTCPClient secureTCPClient = new SecureTCPClient("10.25.94.48", 41797, bytes.Length); var err1 = secureTCPClient.ConnectToServer(); ErrorLog.Info("Secure Error Code after connect: " + (int)err1); var sendErr = secureTCPClient.SendData(bytes, bytes.Length); ErrorLog.Info("Secure Error Code after Send: " + (int)sendErr); ErrorLog.Info("Server Address: " + secureTCPClient.AddressClientConnectedTo); ErrorLog.Info("Socket Status: " + (int)secureTCPClient.ClientStatus); var sendErr2 = secureTCPClient.SendData(bytes1, bytes1.Length); ErrorLog.Info("Secure Error Code after Send2: " + (int)sendErr2); var ids = secureTCPClient.DisconnectFromServer(); ErrorLog.Info("Secure Error Code after Disconnect: " + (int)ids); // TCPClient tCPClient = new TCPClient("10.25.94.48", 41797, 1000); // var err = tCPClient.ConnectToServer(); // ErrorLog.Info("Err code: " + (int)err); //tCPClient.SendData(bytes,0,1000); // tCPClient.DisconnectFromServer(); //System.Net.Sockets.TcpClient csnative = new System.Net.Sockets.TcpClient("10.25.94.48", 443); // ErrorLog.Info("Connected: " + csnative.Connected); // SslStream sslStream = new SslStream(csnative.GetStream()); // ErrorLog.Info("SSL: " + sslStream.IsAuthenticated); // sslStream.AuthenticateAsClient("10.25.94.24"); // ErrorLog.Info("SSL: " + sslStream.SslProtocol); //sslStream.AuthenticateAsClient("10.25.94.48"); //SshClient sshClient = new SshClient("C4-54B203910FDC", 22, "admin", "sfb"); //sshClient.ErrorOccurred += SshClient_ErrorOccurred1; //ErrorLog.Info("Connected? " + (int)tCPClient.ClientStatus); //SocketErrorCodes err = tCPClient.ConnectToServer(); //ErrorLog.Info("SocketErrorCode: " + (int)err); //ErrorLog.Info("Connected? " + (int)tCPClient.ClientStatus); //sslStream.Write(bytes); //tCPClient.SendData(bytes, 0, 1000); //SshClient sshClient = new SshClient("C4-54B203910FDC", 4443, "hrav", "2Loughmuck!"); //sshClient.ErrorOccurred += SshClient_ErrorOccurred; //sshClient.Connect(); //IEnumerable<ForwardedPort> s = sshClient.ForwardedPorts; //List<ForwardedPort> list = s.ToList(); //foreach( var a in list) //{ // ErrorLog.Info("" + a.IsStarted); //} //SshClient sshClient = new SshClient("10.25.94.48", 4443, "hrav", "2Loughmuck!"); //var stream = sshClient.CreateShellStream("terminal", 80, 24, 800, 600, 1024); //stream.Write("Reboot"); //SshCommand sshCommand = new SshCommand(); //ErrorLog.Info("Connected: " + sshClient.IsConnected); //SshCommand cmd = sshClient.RunCommand("info"); //string res = cmd.Execute(); //ErrorLog.Info("Response: " + res); } catch (Exception e) { //if(e.GetType == SshConnectionException) //ErrorLog.Info("Errrrrr : " + (int)e.DisconnectReason); ErrorLog.Error("Error in the constructor: {0}", e.Message); ErrorLog.Error("Error in the constructor: {0}", e.InnerException); ErrorLog.Error("Error in the constructor: {0}", e.ToString()); //ErrorLog.Error("Error in the constructor: {0}", e.Data); } }