Skip to content

Commit 4311d13

Browse files
committed
Merge branch 'release/3.1.2'
2 parents 2effdb2 + 233df98 commit 4311d13

6 files changed

Lines changed: 65 additions & 17 deletions

File tree

.editorconfig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ dotnet_naming_style.camel_case_underscore_style.required_prefix = _
6060
dotnet_naming_style.camel_case_underscore_style.capitalization = camel_case
6161

6262
# private static fields should be _camelCase
63-
dotnet_naming_rule.camel_case_for_private_internal_fields.severity = suggestion
64-
dotnet_naming_rule.camel_case_for_private_internal_fields.symbols = private_static_fields
65-
dotnet_naming_rule.camel_case_for_private_internal_fields.style = camel_case_underscore_style
63+
dotnet_naming_rule.camel_case_for_private_static_fields.severity = suggestion
64+
dotnet_naming_rule.camel_case_for_private_static_fields.symbols = private_static_fields
65+
dotnet_naming_rule.camel_case_for_private_static_fields.style = camel_case_underscore_style
6666

6767
dotnet_naming_symbols.private_static_fields.applicable_kinds = field
6868
dotnet_naming_symbols.private_static_fields.required_modifiers = static

src/FubarDev.FtpServer/ConnectionHandlers/SecureConnectionAdapter.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
using FubarDev.FtpServer.Authentication;
1111

12+
using Microsoft.Extensions.Logging;
13+
1214
namespace FubarDev.FtpServer.ConnectionHandlers
1315
{
1416
/// <summary>
@@ -21,6 +23,7 @@ internal class SecureConnectionAdapter : IFtpSecureConnectionAdapter
2123
private readonly ISslStreamWrapperFactory _sslStreamWrapperFactory;
2224

2325
private readonly CancellationToken _connectionClosed;
26+
private readonly ILoggerFactory? _loggerFactory;
2427
private IFtpConnectionAdapter _activeCommunicationService;
2528

2629
/// <summary>
@@ -30,20 +33,24 @@ internal class SecureConnectionAdapter : IFtpSecureConnectionAdapter
3033
/// <param name="connectionPipe">The pipe to the connection object.</param>
3134
/// <param name="sslStreamWrapperFactory">The SSL stream wrapper factory.</param>
3235
/// <param name="connectionClosed">The cancellation token for a closed connection.</param>
36+
/// <param name="loggerFactory">The logger factory.</param>
3337
public SecureConnectionAdapter(
3438
IDuplexPipe socketPipe,
3539
IDuplexPipe connectionPipe,
3640
ISslStreamWrapperFactory sslStreamWrapperFactory,
37-
CancellationToken connectionClosed)
41+
CancellationToken connectionClosed,
42+
ILoggerFactory? loggerFactory = null)
3843
{
3944
_socketPipe = socketPipe;
4045
_connectionPipe = connectionPipe;
4146
_sslStreamWrapperFactory = sslStreamWrapperFactory;
4247
_connectionClosed = connectionClosed;
48+
_loggerFactory = loggerFactory;
4349
_activeCommunicationService = new PassThroughConnectionAdapter(
4450
socketPipe,
4551
connectionPipe,
46-
connectionClosed);
52+
connectionClosed,
53+
loggerFactory);
4754
}
4855

4956
/// <inheritdoc />
@@ -60,7 +67,8 @@ await StopAsync(cancellationToken)
6067
_activeCommunicationService = new PassThroughConnectionAdapter(
6168
_socketPipe,
6269
_connectionPipe,
63-
_connectionClosed);
70+
_connectionClosed,
71+
_loggerFactory);
6472
await StartAsync(cancellationToken)
6573
.ConfigureAwait(false);
6674
}
@@ -77,14 +85,16 @@ await StopAsync(cancellationToken)
7785
_connectionPipe,
7886
_sslStreamWrapperFactory,
7987
certificate,
80-
_connectionClosed);
88+
_connectionClosed,
89+
_loggerFactory);
8190
}
8291
catch
8392
{
8493
_activeCommunicationService = new PassThroughConnectionAdapter(
8594
_socketPipe,
8695
_connectionPipe,
87-
_connectionClosed);
96+
_connectionClosed,
97+
_loggerFactory);
8898
throw;
8999
}
90100
finally

src/FubarDev.FtpServer/ConnectionHandlers/SslStreamConnectionAdapter.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ internal class SslStreamConnectionAdapter : IFtpConnectionAdapter
2727
private readonly X509Certificate2 _certificate;
2828

2929
private readonly CancellationToken _connectionClosed;
30+
private readonly ILoggerFactory? _loggerFactory;
3031

3132
private SslCommunicationInfo? _info;
3233

@@ -35,13 +36,15 @@ public SslStreamConnectionAdapter(
3536
IDuplexPipe connectionPipe,
3637
ISslStreamWrapperFactory sslStreamWrapperFactory,
3738
X509Certificate2 certificate,
38-
CancellationToken connectionClosed)
39+
CancellationToken connectionClosed,
40+
ILoggerFactory? loggerFactory = null)
3941
{
4042
_socketPipe = socketPipe;
4143
_connectionPipe = connectionPipe;
4244
_sslStreamWrapperFactory = sslStreamWrapperFactory;
4345
_certificate = certificate;
4446
_connectionClosed = connectionClosed;
47+
_loggerFactory = loggerFactory;
4548
}
4649

