/// <summary> /// This method is used internally to notify server about the client disconnection /// </summary> internal void NotifyClientDisconnection(ProxyServerConnection connection) { lock (SyncRoot) { // Remove the existing connection from the list Connections.Remove(connection); } }
/// <summary> /// Processes all incoming requests /// </summary> private void _RequestListener() { try { while (!StopRequested) { if (ListenerSocket.Pending()) { try { Log("Connection received"); // Accept the connection Task<Socket> connectTask = ListenerSocket.AcceptSocketAsync(); connectTask.Wait(); Socket newConnection = connectTask.Result; // Start a new connection ProxyServerConnection proxyConnection = new ProxyServerConnection(newConnection, this); proxyConnection.Start(); // Registering new connection lock (SyncRoot) { Connections.Add(proxyConnection); } } catch (Exception ex) { Log(ex.ToString()); } } else { // Pause a bit Thread.Sleep(10); } } } catch (Exception ex) { Log(ex.ToString()); } Log("Stopping the server..."); // Stop the server ListenerSocket.Stop(); ListenerSocket = null; Log("Waiting for client connections to terminate..."); // Wait for connections int connectionsLeft = short.MaxValue; while (connectionsLeft > 0) { lock (SyncRoot) { // Check the amount of connections left connectionsLeft = Connections.Count; } // Wait a bit Thread.Sleep(10); } Log("Server is stopped"); }
/// <summary> /// Processes all incoming requests /// </summary> private void _RequestListener() { try { while (!StopRequested) { if (ListenerSocket.Pending()) { try { Log("Connection received"); // Accept the connection Task <Socket> connectTask = ListenerSocket.AcceptSocketAsync(); connectTask.Wait(); Socket newConnection = connectTask.Result; // Start a new connection ProxyServerConnection proxyConnection = new ProxyServerConnection(newConnection, this); proxyConnection.Start(); // Registering new connection lock (SyncRoot) { Connections.Add(proxyConnection); } } catch (Exception ex) { Log(ex.ToString()); } } else { // Pause a bit Thread.Sleep(10); } } } catch (Exception ex) { Log(ex.ToString()); } Log("Stopping the server..."); // Stop the server ListenerSocket.Stop(); ListenerSocket = null; Log("Waiting for client connections to terminate..."); // Wait for connections int connectionsLeft = short.MaxValue; while (connectionsLeft > 0) { lock (SyncRoot) { // Check the amount of connections left connectionsLeft = Connections.Count; } // Wait a bit Thread.Sleep(10); } Log("Server is stopped"); }