/// <summary> Function to remove a socket from the list of sockets </summary> /// <param name="connection">The index of the socket to remove </param> public void RemoveSocket(AdkSocketConnection connection) { lock (fSocketConnections.SyncRoot) { try { fSocketConnections.Remove(connection); } catch { } } }
/// <summary> /// Closes any idle socket connections /// </summary> public void Cleanup(object timeInterval) { try { fCleanupTimer.Change(Timeout.Infinite, Timeout.Infinite); lock (fSocketConnections.SyncRoot) { for (int i = fSocketConnections.Count - 1; i > -1; i--) { AdkSocketConnection connection = (AdkSocketConnection)fSocketConnections[i]; if (connection.IdleTime.TotalSeconds > KEEP_ALIVE_TIME) { connection.Dispose(); // will automatically remove itself } } } } finally { TimeSpan timerInterval = (TimeSpan)timeInterval; fCleanupTimer.Change(timerInterval, timerInterval); } }
/// <summary> Function to process and accept socket connection requests </summary> private void AcceptNewConnection( IAsyncResult ar ) { if ( fIsShuttingDown ) { return; } IConnectedSocket clientSocket = null; try { // Call the derived class's accept handler clientSocket = fAcceptSocket.EndAccept( ar ); if ( !clientSocket.Connected ) { return; } lock ( fSocketConnections.SyncRoot ) { try { // If we have room to accept this connection if ( fSocketConnections.Count <= MaxClientConnections ) { // Create a AdkSocketConnection object AdkSocketConnection adkSocket = new AdkSocketConnection ( this, clientSocket, RawBufferSize, DataReceived, SocketClosed, SocketError ); // Call the Accept Handler fSocketConnections.Add( adkSocket ); if ( SocketAccepted != null ) { try { SocketAccepted( adkSocket ); } catch { } } } else { OnMaxConnectionsReached( clientSocket ); // Close the socket connection clientSocket.Shutdown( SocketShutdown.Both ); clientSocket.Close(); } } catch ( SocketException e ) { // Did we stop the TCPListener if ( e.ErrorCode == 10004 ) { // The connection is being shut down, ignore the error } else { OnError( e ); // Close the socket down if it exists if ( clientSocket != null && clientSocket.Connected ) { clientSocket.Shutdown( SocketShutdown.Both ); clientSocket.Close(); } } } catch ( Exception ex ) { OnError( ex ); // Close the socket down if it exists if ( clientSocket != null && clientSocket.Connected ) { clientSocket.Shutdown( SocketShutdown.Both ); clientSocket.Close(); } } } } catch ( SocketException e ) { // Did we stop the TCPListener if ( e.ErrorCode == 10004 ) { // We're done return; } else { OnError( e ); // Close the socket down if it exists if ( clientSocket != null && clientSocket.Connected ) { clientSocket.Shutdown( SocketShutdown.Both ); clientSocket.Close(); } } } finally { if ( fIsRunning ) { fAcceptSocket.BeginAccept( new AsyncCallback( AcceptNewConnection ), null ); } } }
/// <summary> Function to remove a socket from the list of sockets </summary> /// <param name="connection">The index of the socket to remove </param> public void RemoveSocket( AdkSocketConnection connection ) { lock ( fSocketConnections.SyncRoot ) { try { fSocketConnections.Remove( connection ); } catch { } } }
/// <summary> Function to process and accept socket connection requests </summary> private void AcceptNewConnection(IAsyncResult ar) { if (fIsShuttingDown) { return; } IConnectedSocket clientSocket = null; try { // Call the derived class's accept handler clientSocket = fAcceptSocket.EndAccept(ar); if (!clientSocket.Connected) { return; } lock (fSocketConnections.SyncRoot) { try { // If we have room to accept this connection if (fSocketConnections.Count <= MaxClientConnections) { // Create a AdkSocketConnection object AdkSocketConnection adkSocket = new AdkSocketConnection ( this, clientSocket, RawBufferSize, DataReceived, SocketClosed, SocketError ); // Call the Accept Handler fSocketConnections.Add(adkSocket); if (SocketAccepted != null) { try { SocketAccepted(adkSocket); } catch { } } } else { OnMaxConnectionsReached(clientSocket); // Close the socket connection clientSocket.Shutdown(SocketShutdown.Both); clientSocket.Close(); } } catch (SocketException e) { // Did we stop the TCPListener if (e.ErrorCode == 10004) { // The connection is being shut down, ignore the error } else { OnError(e); // Close the socket down if it exists if (clientSocket != null && clientSocket.Connected) { clientSocket.Shutdown(SocketShutdown.Both); clientSocket.Close(); } } } catch (Exception ex) { OnError(ex); // Close the socket down if it exists if (clientSocket != null && clientSocket.Connected) { clientSocket.Shutdown(SocketShutdown.Both); clientSocket.Close(); } } } } catch (SocketException e) { // Did we stop the TCPListener if (e.ErrorCode == 10004) { // We're done return; } else { OnError(e); // Close the socket down if it exists if (clientSocket != null && clientSocket.Connected) { clientSocket.Shutdown(SocketShutdown.Both); clientSocket.Close(); } } } finally { if (fIsRunning) { fAcceptSocket.BeginAccept(new AsyncCallback(AcceptNewConnection), null); } } }