1818using FubarDev . FtpServer . ConnectionChecks ;
1919using FubarDev . FtpServer . Features ;
2020using FubarDev . FtpServer . Localization ;
21- using FubarDev . FtpServer . Networking ;
2221using FubarDev . FtpServer . ServerCommands ;
2322
2423using Microsoft . Extensions . DependencyInjection ;
@@ -37,10 +36,9 @@ public sealed class FtpServer : IFtpServer, IDisposable
3736 private readonly List < IFtpConnectionConfigurator > _connectionConfigurators ;
3837 private readonly List < IFtpControlStreamAdapter > _controlStreamAdapters ;
3938 private readonly ConcurrentDictionary < IFtpConnection , FtpConnectionInfo > _connections = new ConcurrentDictionary < IFtpConnection , FtpConnectionInfo > ( ) ;
40- private readonly FtpServerListenerService _serverListener ;
39+ private readonly IFtpListenerService _serverListener ;
4140 private readonly ILogger < FtpServer > ? _log ;
4241 private readonly Task _clientReader ;
43- private readonly CancellationTokenSource _serverShutdown = new CancellationTokenSource ( ) ;
4442 private readonly Timer ? _connectionTimeoutChecker ;
4543
4644 /// <summary>
@@ -50,12 +48,14 @@ public sealed class FtpServer : IFtpServer, IDisposable
5048 /// <param name="serviceProvider">The service provider used to query services.</param>
5149 /// <param name="controlStreamAdapters">Adapters for the control connection stream.</param>
5250 /// <param name="connectionConfigurators">Configurators for FTP connections.</param>
51+ /// <param name="ftpListenerService">Listener service for FTP connections.</param>
5352 /// <param name="logger">The FTP server logger.</param>
5453 public FtpServer (
5554 IOptions < FtpServerOptions > serverOptions ,
5655 IServiceProvider serviceProvider ,
5756 IEnumerable < IFtpControlStreamAdapter > controlStreamAdapters ,
5857 IEnumerable < IFtpConnectionConfigurator > connectionConfigurators ,
58+ IFtpListenerService ftpListenerService ,
5959 ILogger < FtpServer > ? logger = null )
6060 {
6161 _serviceProvider = serviceProvider ;
@@ -66,15 +66,16 @@ public FtpServer(
6666 Port = serverOptions . Value . Port ;
6767 MaxActiveConnections = serverOptions . Value . MaxActiveConnections ;
6868
69- var tcpClientChannel = Channel . CreateBounded < TcpClient > ( 5 ) ;
70- _serverListener = new FtpServerListenerService ( tcpClientChannel , serverOptions , _serverShutdown , logger ) ;
69+ _serverListener = ftpListenerService ;
7170 _serverListener . ListenerStarted += ( s , e ) =>
7271 {
7372 Port = e . Port ;
7473 OnListenerStarted ( e ) ;
7574 } ;
7675
77- _clientReader = ReadClientsAsync ( tcpClientChannel , _serverShutdown . Token ) ;
76+ _clientReader = ReadClientsAsync (
77+ _serverListener . Channel ,
78+ _serverListener . ListenerShutdown . Token ) ;
7879
7980 if ( serverOptions . Value . ConnectionInactivityCheckInterval is TimeSpan checkInterval )
8081 {
@@ -120,7 +121,7 @@ public void Dispose()
120121
121122 _connectionTimeoutChecker ? . Dispose ( ) ;
122123
123- _serverShutdown . Dispose ( ) ;
124+ ( _serverListener as IDisposable ) ? . Dispose ( ) ;
124125 foreach ( var connectionInfo in _connections . Values )
125126 {
126127 connectionInfo . Scope . Dispose ( ) ;
@@ -177,9 +178,9 @@ public async Task StartAsync(CancellationToken cancellationToken)
177178 /// <inheritdoc />
178179 public async Task StopAsync ( CancellationToken cancellationToken )
179180 {
180- if ( ! _serverShutdown . IsCancellationRequested )
181+ if ( ! _serverListener . ListenerShutdown . IsCancellationRequested )
181182 {
182- _serverShutdown . Cancel ( true ) ;
183+ _serverListener . ListenerShutdown . Cancel ( true ) ;
183184 }
184185
185186 await _serverListener . StopAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
@@ -297,7 +298,7 @@ private async Task AddClientAsync(TcpClient client)
297298 // Remember connection
298299 if ( ! _connections . TryAdd ( connection , new FtpConnectionInfo ( scope ) ) )
299300 {
300- _log . LogCritical ( "A new scope was created, but the connection couldn't be added to the list. " ) ;
301+ _log . LogCritical ( "A new scope was created, but the connection couldn't be added to the list" ) ;
301302 client . Dispose ( ) ;
302303 scope . Dispose ( ) ;
303304 return ;
@@ -359,7 +360,7 @@ await connection.StartAsync()
359360 catch ( Exception ex )
360361 {
361362 scope . Dispose ( ) ;
362- _log ? . LogError ( ex , ex . Message ) ;
363+ _log ? . LogError ( ex , "Failed to start the client connection: {ErrorMessage}" , ex . Message ) ;
363364 }
364365 }
365366
0 commit comments