|
1 |
| -#if !NET6_0_OR_GREATER |
| 1 | +#if !NET |
| 2 | +#if NETFRAMEWORK || NETSTANDARD2_0 |
2 | 3 | using System;
|
| 4 | +#endif |
3 | 5 | using System.Net;
|
4 | 6 | using System.Net.Sockets;
|
5 |
| -using System.Runtime.CompilerServices; |
6 | 7 | using System.Threading;
|
7 | 8 | using System.Threading.Tasks;
|
8 | 9 |
|
9 | 10 | namespace Renci.SshNet.Abstractions
|
10 | 11 | {
|
11 |
| - // Async helpers based on https://devblogs.microsoft.com/pfxteam/awaiting-socket-operations/ |
12 | 12 | internal static class SocketExtensions
|
13 | 13 | {
|
14 |
| - private sealed class AwaitableSocketAsyncEventArgs : SocketAsyncEventArgs, INotifyCompletion |
| 14 | + public static async Task ConnectAsync(this Socket socket, IPEndPoint remoteEndpoint, CancellationToken cancellationToken) |
15 | 15 | {
|
16 |
| - private static readonly Action SENTINEL = () => { }; |
| 16 | + cancellationToken.ThrowIfCancellationRequested(); |
17 | 17 |
|
18 |
| - private bool _isCancelled; |
19 |
| - private Action _continuationAction; |
| 18 | + TaskCompletionSource<object> tcs = new(TaskCreationOptions.RunContinuationsAsynchronously); |
20 | 19 |
|
21 |
| - public AwaitableSocketAsyncEventArgs() |
| 20 | + using var args = new SocketAsyncEventArgs |
22 | 21 | {
|
23 |
| - Completed += (sender, e) => SetCompleted(); |
24 |
| - } |
| 22 | + RemoteEndPoint = remoteEndpoint |
| 23 | + }; |
| 24 | + args.Completed += (_, _) => tcs.TrySetResult(null); |
25 | 25 |
|
26 |
| - public AwaitableSocketAsyncEventArgs ExecuteAsync(Func<SocketAsyncEventArgs, bool> func) |
| 26 | + if (socket.ConnectAsync(args)) |
27 | 27 | {
|
28 |
| - if (!func(this)) |
| 28 | +#if NETSTANDARD2_1 |
| 29 | + await using (cancellationToken.Register(() => |
| 30 | +#else |
| 31 | + using (cancellationToken.Register(() => |
| 32 | +#endif |
29 | 33 | {
|
30 |
| - SetCompleted(); |
31 |
| - } |
32 |
| - |
33 |
| - return this; |
34 |
| - } |
35 |
| - |
36 |
| - public void SetCompleted() |
37 |
| - { |
38 |
| - IsCompleted = true; |
39 |
| - |
40 |
| - var continuation = _continuationAction ?? Interlocked.CompareExchange(ref _continuationAction, SENTINEL, comparand: null); |
41 |
| - if (continuation is not null) |
| 34 | + if (tcs.TrySetCanceled(cancellationToken)) |
| 35 | + { |
| 36 | + socket.Dispose(); |
| 37 | + } |
| 38 | + }, |
| 39 | + useSynchronizationContext: false) |
| 40 | +#if NETSTANDARD2_1 |
| 41 | + .ConfigureAwait(false) |
| 42 | +#endif |
| 43 | + ) |
42 | 44 | {
|
43 |
| - continuation(); |
| 45 | + _ = await tcs.Task.ConfigureAwait(false); |
44 | 46 | }
|
45 | 47 | }
|
46 | 48 |
|
47 |
| - public void SetCancelled() |
| 49 | + if (args.SocketError != SocketError.Success) |
48 | 50 | {
|
49 |
| - _isCancelled = true; |
50 |
| - SetCompleted(); |
| 51 | + throw new SocketException((int) args.SocketError); |
51 | 52 | }
|
| 53 | + } |
52 | 54 |
|
53 |
| -#pragma warning disable S1144 // Unused private types or members should be removed |
54 |
| - public AwaitableSocketAsyncEventArgs GetAwaiter() |
55 |
| -#pragma warning restore S1144 // Unused private types or members should be removed |
56 |
| - { |
57 |
| - return this; |
58 |
| - } |
| 55 | +#if NETFRAMEWORK || NETSTANDARD2_0 |
| 56 | + public static async ValueTask<int> ReceiveAsync(this Socket socket, ArraySegment<byte> buffer, SocketFlags socketFlags, CancellationToken cancellationToken) |
| 57 | + { |
| 58 | + cancellationToken.ThrowIfCancellationRequested(); |
59 | 59 |
|
60 |
| - public bool IsCompleted { get; private set; } |
| 60 | + TaskCompletionSource<object> tcs = new(TaskCreationOptions.RunContinuationsAsynchronously); |
61 | 61 |
|
62 |
| - void INotifyCompletion.OnCompleted(Action continuation) |
63 |
| - { |
64 |
| - if (_continuationAction == SENTINEL || Interlocked.CompareExchange(ref _continuationAction, continuation, comparand: null) == SENTINEL) |
65 |
| - { |
66 |
| - // We have already completed; run continuation asynchronously |
67 |
| - _ = Task.Run(continuation); |
68 |
| - } |
69 |
| - } |
| 62 | + using var args = new SocketAsyncEventArgs(); |
| 63 | + args.SocketFlags = socketFlags; |
| 64 | + args.Completed += (_, _) => tcs.TrySetResult(null); |
| 65 | + args.SetBuffer(buffer.Array, buffer.Offset, buffer.Count); |
70 | 66 |
|
71 |
| -#pragma warning disable S1144 // Unused private types or members should be removed |
72 |
| - public void GetResult() |
73 |
| -#pragma warning restore S1144 // Unused private types or members should be removed |
| 67 | + if (socket.ReceiveAsync(args)) |
74 | 68 | {
|
75 |
| - if (_isCancelled) |
| 69 | + using (cancellationToken.Register(() => |
76 | 70 | {
|
77 |
| - throw new TaskCanceledException(); |
78 |
| - } |
79 |
| - |
80 |
| - if (!IsCompleted) |
81 |
| - { |
82 |
| - // We don't support sync/async |
83 |
| - throw new InvalidOperationException("The asynchronous operation has not yet completed."); |
84 |
| - } |
85 |
| - |
86 |
| - if (SocketError != SocketError.Success) |
| 71 | + if (tcs.TrySetCanceled(cancellationToken)) |
| 72 | + { |
| 73 | + socket.Dispose(); |
| 74 | + } |
| 75 | + }, |
| 76 | + useSynchronizationContext: false)) |
87 | 77 | {
|
88 |
| - throw new SocketException((int)SocketError); |
| 78 | + _ = await tcs.Task.ConfigureAwait(false); |
89 | 79 | }
|
90 | 80 | }
|
91 |
| - } |
92 | 81 |
|
93 |
| - public static async Task ConnectAsync(this Socket socket, IPEndPoint remoteEndpoint, CancellationToken cancellationToken) |
94 |
| - { |
95 |
| - cancellationToken.ThrowIfCancellationRequested(); |
96 |
| - |
97 |
| - using (var args = new AwaitableSocketAsyncEventArgs()) |
| 82 | + if (args.SocketError != SocketError.Success) |
98 | 83 | {
|
99 |
| - args.RemoteEndPoint = remoteEndpoint; |
100 |
| - |
101 |
| -#if NET || NETSTANDARD2_1_OR_GREATER |
102 |
| - await using (cancellationToken.Register(o => ((AwaitableSocketAsyncEventArgs)o).SetCancelled(), args, useSynchronizationContext: false).ConfigureAwait(continueOnCapturedContext: false)) |
103 |
| -#else |
104 |
| - using (cancellationToken.Register(o => ((AwaitableSocketAsyncEventArgs) o).SetCancelled(), args, useSynchronizationContext: false)) |
105 |
| -#endif // NET || NETSTANDARD2_1_OR_GREATER |
106 |
| - { |
107 |
| - await args.ExecuteAsync(socket.ConnectAsync); |
108 |
| - } |
| 84 | + throw new SocketException((int) args.SocketError); |
109 | 85 | }
|
| 86 | + |
| 87 | + return args.BytesTransferred; |
110 | 88 | }
|
111 | 89 |
|
112 |
| - public static async Task<int> ReceiveAsync(this Socket socket, byte[] buffer, int offset, int length, CancellationToken cancellationToken) |
| 90 | + public static async ValueTask<int> SendAsync(this Socket socket, byte[] buffer, SocketFlags socketFlags, CancellationToken cancellationToken) |
113 | 91 | {
|
114 | 92 | cancellationToken.ThrowIfCancellationRequested();
|
115 | 93 |
|
116 |
| - using (var args = new AwaitableSocketAsyncEventArgs()) |
117 |
| - { |
118 |
| - args.SetBuffer(buffer, offset, length); |
| 94 | + TaskCompletionSource<object> tcs = new(TaskCreationOptions.RunContinuationsAsynchronously); |
119 | 95 |
|
120 |
| -#if NET || NETSTANDARD2_1_OR_GREATER |
121 |
| - await using (cancellationToken.Register(o => ((AwaitableSocketAsyncEventArgs) o).SetCancelled(), args, useSynchronizationContext: false).ConfigureAwait(continueOnCapturedContext: false)) |
122 |
| -#else |
123 |
| - using (cancellationToken.Register(o => ((AwaitableSocketAsyncEventArgs) o).SetCancelled(), args, useSynchronizationContext: false)) |
124 |
| -#endif // NET || NETSTANDARD2_1_OR_GREATER |
| 96 | + using var args = new SocketAsyncEventArgs(); |
| 97 | + args.SocketFlags = socketFlags; |
| 98 | + args.Completed += (_, _) => tcs.TrySetResult(null); |
| 99 | + args.SetBuffer(buffer, 0, buffer.Length); |
| 100 | + |
| 101 | + if (socket.SendAsync(args)) |
| 102 | + { |
| 103 | + using (cancellationToken.Register(() => |
| 104 | + { |
| 105 | + if (tcs.TrySetCanceled(cancellationToken)) |
| 106 | + { |
| 107 | + socket.Dispose(); |
| 108 | + } |
| 109 | + }, |
| 110 | + useSynchronizationContext: false)) |
125 | 111 | {
|
126 |
| - await args.ExecuteAsync(socket.ReceiveAsync); |
| 112 | + _ = await tcs.Task.ConfigureAwait(false); |
127 | 113 | }
|
| 114 | + } |
128 | 115 |
|
129 |
| - return args.BytesTransferred; |
| 116 | + if (args.SocketError != SocketError.Success) |
| 117 | + { |
| 118 | + throw new SocketException((int) args.SocketError); |
130 | 119 | }
|
| 120 | + |
| 121 | + return args.BytesTransferred; |
131 | 122 | }
|
| 123 | +#endif // NETFRAMEWORK || NETSTANDARD2_0 |
132 | 124 | }
|
133 | 125 | }
|
134 | 126 | #endif
|
0 commit comments