/// <summary> /// Stop Listeneing /// </summary> public void StopListening() { Debug.Console(2, "Stopping Listener"); if (SecureServer != null) { SecureServer.Stop(); } if (UnsecureServer != null) { UnsecureServer.Stop(); } ServerStopped = true; onServerStateChange(); }
/// <summary> /// Disposes of resources. /// </summary> public void Dispose() { if (tcpServer != null) { tcpServer.Stop(); tcpServer.DisconnectAll(); tcpServer = null; } if (secureTcpServer != null) { secureTcpServer.Stop(); secureTcpServer.DisconnectAll(); secureTcpServer = null; } }
/// <summary> /// Stop Listeneing /// </summary> public void StopListening() { try { Debug.Console(2, this, Debug.ErrorLogLevel.Notice, "Stopping Listener"); if (SecureServer != null) { SecureServer.Stop(); Debug.Console(2, this, Debug.ErrorLogLevel.Notice, "Server State: {0}", SecureServer.State); OnServerStateChange(SecureServer.State); } ServerStopped = true; } catch (Exception ex) { Debug.Console(2, this, Debug.ErrorLogLevel.Error, "Error stopping server. Error: {0}", ex); } }
public void ServerConnectedCallback(SecureTCPServer server, uint clientIndex) { if (clientIndex != 0) { CrestronConsole.PrintLine("Server listening on port " + server.PortNumber + " has connected with a client (client #" + clientIndex + ")"); server.ReceiveDataAsync(clientIndex, ServerDataReceivedCallback); if (server.MaxNumberOfClientSupported == server.NumberOfClientsConnected) { CrestronConsole.PrintLine("Client limit reached."); // This call to Stop() causes the server.State flag, SERVER_NOT_LISTENING, to be set server.Stop(); CrestronConsole.PrintLine("After calling server.Stop(), the server state is: " + server.State); } // If the client limit is reached, WaitForConnectionAsync will return SOCKET_MAX_CONNECTIONS_REACHED // and the ServerConnectedCallback will not be registered. Otherwise, the call to WaitForConnectionAsync // causes the server to keep listening for more clients. SocketErrorCodes err = server.WaitForConnectionAsync(ServerConnectedCallback); PrintAndLog("WaitForConnectionAsync returned: " + err); } // A clientIndex of 0 could mean that the server is no longer listening, or that the TLS handshake failed when a client tried to connect. // In the case of a TLS handshake failure, wait for another connection so that other clients can still connect else { CrestronConsole.Print("Error in ServerConnectedCallback: "); if ((server.State & ServerState.SERVER_NOT_LISTENING) > 0) { CrestronConsole.PrintLine("Server is no longer listening."); } else { CrestronConsole.PrintLine("Unable to make connection with client."); // This connection failed, but keep waiting for another server.WaitForConnectionAsync(ServerConnectedCallback); } } }
/// <summary> /// Secure TCP Client Connected to Secure Server Callback /// </summary> /// <param name="mySecureTCPServer"></param> /// <param name="clientIndex"></param> void SecureConnectCallback(SecureTCPServer server, uint clientIndex) { try { Debug.Console(1, this, Debug.ErrorLogLevel.Notice, "ConnectCallback: IPAddress: {0}. Index: {1}. Status: {2}", server.GetAddressServerAcceptedConnectionFromForSpecificClient(clientIndex), clientIndex, server.GetServerSocketStatusForSpecificClient(clientIndex)); if (clientIndex != 0) { if (server.ClientConnected(clientIndex)) { if (!ConnectedClientsIndexes.Contains(clientIndex)) { ConnectedClientsIndexes.Add(clientIndex); } if (SharedKeyRequired) { if (!WaitingForSharedKey.Contains(clientIndex)) { WaitingForSharedKey.Add(clientIndex); } byte[] b = Encoding.GetEncoding(28591).GetBytes("SharedKey:"); server.SendDataAsync(clientIndex, b, b.Length, (x, y, z) => { }); Debug.Console(1, this, Debug.ErrorLogLevel.Notice, "Sent Shared Key Request to client at {0}", server.GetAddressServerAcceptedConnectionFromForSpecificClient(clientIndex)); } else { OnServerClientReadyForCommunications(clientIndex); } if (HeartbeatRequired) { if (!HeartbeatTimerDictionary.ContainsKey(clientIndex)) { HeartbeatTimerDictionary.Add(clientIndex, new CTimer(HeartbeatTimer_CallbackFunction, clientIndex, HeartbeatRequiredIntervalMs)); } } server.ReceiveDataAsync(clientIndex, SecureReceivedDataAsyncCallback); } } else { Debug.Console(1, this, Debug.ErrorLogLevel.Error, "Client attempt faulty."); } } catch (Exception ex) { Debug.Console(2, this, Debug.ErrorLogLevel.Error, "Error in Socket Status Connect Callback. Error: {0}", ex); } // Rearm the listner SocketErrorCodes status = server.WaitForConnectionAsync(IPAddress.Any, SecureConnectCallback); if (status != SocketErrorCodes.SOCKET_OPERATION_PENDING) { Debug.Console(0, this, Debug.ErrorLogLevel.Error, "Socket status connect callback status {0}", status); if (status == SocketErrorCodes.SOCKET_CONNECTION_IN_PROGRESS) { // There is an issue where on a failed negotiation we need to stop and start the server. This should still leave connected clients intact. server.Stop(); Listen(); } } }