Task SendUsingNewConnection(IPipe<OwinHostContext> connectionPipe, OwinHostScope scope, CancellationToken stoppingToken) { try { if (_cacheTaskScope.StoppingToken.IsCancellationRequested) throw new TaskCanceledException($"The connection is being disconnected: {_settings.ToDebugString()}"); if (_log.IsDebugEnabled) _log.DebugFormat("Connecting: {0}", _settings.ToDebugString()); if (_log.IsDebugEnabled) { _log.DebugFormat("Connected: {0} (address: {1}, local: {2}", _settings.ToDebugString(), _settings.Host, _settings.Port); } var hostContext = new HttpOwinHostContext(_settings, _cacheTaskScope); hostContext.GetOrAddPayload(() => _settings); scope.Connected(hostContext); } catch (Exception ex) { Interlocked.CompareExchange(ref _scope, null, scope); scope.ConnectFaulted(ex); throw new HttpConnectionException("Connect failed: " + _settings.ToDebugString(), ex); } return SendUsingExistingConnection(connectionPipe, scope, stoppingToken); }
public Task Send(IPipe<OwinHostContext> connectionPipe, CancellationToken stoppingToken) { OwinHostScope newScope = null; OwinHostScope existingScope; lock (_scopeLock) { existingScope = _scope; if (existingScope == null) { newScope = new OwinHostScope(_cacheTaskScope, _settings); _scope = newScope; } } if (existingScope != null) return SendUsingExistingConnection(connectionPipe, existingScope, stoppingToken); return SendUsingNewConnection(connectionPipe, newScope, stoppingToken); }
public Task Send(IPipe <OwinHostContext> connectionPipe, CancellationToken stoppingToken) { OwinHostScope newScope = null; OwinHostScope existingScope; lock (_scopeLock) { existingScope = _scope; if (existingScope == null) { newScope = new OwinHostScope(_cacheTaskScope, _settings); _scope = newScope; } } if (existingScope != null) { return(SendUsingExistingConnection(connectionPipe, existingScope, stoppingToken)); } return(SendUsingNewConnection(connectionPipe, newScope, stoppingToken)); }
Task SendUsingNewConnection(IPipe <OwinHostContext> connectionPipe, OwinHostScope scope, CancellationToken stoppingToken) { try { if (_cacheTaskScope.StoppingToken.IsCancellationRequested) { throw new TaskCanceledException($"The connection is being disconnected: {_settings.ToDebugString()}"); } if (_log.IsDebugEnabled) { _log.DebugFormat("Connecting: {0}", _settings.ToDebugString()); } if (_log.IsDebugEnabled) { _log.DebugFormat("Connected: {0} (address: {1}, local: {2}", _settings.ToDebugString(), _settings.Host, _settings.Port); } var hostContext = new HttpOwinHostContext(_settings, _cacheTaskScope); hostContext.GetOrAddPayload(() => _settings); scope.Connected(hostContext); } catch (Exception ex) { Interlocked.CompareExchange(ref _scope, null, scope); scope.ConnectFaulted(ex); throw new HttpConnectionException("Connect failed: " + _settings.ToDebugString(), ex); } return(SendUsingExistingConnection(connectionPipe, scope, stoppingToken)); }
async Task SendUsingExistingConnection(IPipe <OwinHostContext> connectionPipe, OwinHostScope scope, CancellationToken cancellationToken) { try { using (var context = await scope.Attach(cancellationToken).ConfigureAwait(false)) { if (_log.IsDebugEnabled) { _log.DebugFormat("Using host: {0}", context.HostSettings.ToDebugString()); } await connectionPipe.Send(context).ConfigureAwait(false); } } catch (Exception ex) { if (_log.IsDebugEnabled) { _log.Debug("The host usage threw an exception", ex); } Interlocked.CompareExchange(ref _scope, null, scope); scope.ConnectFaulted(ex); throw; } }
async Task SendUsingExistingConnection(IPipe<OwinHostContext> connectionPipe, OwinHostScope scope, CancellationToken cancellationToken) { try { using (var context = await scope.Attach(cancellationToken).ConfigureAwait(false)) { if (_log.IsDebugEnabled) _log.DebugFormat("Using host: {0}", context.HostSettings.ToDebugString()); await connectionPipe.Send(context).ConfigureAwait(false); } } catch (Exception ex) { if (_log.IsDebugEnabled) _log.Debug("The host usage threw an exception", ex); Interlocked.CompareExchange(ref _scope, null, scope); scope.ConnectFaulted(ex); throw; } }