Skip to content

Commit 737c3dc

Browse files
committed
Using channel to send responses
The WriteAsync functions are now obsolete.
1 parent 59f215a commit 737c3dc

15 files changed

Lines changed: 175 additions & 51 deletions

FubarDev.FtpServer.sln.DotSettings

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Copyright (c) Fubar Development Junker. All rights reserved.
4040
<s:String x:Key="/Default/CodeStyle/Naming/CppNaming/UserRules/=UNION/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aa_bb" /&gt;</s:String>
4141
<s:String x:Key="/Default/CodeStyle/Naming/CppNaming/UserRules/=UNION_005FMEMBER/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aa_bb" /&gt;</s:String>
4242
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=IAC/@EntryIndexedValue">IAC</s:String>
43+
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=IO/@EntryIndexedValue">IO</s:String>
4344
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=IP/@EntryIndexedValue">IP</s:String>
4445
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=PAM/@EntryIndexedValue">PAM</s:String>
4546
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateStaticReadonly/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="_" Suffix="" Style="aaBb" /&gt;</s:String>
@@ -91,4 +92,5 @@ Copyright (c) Fubar Development Junker. All rights reserved.&#xD;
9192
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateThisQualifierSettings/@EntryIndexedValue">True</s:Boolean>
9293
<s:Boolean x:Key="/Default/UserDictionary/Words/=abortable/@EntryIndexedValue">True</s:Boolean>
9394
<s:Boolean x:Key="/Default/UserDictionary/Words/=Fubar/@EntryIndexedValue">True</s:Boolean>
95+
<s:Boolean x:Key="/Default/UserDictionary/Words/=NLST/@EntryIndexedValue">True</s:Boolean>
9496
<s:Boolean x:Key="/Default/UserDictionary/Words/=PASV/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

src/FubarDev.FtpServer.Abstractions/Features/IConnectionFeature.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// </copyright>
44

55
using System.Net;
6+
using System.Threading.Channels;
7+
68
using JetBrains.Annotations;
79

810
namespace FubarDev.FtpServer.Features
@@ -20,5 +22,10 @@ public interface IConnectionFeature
2022
/// </summary>
2123
[NotNull]
2224
Address RemoteAddress { get; }
25+
26+
/// <summary>
27+
/// Gets the response writer.
28+
/// </summary>
29+
ChannelWriter<IFtpResponse> ResponseWriter { get; }
2330
}
2431
}

