private async Task AcceptConnectionsAsync(CancellationToken cancellationToken) { try { while (!cancellationToken.IsCancellationRequested) { var client = await _socketWrapper.AcceptAsync().ConfigureAwait(false); HttpNetTrace.Information(nameof(HttpServer), "Client '{0}' connected.", client.Identifier); #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed Task.Run(async() => await HandleClientConnectionAsync(client).ConfigureAwait(false), _cancellationTokenSource.Token).ConfigureAwait(false); #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed } } catch (OperationCanceledException) { } catch (Exception exception) { HttpNetTrace.Error(nameof(HttpServer), exception, "Unhandled exception while accepting clients."); } finally { _cancellationTokenSource?.Cancel(false); } }
private async Task HandleClientConnectionAsync(IClientSocketWrapper client) { using (var clientSession = new ClientSession(client, this, _options)) { try { await clientSession.RunAsync().ConfigureAwait(false); } catch (OperationCanceledException) { } catch (Exception exception) { HttpNetTrace.Error(nameof(HttpServer), exception, "Unhandled exception while handling cient connection."); } finally { HttpNetTrace.Information(nameof(HttpServer), "Client '{0}' disconnected.", client.Identifier); await client.DisconnectAsync(); } } }