private void ExecuteServerListener(SemaphoreSlim semaphore) { Task.Run(async() => { var listener = new MultiBindingTcpListener(ServerAddress, Port, _log); try { await listener.StartAsync().ConfigureAwait(false); listener.StartAccepting(); Ready = true; semaphore.Release(); try { while (!Stopped && !_cancellationTokenSource.IsCancellationRequested) { var acceptTask = listener.WaitAnyTcpClientAsync(_cancellationTokenSource.Token); var client = acceptTask.Result; AddClient(client); } } catch (OperationCanceledException) { // Ignore - everything is fine } finally { listener.Stop(); foreach (var connection in _connections.Keys.ToList()) { connection.Close(); } } } catch (Exception ex) { _log?.LogCritical(ex, "{0}", ex.Message); } finally { _stoppedSemaphore.Release(); } }); }
private Task ExecuteServerListener() { return(Task.Run(async() => { var listener = new MultiBindingTcpListener(ServerAddress, Port); try { await listener.StartAsync().ConfigureAwait(false); Ready = true; try { while (!Stopped) { if (listener.TryGetPending(out var tcpListener)) { var acceptTask = tcpListener.AcceptTcpClientAsync(); acceptTask.Wait(_cancellationTokenSource.Token); var client = acceptTask.Result; AddClient(client); continue; } Thread.Sleep(0); } } finally { listener.Stop(); foreach (var connection in _connections.Keys.ToList()) { connection.Close(); } } } catch (Exception ex) { _log?.LogCritical(ex, "{0}", ex.Message); } })); }
private Task ExecuteServerListener() { return(Task.Run(async() => { var listener = new MultiBindingTcpListener(ServerAddress, Port); try { await listener.StartAsync().ConfigureAwait(false); listener.StartAccepting(); Ready = true; try { while (!Stopped) { var acceptTask = listener.WaitAnyTcpClientAsync(_cancellationTokenSource.Token); var client = acceptTask.Result; AddClient(client); continue; } } catch (OperationCanceledException) { // Ignorieren - alles ist OK } finally { listener.Stop(); foreach (var connection in _connections.Keys.ToList()) { connection.Close(); } } } catch (Exception ex) { _log?.LogCritical(ex, "{0}", ex.Message); } })); }