/// <summary> /// Ends the current client connection. /// </summary> /// <param name="sender">The current server to client connection channel.</param> private void EndConnection(SingleContextInt sender) { // Decrement the count. DecrementCount(); // Signal to the blocking handler // to un-block. lock (_lockingObject) _connAvailable.Set(); try { lock (_lockingObject) { try { // Make sure remotes servers exist. if (_remoteServers != null && _remoteServers.Count > 0) { // Get all matching remotes servers. IEnumerable <RemoteServer> items = _remoteServers.Where(u => (u.Host.ToLower() == sender.RemoteServer.Host.ToLower()) && (u.Port == sender.RemoteServer.Port)); // Take the first item. if (items.Count() > 0) { // Decrement the count. RemoteServer remoteServer = items.First(); remoteServer.Count -= 1; } } } catch { } // Send the new disconnection to the caller. if (OnClientDisconnected != null) { OnClientDisconnected(this, sender); } } } catch { } try { lock (_lockingObject) { // Attempt to release all the resources of the curretn server context. sender.Dispose(); sender = null; } } catch { } }
/// <summary> /// Called when the socket has some data to read. /// </summary> void IMultiplexed.ReadyRead() { // Do not allow any more clients // if maximum is reached. if (_clientCount <= _maxNumClients) { Socket clientSocket = null; SingleContextInt client = null; bool endTriggered = false; try { // Clear last error. ClearLastError(); // Get the socket that handles the client request. clientSocket = _socket.Accept(); // Determine which buffer this client should use. int bufferIndex = (int)(_clientCount / _totalNumSegments); // Create a new client host // Add this client socket to the list. client = new SingleContextInt(); // Assign the accepted socket. client.Socket = clientSocket; client.UseSslConnection = _useSslConnection; client.BeginSslAuthentication = _beginSslAuthentication; client.X509Certificate = _sslCertificate; client.Name = _serverName; client.SslProtocols = _sslProtocols; client.SocketType = _socketType; client.ProtocolType = _protocolType; client.ReceiveSocketFlags = _receiveSocketFlags; client.SendSocketFlags = _sendSocketFlags; client.Timeout = _timeOut; client.RequestBufferCapacity = _requestBufferCapacity; client.ResponseBufferCapacity = _responseBufferCapacity; client.Port = _port; client.ServiceName = _serviceName; client.NumberOfClients = _clientCount; client.OnReceivedHandler = _onReceivedActionHandler; client.SendToServerInfoHandler = _sendToServerInfoHandler; client.WritePoller = !_pollWriter; client.WriteBufferSize = WRITE_BUFFER_SIZE; client.ReadBufferSize = READ_BUFFER_SIZE; client.WaitForTlsCommand = _waitForTlsCommand; client.TlsCommand = _tlsCommand; client.TlsAcknowledgeCommand = _tlsAcknowledgeCommand; client.InterceptItems = _interceptItems; client.RemoteServer = GetRemoteServer(_algorithmType); client.EndConnectionCallback = (end, context) => { // End has been triggered. endTriggered = end; try { // Uregisters the socket from issuing notifications. _multiplexer.RemoveSocket(context.Socket); } catch { } // End the connection. EndConnection(context); }; // Create and assign each component. client.Initialise(); // Increment the count. IncrementCount(); try { // Send the new connection to the caller. if (OnClientConnected != null) { OnClientConnected(this, client); } // Add the socket to the multiplexer. if (!endTriggered) { _multiplexer.AddSocket(clientSocket, client); } } catch (Exception ex) { if (client != null) { client.Dispose(); } SetLastError(ex); } } catch (Exception ex) { SetLastError(ex); } } }