|
3 | 3 | using System.Net;
|
4 | 4 | using System.Net.Sockets;
|
5 | 5 | using System.Threading;
|
6 |
| -#if NET6_0_OR_GREATER == false |
7 | 6 | using System.Threading.Tasks;
|
8 |
| -#endif |
9 | 7 |
|
10 | 8 | using Renci.SshNet.Common;
|
11 | 9 |
|
@@ -151,11 +149,34 @@ public static int Read(Socket socket, byte[] buffer, int offset, int size, TimeS
|
151 | 149 | return totalBytesRead;
|
152 | 150 | }
|
153 | 151 |
|
154 |
| -#if NET6_0_OR_GREATER == false |
155 |
| - public static ValueTask<int> ReadAsync(Socket socket, byte[] buffer, CancellationToken cancellationToken) |
| 152 | + public static async ValueTask<int> ReadAsync(Socket socket, byte[] buffer, int offset, int size, CancellationToken cancellationToken) |
156 | 153 | {
|
157 |
| - return socket.ReceiveAsync(new ArraySegment<byte>(buffer, 0, buffer.Length), SocketFlags.None, cancellationToken); |
| 154 | + var totalBytesRead = 0; |
| 155 | + var totalBytesToRead = size; |
| 156 | + |
| 157 | + do |
| 158 | + { |
| 159 | + try |
| 160 | + { |
| 161 | + var bytesRead = await socket.ReceiveAsync(new ArraySegment<byte>(buffer, offset + totalBytesRead, totalBytesToRead - totalBytesRead), SocketFlags.None, cancellationToken).ConfigureAwait(false); |
| 162 | + if (bytesRead == 0) |
| 163 | + { |
| 164 | + return 0; |
| 165 | + } |
| 166 | + |
| 167 | + totalBytesRead += bytesRead; |
| 168 | + } |
| 169 | + catch (SocketException ex) when (ex.SocketErrorCode == SocketError.TimedOut) |
| 170 | + { |
| 171 | + throw new SshOperationTimeoutException(string.Format(CultureInfo.InvariantCulture, |
| 172 | + "Socket read operation has timed out after {0:F0} milliseconds.", |
| 173 | + socket.ReceiveTimeout), |
| 174 | + ex); |
| 175 | + } |
| 176 | + } |
| 177 | + while (totalBytesRead < totalBytesToRead); |
| 178 | + |
| 179 | + return totalBytesRead; |
158 | 180 | }
|
159 |
| -#endif |
160 | 181 | }
|
161 | 182 | }
|
0 commit comments