Skip to content
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

Rebased AsyncResult fix #1184

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 13 additions & 20 deletions src/Renci.SshNet/SftpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ public IEnumerable<ISftpFile> ListDirectory(string path, Action<int> listCallbac
{
CheckDisposed();

return InternalListDirectory(path, listCallback);
return InternalListDirectory(path, asyncResult: null, listCallback);
}

/// <summary>
Expand Down Expand Up @@ -666,12 +666,7 @@ public IAsyncResult BeginListDirectory(string path, AsyncCallback asyncCallback,
{
try
{
var result = InternalListDirectory(path, count =>
{
asyncResult.Update(count);

listCallback?.Invoke(count);
});
var result = InternalListDirectory(path, asyncResult, listCallback);

asyncResult.SetAsCompleted(result, completedSynchronously: false);
}
Expand Down Expand Up @@ -898,12 +893,7 @@ public IAsyncResult BeginDownloadFile(string path, Stream output, AsyncCallback
{
try
{
InternalDownloadFile(path, output, asyncResult, offset =>
{
asyncResult.Update(offset);

downloadCallback?.Invoke(offset);
});
InternalDownloadFile(path, output, asyncResult, downloadCallback);

asyncResult.SetAsCompleted(exception: null, completedSynchronously: false);
}
Expand Down Expand Up @@ -1131,11 +1121,7 @@ public IAsyncResult BeginUploadFile(Stream input, string path, bool canOverride,
{
try
{
InternalUploadFile(input, path, flags, asyncResult, offset =>
{
asyncResult.Update(offset);
uploadCallback?.Invoke(offset);
});
InternalUploadFile(input, path, flags, asyncResult, uploadCallback);

asyncResult.SetAsCompleted(exception: null, completedSynchronously: false);
}
Expand Down Expand Up @@ -2200,7 +2186,7 @@ private List<FileInfo> InternalSynchronizeDirectories(string sourcePath, string

#region Existing Files at The Destination

var destFiles = InternalListDirectory(destinationPath, listCallback: null);
var destFiles = InternalListDirectory(destinationPath, asyncResult: null, listCallback: null);
var destDict = new Dictionary<string, ISftpFile>();
foreach (var destFile in destFiles)
{
Expand Down Expand Up @@ -2268,13 +2254,14 @@ private List<FileInfo> InternalSynchronizeDirectories(string sourcePath, string
/// Internals the list directory.
/// </summary>
/// <param name="path">The path.</param>
/// <param name="asyncResult">An <see cref="IAsyncResult"/> that references the asynchronous request.</param>
/// <param name="listCallback">The list callback.</param>
/// <returns>
/// A list of files in the specfied directory.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="path" /> is <see langword="null"/>.</exception>
/// <exception cref="SshConnectionException">Client not connected.</exception>
private List<ISftpFile> InternalListDirectory(string path, Action<int> listCallback)
private List<ISftpFile> InternalListDirectory(string path, SftpListDirectoryAsyncResult asyncResult, Action<int> listCallback)
{
if (path is null)
{
Expand Down Expand Up @@ -2314,6 +2301,8 @@ private List<ISftpFile> InternalListDirectory(string path, Action<int> listCallb
f.Value));
}

asyncResult?.Update(result.Count);

// Call callback to report number of files read
if (listCallback is not null)
{
Expand Down Expand Up @@ -2380,6 +2369,8 @@ private void InternalDownloadFile(string path, Stream output, SftpDownloadAsyncR

totalBytesRead += (ulong) data.Length;

asyncResult?.Update(totalBytesRead);

if (downloadCallback is not null)
{
// Copy offset to ensure it's not modified between now and execution of callback
Expand Down Expand Up @@ -2452,6 +2443,8 @@ private void InternalUploadFile(Stream input, string path, Flags flags, SftpUplo
_ = Interlocked.Decrement(ref expectedResponses);
_ = responseReceivedWaitHandle.Set();

asyncResult?.Update(writtenBytes);

// Call callback to report number of bytes written
if (uploadCallback is not null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,15 +230,7 @@ public void Test_Sftp_Multiple_Async_Upload_And_Download_10Files_5MB_Each()
sftp.Disconnect();

Assert.IsTrue(hashMatches, "Hash does not match");
if (!uploadDownloadSizeOk)
{
// TODO https://github.com/sshnet/SSH.NET/issues/1253
Assert.Inconclusive("Uploaded and downloaded bytes should match, but test is not stable");
}
else
{
Assert.IsTrue(uploadDownloadSizeOk, "Uploaded and downloaded bytes does not match");
}
Assert.IsTrue(uploadDownloadSizeOk, "Uploaded and downloaded bytes does not match");
}
}

Expand Down