src/FubarDev.FtpServer.Abstractions/FtpConnectionData.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ private class RestCommandFeature : IRestCommandFeature
271271
/// </summary>
272272
private class RenameCommandFeature : IRenameCommandFeature
273273
{
274-
public RenameCommandFeature(SearchResult<IUnixFileSystemEntry> renameFrom)
274+
public RenameCommandFeature([NotNull] SearchResult<IUnixFileSystemEntry> renameFrom)
275275
{
276276
RenameFrom = renameFrom;
277277
}

src/FubarDev.FtpServer.Abstractions/FubarDev.FtpServer.Abstractions.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
</ItemGroup>
2222
<ItemGroup>
2323
<PackageReference Include="NGettext" Version="0.6.3" />
24+
<PackageReference Include="System.Threading.Channels" Version="4.5.0" />
2425
</ItemGroup>
2526
<Import Project="../../PackageLibrary.props" />
2627
</Project>

src/FubarDev.FtpServer.Abstractions/IFtpConnection.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ public interface IFtpConnection : IConnectionFeature, IDisposable
104104
/// <param name="response">The response to write to the client.</param>
105105
/// <param name="cancellationToken">The cancellation token.</param>
106106
/// <returns>The task.</returns>
107+
[Obsolete("Use the IConnectionFeature.ResponseWriter instead.")]
108+
[NotNull]
107109
Task WriteAsync([NotNull] IFtpResponse response, CancellationToken cancellationToken);
108110

109111
/// <summary>
@@ -112,12 +114,16 @@ public interface IFtpConnection : IConnectionFeature, IDisposable
112114
/// <param name="response">The response to write to the client.</param>
113115
/// <param name="cancellationToken">The cancellation token.</param>
114116
/// <returns>The task.</returns>
117+
[Obsolete("Use the IConnectionFeature.ResponseWriter instead.")]
118+
[NotNull]
115119
Task WriteAsync([NotNull] string response, CancellationToken cancellationToken);
116120

117121
/// <summary>
118122
/// Creates a response socket for e.g. LIST/NLST.
119123
/// </summary>
120124
/// <returns>The data connection.</returns>
125+
[NotNull]
126+
[ItemNotNull]
121127
Task<TcpClient> CreateResponseSocket();
122128

123129
/// <summary>

src/FubarDev.FtpServer.Commands/CommandExtensions/SiteBlstCommandExtension.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ private Task<IFtpResponse> SendBlstDirectly()
9696

9797
private async Task<IFtpResponse> SendBlstWithDataConnection(CancellationToken cancellationToken)
9898
{
99-
await Connection.WriteAsync(new FtpResponse(150, T("Opening data connection.")), cancellationToken).ConfigureAwait(false);
99+
var connFeature = Connection.Features.Get<IConnectionFeature>();
100+
await connFeature.ResponseWriter.WriteAsync(new FtpResponse(150, T("Opening data connection.")), cancellationToken).ConfigureAwait(false);
100101

101102
return await Connection.SendResponseAsync(
102103
ExecuteSend,

src/FubarDev.FtpServer.Commands/CommandHandlers/AppeCommandHandler.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,10 @@ public override async Task<IFtpResponse> Process(FtpCommand command, Cancellatio
7676
return new FtpResponse(553, T("File name not allowed."));
7777
}
7878

79-
await Connection.WriteAsync(new FtpResponse(150, T("Opening connection for data transfer.")), cancellationToken).ConfigureAwait(false);
79+
var connFeature = Connection.Features.Get<IConnectionFeature>();
80+
await connFeature.ResponseWriter
81+
.WriteAsync(new FtpResponse(150, T("Opening connection for data transfer.")), cancellationToken)
82+
.ConfigureAwait(false);
8083

8184
return await Connection
8285
.SendResponseAsync(client => ExecuteSend(client, fileInfo, restartPosition, cancellationToken))

src/FubarDev.FtpServer.Commands/CommandHandlers/ListCommandHandler.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ public ListCommandHandler(
5959
/// <inheritdoc/>
6060
public override async Task<IFtpResponse> Process(FtpCommand command, CancellationToken cancellationToken)
6161
{
62-
await Connection.WriteAsync(new FtpResponse(150, T("Opening data connection.")), cancellationToken).ConfigureAwait(false);
62+
var connFeature = Connection.Features.Get<IConnectionFeature>();
63+
await connFeature.ResponseWriter
64+
.WriteAsync(new FtpResponse(150, T("Opening data connection.")), cancellationToken)
65+
.ConfigureAwait(false);
6366

6467
return await Connection.SendResponseAsync(
6568
client => ExecuteSend(client, command, cancellationToken),

src/FubarDev.FtpServer.Commands/CommandHandlers/MlstCommandHandler.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,10 @@ private async Task<IFtpResponse> ProcessMlsdAsync(FtpCommand command, Cancellati
157157
}
158158
}
159159

160-
await Connection.WriteAsync(new FtpResponse(150, T("Opening data connection.")), cancellationToken).ConfigureAwait(false);
160+
var connFeature = Connection.Features.Get<IConnectionFeature>();
161+
await connFeature.ResponseWriter
162+
.WriteAsync(new FtpResponse(150, T("Opening data connection.")), cancellationToken)
163+
.ConfigureAwait(false);
161164

162165
var authInfoFeature = Connection.Features.Get<IAuthorizationInformationFeature>();
163166
var factsFeature = Connection.Features.Get<IMlstFactsFeature>() ?? CreateMlstFactsFeature();

src/FubarDev.FtpServer.Commands/CommandHandlers/PasvCommandHandler.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,18 +99,19 @@ public override async Task<IFtpResponse> Process(FtpCommand command, Cancellatio
9999
{
100100
var address = listener.PasvEndPoint.Address;
101101

102+
var connFeature = Connection.Features.Get<IConnectionFeature>();
102103
var localPort = listener.PasvEndPoint.Port;
103104
if (isEpsv || address.AddressFamily == AddressFamily.InterNetworkV6)
104105
{
105106
var listenerAddress = new Address(localPort);
106-
await Connection.WriteAsync(
107+
await connFeature.ResponseWriter.WriteAsync(
107108
new FtpResponse(229, T("Entering Extended Passive Mode ({0}).", listenerAddress)),
108109
cancellationToken).ConfigureAwait(false);
109110
}
110111
else
111112
{
112113
var listenerAddress = new Address(address.ToString(), localPort);
113-
await Connection.WriteAsync(
114+
await connFeature.ResponseWriter.WriteAsync(
114115
new FtpResponse(227, T("Entering Passive Mode ({0}).", listenerAddress)),
115116
cancellationToken).ConfigureAwait(false);
116117
}

0 commit comments

Comments
 (0)