Skip to content

Commit 6220beb

Browse files
Native async (#1267)
1 parent 04178d8 commit 6220beb

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

src/Renci.SshNet/Abstractions/SocketAbstraction.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,10 +325,17 @@ public static int Read(Socket socket, byte[] buffer, int offset, int size, TimeS
325325
return totalBytesRead;
326326
}
327327

328-
public static Task<int> ReadAsync(Socket socket, byte[] buffer, int offset, int length, CancellationToken cancellationToken)
328+
#if NET6_0_OR_GREATER
329+
public static async Task<int> ReadAsync(Socket socket, byte[] buffer, CancellationToken cancellationToken)
329330
{
330-
return socket.ReceiveAsync(buffer, offset, length, cancellationToken);
331+
return await socket.ReceiveAsync(buffer, SocketFlags.None, cancellationToken).ConfigureAwait(false);
331332
}
333+
#else
334+
public static Task<int> ReadAsync(Socket socket, byte[] buffer, CancellationToken cancellationToken)
335+
{
336+
return socket.ReceiveAsync(buffer, 0, buffer.Length, cancellationToken);
337+
}
338+
#endif
332339

333340
public static void Send(Socket socket, byte[] data)
334341
{

src/Renci.SshNet/Abstractions/SocketExtensions.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
#if !NET6_0_OR_GREATER
2+
using System;
23
using System.Net;
34
using System.Net.Sockets;
45
using System.Runtime.CompilerServices;
@@ -130,3 +131,4 @@ public static async Task<int> ReceiveAsync(this Socket socket, byte[] buffer, in
130131
}
131132
}
132133
}
134+
#endif

src/Renci.SshNet/Connection/ProtocolVersionExchange.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ private static async Task<string> SocketReadLineAsync(Socket socket, List<byte>
187187
// to be processed by subsequent invocations.
188188
while (true)
189189
{
190-
var bytesRead = await SocketAbstraction.ReadAsync(socket, data, 0, data.Length, cancellationToken).ConfigureAwait(false);
190+
var bytesRead = await SocketAbstraction.ReadAsync(socket, data, cancellationToken).ConfigureAwait(false);
191191
if (bytesRead == 0)
192192
{
193193
throw new SshConnectionException("The connection was closed by the remote host.");

0 commit comments

Comments
 (0)