Native async (#1267)

This commit is contained in:
Wojciech Nagórski
2023-12-09 12:28:06 +01:00
committed by GitHub
parent 04178d8432
commit 6220beb83d
3 changed files with 13 additions and 4 deletions
@@ -325,10 +325,17 @@ namespace Renci.SshNet.Abstractions
return totalBytesRead;
}
public static Task<int> ReadAsync(Socket socket, byte[] buffer, int offset, int length, CancellationToken cancellationToken)
#if NET6_0_OR_GREATER
public static async Task<int> ReadAsync(Socket socket, byte[] buffer, CancellationToken cancellationToken)
{
return socket.ReceiveAsync(buffer, offset, length, cancellationToken);
return await socket.ReceiveAsync(buffer, SocketFlags.None, cancellationToken).ConfigureAwait(false);
}
#else
public static Task<int> ReadAsync(Socket socket, byte[] buffer, CancellationToken cancellationToken)
{
return socket.ReceiveAsync(buffer, 0, buffer.Length, cancellationToken);
}
#endif
public static void Send(Socket socket, byte[] data)
{
@@ -1,4 +1,5 @@
using System;
#if !NET6_0_OR_GREATER
using System;
using System.Net;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
@@ -130,3 +131,4 @@ namespace Renci.SshNet.Abstractions
}
}
}
#endif
@@ -187,7 +187,7 @@ namespace Renci.SshNet.Connection
// to be processed by subsequent invocations.
while (true)
{
var bytesRead = await SocketAbstraction.ReadAsync(socket, data, 0, data.Length, cancellationToken).ConfigureAwait(false);
var bytesRead = await SocketAbstraction.ReadAsync(socket, data, cancellationToken).ConfigureAwait(false);
if (bytesRead == 0)
{
throw new SshConnectionException("The connection was closed by the remote host.");