private async Task ExecuteServerListener(CancellationToken cancellationToken) { _log = LogManager?.CreateLog(typeof(FtpServer)); using (var listener = new TcpSocketListener(0)) { listener.ConnectionReceived = ConnectionReceived; try { await listener.StartListeningAsync(Port); try { for (; ;) { cancellationToken.ThrowIfCancellationRequested(); await Task.Delay(100, cancellationToken); } } finally { await listener.StopListeningAsync(); foreach (var connection in _connections.ToArray()) { connection.Close(); } } } catch (Exception ex) { _log?.Fatal(ex, "{0}", ex.Message); } } }
private Task ExecuteServerListener(AutoResetEvent e) { return(Task.Run(() => { _log = LogManager?.CreateLog(typeof(FtpServer)); using (var listener = new TcpSocketListener(0)) { listener.ConnectionReceived = ConnectionReceived; try { e.Reset(); listener.StartListeningAsync(Port).Wait(); _log?.Debug("Server listening on port {0}", Port); try { e.WaitOne(); } finally { listener.StopListeningAsync().Wait(); foreach (var connection in _connections.ToArray()) { connection.Close(); } } } catch (Exception ex) { _log?.Fatal(ex, "{0}", ex.Message); } } } )); }