public override async Task <IConnection> AcceptAsync() { if (Accepters == null) { Accepters = new Dictionary <Task <IConnection>, AsyncTransport>(); foreach (AsyncTransport transport in transports) { Task <IConnection> transportAccepter = transport.AcceptAsync(); Accepters[transportAccepter] = transport; } } // that's it nobody is left to accept if (Accepters.Count == 0) { return(null); } // wait for any one of them to accept var task = await Task.WhenAny(Accepters.Keys); AsyncTransport acceptedTransport = Accepters[task]; Accepters.Remove(task); IConnection value = await task; if (value == null) { // this transport closed. Get the next one return(await AcceptAsync()); } else { // transport may accept more connections task = acceptedTransport.AcceptAsync(); Accepters[task] = acceptedTransport; return(value); } }
// accept connections from clients private async Task AcceptAsync() { try { IConnection connection; while ((connection = await transport.AcceptAsync()) != null) { var networkConnectionToClient = new NetworkConnection(connection); _ = ConnectionAcceptedAsync(networkConnectionToClient); } Cleanup(); } catch (Exception ex) { logger.LogException(ex); } }