/// <summary> /// Ends the current client connection. /// </summary> /// <param name="sender">The current server to client connection channel.</param> private void EndConnection(SingleContext sender) { // Decrement the count. DecrementCount(); // Signal to the blocking handler // to un-block. lock (_lockingObject) _connAvailable.Set(); try { lock (_lockingObject) { // 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> /// Create a new instance of the server context type. /// </summary> /// <param name="state">The current state, contains a socket client.</param> private void CreateServerContext(object state) { SingleContext client = null; bool endTriggered = false; try { // Clear last error. ClearLastError(); // Create a new client host client = new SingleContext(); // Assign the accepted socket. client.Socket = (System.Net.Sockets.Socket)state; 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.WriteBufferSize = WRITE_BUFFER_SIZE; client.ReadBufferSize = READ_BUFFER_SIZE; client.EndConnectionCallback = (end, socketClient) => { // End has been triggered. endTriggered = end; // End the connection. EndConnection(socketClient); }; // 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); } } catch (Exception ex) { if (client != null) { client.Dispose(); } SetLastError(ex); } } catch (Exception ex) { SetLastError(ex); } }
/// <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; SingleContext 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 SingleContext(); // 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.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); } } }