private void RunHandlerTask(IConnectionHandler handler, HandlerContext context) { var handlerTask = Task.Run(async () => { if (handler is IAsyncConnectionHandler) { await ((IAsyncConnectionHandler)handler).ProcessAsync(context); } else { await Task.Run(() => handler.Process(context)); } }).ContinueWith(t => { if (t.IsFaulted) { OnHandlerTaskError(handler, context); } }); activeHandlerTasks.Add(handlerTask); }
private IConnectionHandler CreateHandler(HandlerContext context) { return handlerFactory.Invoke(context); }
private void OnHandlerTaskError(IConnectionHandler handler, HandlerContext context) { }
private Task RunListenerTask() { return Task.Run(async () => { while (!cancellationSource.IsCancellationRequested) { var socket = await tcpListener.AcceptSocketAsync(); var context = new HandlerContext(new TcpChannel(socket)); var handler = CreateHandler(context); RunHandlerTask(handler, context); } }).ContinueWith(OnListenerTaskError); }