/// <summary> /// Receives event from connected TcpClient and invokes handler on the event. /// </summary> /// <param name="client">The connected client</param> private async Task ProcessClient(TcpClient client) { CancellationToken token = _cancellationSource.Token; try { // Keep reading messages from client until they disconnect or timeout using (ILink <T> link = new StreamingLink <T>(client, _streamingCodec)) { while (!token.IsCancellationRequested) { T message = await link.ReadAsync(token); if (message == null) { break; } TransportEvent <T> transportEvent = new TransportEvent <T>(message, link); _remoteObserver.OnNext(transportEvent); } LOGGER.Log(Level.Error, "ProcessClient close the Link. IsCancellationRequested: " + token.IsCancellationRequested); } } catch (Exception e) { LOGGER.Log(Level.Warning, "StreamingTransportServer get exception in ProcessClient: {0}, IsCancellationRequested {1}.", e.GetType(), token.IsCancellationRequested); throw e; } }
/// <summary> /// Helper method to start TransportServer. This will /// be run in an asynchronous Task. /// </summary> /// <returns>An asynchronous Task for the running server.</returns> private async Task StartServer() { try { while (!_cancellationSource.Token.IsCancellationRequested) { TcpClient client = await _listener.AcceptTcpClientAsync().ConfigureAwait(false); ILink <T> link = new StreamingLink <T>(client, _streamingCodec); ProcessClient(client, link).Forget(); } } catch (InvalidOperationException) { LOGGER.Log(Level.Info, "StreamingTransportServer has been closed."); } catch (OperationCanceledException) { LOGGER.Log(Level.Info, "StreamingTransportServer has been closed."); } }
/// <summary> /// Receives event from connected TcpClient and invokes handler on the event. /// </summary> /// <param name="client">The connected client</param> private async Task ProcessClient(TcpClient client) { // Keep reading messages from client until they disconnect or timeout CancellationToken token = _cancellationSource.Token; using (ILink <T> link = new StreamingLink <T>(client, _streamingCodec)) { while (!token.IsCancellationRequested) { T message = await link.ReadAsync(token); if (message == null) { break; } TransportEvent <T> transportEvent = new TransportEvent <T>(message, link); _remoteObserver.OnNext(transportEvent); } LOGGER.Log(Level.Error, "ProcessClient close the Link. IsCancellationRequested: " + token.IsCancellationRequested); } }