Skip to content

Remove custom cancellation token handling code, rely on Stream.ReadAsync to handle it instead #141

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 0 additions & 32 deletions src/FubarDev.FtpServer/FtpConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -932,38 +932,6 @@ public ConnectionClosingNetworkStreamReader(
_connectionClosedCts = connectionClosedCts;
}

/// <inheritdoc />
protected override async Task<int> ReadFromStreamAsync(byte[] buffer, int offset, int length, CancellationToken cancellationToken)
{
var readTask = Stream
.ReadAsync(buffer, offset, length, cancellationToken);

var tcs = new TaskCompletionSource<object?>();
using var registration = cancellationToken.Register(() => tcs.TrySetResult(null));
var resultTask = await Task.WhenAny(readTask, tcs.Task)
.ConfigureAwait(false);

if (cancellationToken.IsCancellationRequested)
{
Logger?.LogTrace("Cancelled through CancellationToken");
return 0;
}

if (resultTask != readTask)
{
Logger?.LogTrace("Cancelled through Task.Delay");
return 0;
}

#if NETSTANDARD1_3
return await readTask.ConfigureAwait(false);
#else
var result = readTask.Result;
readTask.Dispose();
return result;
#endif
}

/// <inheritdoc />
protected override async Task OnCloseAsync(Exception? exception, CancellationToken cancellationToken)
{
Expand Down