private async Task Run(CancellationToken cancellationToken) { while (!cancellationToken.IsCancellationRequested) { var client = await this.server.AcceptTcpClientAsync(); var inbound = await CreateInboundConnection(client); await inbound.OpenAsync(cancellationToken); Debug.WriteLine(string.Format("{0}: Connected", inbound.RemoteEndPoint)); var context = new SwitchboardContext(inbound); HandleSession(context, cancellationToken); } }
private async void HandleSession(SwitchboardContext context, CancellationToken cancellationToken) { try { Debug.WriteLine("{0}: Starting session", context.InboundConnection.RemoteEndPoint); do { var request = await context.InboundConnection.ReadRequestAsync(cancellationToken).ConfigureAwait(false); if (request == null) { return; } Debug.WriteLine(string.Format("{0}: Got {1} request for {2}", context.InboundConnection.RemoteEndPoint, request.Method, request.RequestUri)); var response = await handler.GetResponseAsync(context, request).ConfigureAwait(false); Debug.WriteLine(string.Format("{0}: Got response from handler ({1})", context.InboundConnection.RemoteEndPoint, response.StatusCode)); await context.InboundConnection.WriteResponseAsync(response).ConfigureAwait(false); Debug.WriteLine(string.Format("{0}: Wrote response to client", context.InboundConnection.RemoteEndPoint)); if (context.OutboundConnection != null && !context.OutboundConnection.IsConnected) { context.Close(); } } while (context.InboundConnection.IsConnected); } catch (Exception exc) { Debug.WriteLine(string.Format("{0}: Error: {1}", context.InboundConnection.RemoteEndPoint, exc.Message)); context.Close(); Debug.WriteLine(string.Format("{0}: Closed context", context.InboundConnection.RemoteEndPoint, exc.Message)); } finally { context.Dispose(); } }