Skip to content

Commit

Permalink
Add SshCommand.ExecuteAsync (#1426)
Browse files Browse the repository at this point in the history
* Add SshCommand.ExecuteAsync

After the previous change (#1423), this basically entails swapping out the
IAsyncResult for a TaskCompletionSource and hooking up the cancellation/timeout
logic.

As with the prior Begin/End implementation, the initiation of the command
(SendExecRequest) happens synchronously, so there's a bit of room for improvement
there, but otherwise it is the Task-based async that we know and like.

I chose to make it void (Task)- returning instead of string like in the existing
overloads, so that OutputStream is not automatically consumed (and encoded as a
string) when that may not be desired. As in #650, I was initially considering
changing the other overloads to be void-returning as well, but decided that it was
not worth the break since most people will probably want to change over to
ExecuteAsync anyway.

* Update examples

---------

Co-authored-by: Wojciech Nagórski <wojtpl2@gmail.com>
  • Loading branch information
Rob-Hague and WojciechNagorski committed Jun 19, 2024
1 parent 919af75 commit 3bc5684
Show file tree
Hide file tree
Showing 9 changed files with 259 additions and 274 deletions.
7 changes: 4 additions & 3 deletions docfx/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,17 @@ using (var client = new SshClient("sftp.foo.com", "guest", "pwd"))
// Make the server echo back the input file with "cat"
using (SshCommand command = client.CreateCommand("cat"))
{
IAsyncResult asyncResult = command.BeginExecute();
Task executeTask = command.ExecuteAsync(CancellationToken.None);

using (Stream inputStream = command.CreateInputStream())
{
inputStream.Write("Hello World!"u8);
}

string result = command.EndExecute(asyncResult);
await executeTask;

Console.WriteLine(result); // "Hello World!"
Console.WriteLine(command.ExitStatus); // 0
Console.WriteLine(command.Result); // "Hello World!"
}
}
```
8 changes: 8 additions & 0 deletions src/Renci.SshNet/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,11 @@ dotnet_diagnostic.CA5350.severity = none
# CA5351: Do Not Use Broken Cryptographic Algorithms
# https://learn.microsoft.com/en-ca/dotnet/fundamentals/code-analysis/quality-rules/ca5351
dotnet_diagnostic.CA5351.severity = none

# MA0040: Forward the CancellationToken parameter to methods that take one
# Partial/noisy duplicate of CA2016
dotnet_diagnostic.MA0040.severity = none

# MA0042: Do not use blocking calls in an async method
# duplicate of CA1849
dotnet_diagnostic.MA0042.severity = none
60 changes: 0 additions & 60 deletions src/Renci.SshNet/CommandAsyncResult.cs

This file was deleted.

Loading

0 comments on commit 3bc5684

Please sign in to comment.