-
-
Notifications
You must be signed in to change notification settings - Fork 940
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enable list directory async for net framework #1206
Changes from 3 commits
db6897d
68f1640
231e747
aedc32f
2beb0c6
27bce46
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using System.Diagnostics; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Renci.SshNet.Common; | ||
using Renci.SshNet.Tests.Properties; | ||
|
||
namespace Renci.SshNet.Tests.Classes | ||
{ | ||
/// <summary> | ||
/// Implementation of the SSH File Transfer Protocol (SFTP) over SSH. | ||
/// </summary> | ||
public partial class SftpClientTest | ||
{ | ||
[TestMethod] | ||
[TestCategory("Sftp")] | ||
[ExpectedException(typeof(SshConnectionException))] | ||
public async Task Test_Sftp_ListDirectoryAsync_Without_ConnectingAsync() | ||
{ | ||
using (var sftp = new SftpClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD)) | ||
{ | ||
await foreach (var file in sftp.ListDirectoryAsync(".", CancellationToken.None)) | ||
{ | ||
Debug.WriteLine(file.FullName); | ||
} | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,9 +10,7 @@ | |
using Renci.SshNet.Common; | ||
using Renci.SshNet.Sftp; | ||
using System.Threading.Tasks; | ||
#if FEATURE_ASYNC_ENUMERABLE | ||
using System.Runtime.CompilerServices; | ||
#endif | ||
|
||
namespace Renci.SshNet | ||
{ | ||
|
@@ -587,7 +585,6 @@ public IEnumerable<ISftpFile> ListDirectory(string path, Action<int> listCallbac | |
return InternalListDirectory(path, listCallback); | ||
} | ||
|
||
#if FEATURE_ASYNC_ENUMERABLE | ||
/// <summary> | ||
/// Asynchronously enumerates the files in remote directory. | ||
/// </summary> | ||
|
@@ -646,7 +643,6 @@ public async IAsyncEnumerable<ISftpFile> ListDirectoryAsync(string path, [Enumer | |
await _sftpSession.RequestCloseAsync(handle, cancellationToken).ConfigureAwait(false); | ||
} | ||
} | ||
#endif //FEATURE_ASYNC_ENUMERABLE | ||
|
||
/// <summary> | ||
/// Begins an asynchronous operation of retrieving list of files in remote directory. | ||
|
@@ -1133,10 +1129,10 @@ public IAsyncResult BeginUploadFile(Stream input, string path, bool canOverride, | |
try | ||
{ | ||
InternalUploadFile(input, path, flags, asyncResult, offset => | ||
{ | ||
asyncResult.Update(offset); | ||
uploadCallback?.Invoke(offset); | ||
}); | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please undo formatting changes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
asyncResult.Update(offset); | ||
uploadCallback?.Invoke(offset); | ||
}); | ||
|
||
asyncResult.SetAsCompleted(exception: null, completedSynchronously: false); | ||
} | ||
|
@@ -1613,7 +1609,7 @@ public Task<SftpFileStream> OpenAsync(string path, FileMode mode, FileAccess acc | |
|
||
cancellationToken.ThrowIfCancellationRequested(); | ||
|
||
return SftpFileStream.OpenAsync(_sftpSession, path, mode, access, (int)_bufferSize, cancellationToken); | ||
return SftpFileStream.OpenAsync(_sftpSession, path, mode, access, (int) _bufferSize, cancellationToken); | ||
} | ||
|
||
/// <summary> | ||
|
@@ -2148,18 +2144,18 @@ public IAsyncResult BeginSynchronizeDirectories(string sourcePath, string destin | |
var asyncResult = new SftpSynchronizeDirectoriesAsyncResult(asyncCallback, state); | ||
|
||
ThreadAbstraction.ExecuteThread(() => | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please indent block. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
try | ||
{ | ||
try | ||
{ | ||
var result = InternalSynchronizeDirectories(sourcePath, destinationPath, searchPattern, asyncResult); | ||
var result = InternalSynchronizeDirectories(sourcePath, destinationPath, searchPattern, asyncResult); | ||
|
||
asyncResult.SetAsCompleted(result, completedSynchronously: false); | ||
} | ||
catch (Exception exp) | ||
{ | ||
asyncResult.SetAsCompleted(exp, completedSynchronously: false); | ||
} | ||
}); | ||
asyncResult.SetAsCompleted(result, completedSynchronously: false); | ||
} | ||
catch (Exception exp) | ||
{ | ||
asyncResult.SetAsCompleted(exp, completedSynchronously: false); | ||
} | ||
}); | ||
|
||
return asyncResult; | ||
} | ||
|
@@ -2445,20 +2441,20 @@ private void InternalUploadFile(Stream input, string path, Flags flags, SftpUplo | |
var writtenBytes = offset + (ulong) bytesRead; | ||
|
||
_sftpSession.RequestWrite(handle, offset, buffer, offset: 0, bytesRead, wait: null, s => | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please indent block. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
if (s.StatusCode == StatusCodes.Ok) | ||
{ | ||
if (s.StatusCode == StatusCodes.Ok) | ||
_ = Interlocked.Decrement(ref expectedResponses); | ||
_ = responseReceivedWaitHandle.Set(); | ||
|
||
// Call callback to report number of bytes written | ||
if (uploadCallback is not null) | ||
{ | ||
_ = Interlocked.Decrement(ref expectedResponses); | ||
_ = responseReceivedWaitHandle.Set(); | ||
|
||
// Call callback to report number of bytes written | ||
if (uploadCallback is not null) | ||
{ | ||
// Execute callback on different thread | ||
ThreadAbstraction.ExecuteThread(() => uploadCallback(writtenBytes)); | ||
} | ||
// Execute callback on different thread | ||
ThreadAbstraction.ExecuteThread(() => uploadCallback(writtenBytes)); | ||
} | ||
}); | ||
} | ||
}); | ||
|
||
_ = Interlocked.Increment(ref expectedResponses); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please make it conditional so that it only applies to TFMs that do not support IAsyncEnumerable<T> out-of-the-box.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your comment, good idea! Done.