4750
/// <inheritdoc />
@@ -62,15 +65,24 @@ public async Task StartAsync(CancellationToken cancellationToken)
6265
_socketPipe.Output);
6366
var sslStream = await _sslStreamWrapperFactory.WrapStreamAsync(rawStream, false, _certificate, cancellationToken)
6467
.ConfigureAwait(false);
68+
69+
var receiverLogger = _loggerFactory
70+
?.CreateLogger(typeof(SslStreamConnectionAdapter).FullName + ":Receiver");
6571
var receiverService = new NonClosingNetworkStreamReader(
6672
sslStream,
6773
_connectionPipe.Output,
6874
_socketPipe.Input,
69-
_connectionClosed);
75+
_connectionClosed,
76+
receiverLogger);
77+
78+
var transmitterLogger = _loggerFactory
79+
?.CreateLogger(typeof(SslStreamConnectionAdapter).FullName + ":Transmitter");
7080
var transmitterService = new NonClosingNetworkStreamWriter(
7181
sslStream,
7282
_connectionPipe.Input,
73-
_connectionClosed);
83+
_connectionClosed,
84+
transmitterLogger);
85+
7486
var info = new SslCommunicationInfo(transmitterService, receiverService, sslStream);
7587
_info = info;
7688

src/FubarDev.FtpServer/FtpConnection.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,8 @@ public FtpConnection(
206206
socketPipe,
207207
connectionPipe,
208208
sslStreamWrapperFactory,
209-
_cancellationTokenSource.Token),
209+
_cancellationTokenSource.Token,
210+
loggerFactory),
210211
applicationOutputPipe.Writer);
211212

212213
parentFeatures.Set<IConnectionFeature>(connectionFeature);
@@ -917,9 +918,9 @@ protected override async Task<int> ReadFromStreamAsync(byte[] buffer, int offset
917918
var readTask = Stream
918919
.ReadAsync(buffer, offset, length, cancellationToken);
919920

920-
// We ensure that this service can be closed ASAP with the help
921-
// of a Task.Delay.
922-
var resultTask = await Task.WhenAny(readTask, Task.Delay(-1, cancellationToken))
921+
var tcs = new TaskCompletionSource<object?>();
922+
using var registration = cancellationToken.Register(() => tcs.TrySetResult(null));
923+
var resultTask = await Task.WhenAny(readTask, tcs.Task)
923924
.ConfigureAwait(false);
924925
if (resultTask != readTask || cancellationToken.IsCancellationRequested)
925926
{

src/FubarDev.FtpServer/Networking/PassThroughService.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,13 @@ public override async Task StopAsync(CancellationToken cancellationToken)
4242
{
4343
await base.StopAsync(cancellationToken)
4444
.ConfigureAwait(false);
45-
await SafeFlushAsync(cancellationToken)
46-
.ConfigureAwait(false);
45+
46+
if (!IsPauseRequested)
47+
{
48+
await SafeFlushAsync(cancellationToken)
49+
.ConfigureAwait(false);
50+
}
51+
4752
await OnCloseAsync(_exception, cancellationToken)
4853
.ConfigureAwait(false);
4954
}

src/FubarDev.FtpServer/Networking/PausableFtpService.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ await _task
9494
.ConfigureAwait(false);
9595

9696
Status = FtpServiceStatus.Stopped;
97+
98+
if (IsPauseRequested)
99+
{
100+
await OnStoppedAsync(cancellationToken)
101+
.ConfigureAwait(false);
102+
}
97103
}
98104

99105
/// <inheritdoc />
@@ -119,6 +125,14 @@ await OnPauseRequestedAsync(cancellationToken)
119125

120126
await _task
121127
.ConfigureAwait(false);
128+
129+
Status = FtpServiceStatus.Paused;
130+
131+
if (IsStopRequested)
132+
{
133+
await OnPausedAsync(cancellationToken)
134+
.ConfigureAwait(false);
135+
}
122136
}
123137

124138
/// <inheritdoc />
@@ -169,24 +183,28 @@ protected abstract Task ExecuteAsync(
169183
protected virtual Task OnStopRequestingAsync(
170184
CancellationToken cancellationToken)
171185
{
186+
Logger?.LogTrace("STOP requesting");
172187
return Task.CompletedTask;
173188
}
174189

175190
protected virtual Task OnStopRequestedAsync(
176191
CancellationToken cancellationToken)
177192
{
193+
Logger?.LogTrace("STOP requested");
178194
return Task.CompletedTask;
179195
}
180196

181197
protected virtual Task OnPauseRequestingAsync(
182198
CancellationToken cancellationToken)
183199
{
200+
Logger?.LogTrace("PAUSE requesting");
184201
return Task.CompletedTask;
185202
}
186203

187204
protected virtual Task OnPauseRequestedAsync(
188205
CancellationToken cancellationToken)
189206
{
207+
Logger?.LogTrace("PAUSE requested");
190208
return Task.CompletedTask;
191209
}
192210

@@ -199,12 +217,14 @@ protected virtual Task OnContinueRequestingAsync(
199217
protected virtual Task OnPausedAsync(
200218
CancellationToken cancellationToken)
201219
{
220+
Logger?.LogTrace("PAUSED");
202221
return Task.CompletedTask;
203222
}
204223

205224
protected virtual Task OnStoppedAsync(
206225
CancellationToken cancellationToken)
207226
{
227+
Logger?.LogTrace("STOPPED");
208228
return Task.CompletedTask;
209229
}
210230

0 commit comments

Comments
 (0)