private async Task AcceptTransportsAsync() { while (!_acceptTransportCts.IsCancellationRequested) { try { var httpContext = await _httpListener .GetContextAsync() .WithCancellation(_acceptTransportCts.Token) .ConfigureAwait(false); await _httpListenerContextBufferBlock .SendAsync(httpContext, _acceptTransportCts.Token) .ConfigureAwait(false); } catch (OperationCanceledException) when(_acceptTransportCts.IsCancellationRequested) { break; } catch (Exception ex) { var args = new ExceptionEventArgs(ex); ListenerException.RaiseEvent(this, new ExceptionEventArgs(ex)); await args.WaitForDeferralsAsync(); } } }
private async Task PingRemoteAsync() { LastReceivedEnvelope = DateTime.UtcNow; while (_channel.IsEstablished() && !_cts.IsCancellationRequestedOrDisposed()) { try { // Waits for the next ping await Task.Delay(_remotePingInterval, _cts.Token).ConfigureAwait(false); if (!_channel.IsEstablished()) { break; } var idleTime = DateTimeOffset.UtcNow - LastReceivedEnvelope; if (_hasPendingPingRequest && _remoteIdleTimeout > TimeSpan.Zero && idleTime >= _remoteIdleTimeout) { using var cts = new CancellationTokenSource(_finishChannelTimeout); switch (_channel) { case IClientChannel clientChannel: await FinishAsync(clientChannel, cts.Token).ConfigureAwait(false); break; case IServerChannel serverChannel: await FinishAsync(serverChannel, cts.Token).ConfigureAwait(false); break; } } else if (idleTime >= _remotePingInterval) { _lastPingCommandRequestId = EnvelopeId.NewId(); // Send a ping command to the remote party var pingCommandRequest = new Command(_lastPingCommandRequestId) { Method = CommandMethod.Get, Uri = new LimeUri(PING_URI) }; _hasPendingPingRequest = true; using var cts = new CancellationTokenSource(_remotePingInterval); await _channel.SendCommandAsync(pingCommandRequest, cts.Token) .ConfigureAwait(false); } } catch (ObjectDisposedException) when(_disposing) { break; } catch (OperationCanceledException) when(_cts.IsCancellationRequested) { break; } catch (Exception ex) { using var cts = new CancellationTokenSource(_finishChannelTimeout); var args = new ExceptionEventArgs(ex); RemotePingException?.Invoke(this, args); await args.WaitForDeferralsAsync(cts.Token).ConfigureAwait(false); } } }