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