예제 #1
0
        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);
                }
            }
        }
예제 #2
0
        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);
                    }
                }
            }
                            ));
        }