public override async Task <HostHandle> Start() { var handlesReady = new TaskCompletionSource <HostReceiveEndpointHandle[]>(); var hostStarted = new TaskCompletionSource <bool>(); IPipe <HttpHostContext> connectionPipe = Pipe.ExecuteAsync <HttpHostContext>(async context => { if (_log.IsDebugEnabled) { _log.DebugFormat("Connection established to {0}", Settings.Host); } try { await handlesReady.Task.ConfigureAwait(false); await context.Start(context.CancellationToken).ConfigureAwait(false); hostStarted.TrySetResult(true); await Completed.ConfigureAwait(false); } catch (OperationCanceledException ex) { hostStarted.TrySetException(ex); } catch (Exception ex) { hostStarted.TrySetException(ex); throw; } }); if (_log.IsDebugEnabled) { _log.DebugFormat("Starting connection to {0}", Settings.Host); } var connectionTask = HttpHostContextSupervisor.Send(connectionPipe, Stopping); HostReceiveEndpointHandle[] handles = ReceiveEndpoints.StartEndpoints(); var hostHandle = new Handle(handles, this, _httpHostContextSupervisor, connectionTask); await hostHandle.Ready.ConfigureAwait(false); handlesReady.TrySetResult(handles); await hostStarted.Task.ConfigureAwait(false); return(hostHandle); }
public override async Task <HostHandle> Start(CancellationToken cancellationToken) { LogContext.Debug?.Log("Starting host: {HostAddress}", _hostConfiguration.HostAddress); DefaultLogContext = LogContext.Current; SendLogContext = LogContext.Current.CreateLogContext(LogCategoryName.Transport.Send); ReceiveLogContext = LogContext.Current.CreateLogContext(LogCategoryName.Transport.Receive); var handlesReady = TaskUtil.GetTask <HostReceiveEndpointHandle[]>(); var hostStarted = TaskUtil.GetTask <bool>(); IPipe <HttpHostContext> connectionPipe = Pipe.ExecuteAsync <HttpHostContext>(async context => { try { await handlesReady.Task.ConfigureAwait(false); await context.Start(context.CancellationToken).ConfigureAwait(false); hostStarted.TrySetResult(true); await Completed.ConfigureAwait(false); } catch (OperationCanceledException ex) { hostStarted.TrySetException(ex); } catch (Exception ex) { hostStarted.TrySetException(ex); throw; } }); var connectionTask = HttpHostContextSupervisor.Send(connectionPipe, Stopping); HostReceiveEndpointHandle[] handles = ReceiveEndpoints.StartEndpoints(cancellationToken); HostHandle hostHandle = new StartHostHandle(this, handles, GetAgentHandles()); await hostHandle.Ready.ConfigureAwait(false); handlesReady.TrySetResult(handles); await hostStarted.Task.ConfigureAwait(false); return(hostHandle); }