-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(#109) XmppTcpConnection: cancellable TCP client connections
- Loading branch information
Showing
3 changed files
with
96 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using System; | ||
using System.Net; | ||
using System.Net.Sockets; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace SharpXMPP.Compat | ||
{ | ||
internal static class TcpClientEx | ||
{ | ||
private static async Task WithCancellation(this Task task, CancellationToken cancellationToken) | ||
{ | ||
// See https://devblogs.microsoft.com/pfxteam/how-do-i-cancel-non-cancelable-async-operations/ for details. | ||
var tcs = new TaskCompletionSource<bool>(); | ||
using (cancellationToken.Register(() => tcs.TrySetResult(true))) | ||
{ | ||
if (task != await Task.WhenAny(task, tcs.Task)) | ||
{ | ||
throw new OperationCanceledException(cancellationToken); | ||
} | ||
} | ||
} | ||
|
||
public static async Task ConnectWithCancellationAsync( | ||
this TcpClient tcpClient, | ||
IPAddress address, | ||
int port, | ||
CancellationToken cancellationToken) | ||
{ | ||
#if NET5_0_OR_GREATER | ||
await tcpClient.ConnectAsync(address, port, cancellationToken); | ||
#else | ||
// Unfortunately, only .NET 5+ supports TcpClient connection cancellation. We'll do the best effort here, | ||
// though. | ||
// | ||
// Old TcpClient uses Socket::BeginConnect under the covers, which is documented to be cancelled on Close(). | ||
// So, ideally, if the caller eventually disposes the client, then all the resources will be freed upon its | ||
// destruction. Which means we are free to just abandon the task in question. | ||
var task = tcpClient.ConnectAsync(address, port); | ||
await task.WithCancellation(cancellationToken); | ||
#endif | ||
} | ||
|
||
public static async Task ConnectWithCancellationAsync( | ||
this TcpClient tcpClient, | ||
IPAddress[] addresses, | ||
int port, | ||
CancellationToken cancellationToken) | ||
{ | ||
#if NET5_0_OR_GREATER | ||
await tcpClient.ConnectAsync(addresses, port, cancellationToken); | ||
#else | ||
// Unfortunately, only .NET 5+ supports TcpClient connection cancellation. We'll do the best effort here, | ||
// though. | ||
// | ||
// Old TcpClient uses Socket::BeginConnect under the covers, which is documented to be cancelled on Close(). | ||
// So, ideally, if the caller eventually disposes the client, then all the resources will be freed upon its | ||
// destruction. Which means we are free to just abandon the task in question. | ||
var task = tcpClient.ConnectAsync(addresses, port); | ||
await task.WithCancellation(cancellationToken); | ||
#endif | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters