public void AddConnection(long id, KestrelConnection connection) { if (!_connectionReferences.TryAdd(id, new ConnectionReference(connection))) { throw new ArgumentException(nameof(id)); } }
private void StartAcceptingConnectionsCore(IConnectionListener listener) { // REVIEW: Multiple accept loops in parallel? _ = AcceptConnectionsAsync(); async Task AcceptConnectionsAsync() { try { while (true) { var connection = await listener.AcceptAsync(); if (connection == null) { // We're done listening break; } // Add the connection to the connection manager before we queue it for execution var id = Interlocked.Increment(ref _lastConnectionId); var kestrelConnection = new KestrelConnection(id, _serviceContext, _connectionDelegate, connection, Log); _serviceContext.ConnectionManager.AddConnection(id, kestrelConnection); Log.ConnectionAccepted(connection.ConnectionId); ThreadPool.UnsafeQueueUserWorkItem(kestrelConnection, preferLocal: false); } } catch (Exception ex) { // REVIEW: If the accept loop ends should this trigger a server shutdown? It will manifest as a hang Log.LogCritical(0, ex, "The connection listener failed to accept any new connections."); } finally { _acceptLoopTcs.TrySetResult(null); } } }
public bool TryGetConnection(out KestrelConnection connection) { return(_weakReference.TryGetTarget(out connection)); }
public ConnectionReference(KestrelConnection connection) { _weakReference = new WeakReference <KestrelConnection>(connection); ConnectionId = connection.TransportConnection.ConnectionId; }