public void Update() { var target = new SftpDownloadAsyncResult(null, null); target.Update(123); target.Update(431); Assert.AreEqual(431UL, target.DownloadedBytes); }
/// <summary> /// Begins an asynchronous file downloading into the stream. /// </summary> /// <param name="path">The path.</param> /// <param name="output">The output.</param> /// <param name="asyncCallback">The method to be called when the asynchronous write operation is completed.</param> /// <param name="state">A user-provided object that distinguishes this particular asynchronous write request from other requests.</param> /// <param name="downloadCallback">The download callback.</param> /// <returns> /// An <see cref="IAsyncResult" /> that references the asynchronous operation. /// </returns> /// <exception cref="ArgumentNullException"><paramref name="output" /> is <b>null</b>.</exception> /// <exception cref="ArgumentException"><paramref name="path" /> is <b>null</b> or contains whitespace characters.</exception> /// <exception cref="SshConnectionException">Client is not connected.</exception> /// <exception cref="SftpPermissionDeniedException">Permission to perform the operation was denied by the remote host. <para>-or-</para> A SSH command was denied by the server.</exception> /// <exception cref="SshException">A SSH error where <see cref="P:System.Exception.Message" /> is the message from the remote host.</exception> /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception> /// <remarks> /// Method calls made by this method to <paramref name="output" />, may under certain conditions result in exceptions thrown by the stream. /// </remarks> public IAsyncResult BeginDownloadFile(string path, Stream output, AsyncCallback asyncCallback, object state, Action<ulong> downloadCallback = null) { CheckDisposed(); if (path.IsNullOrWhiteSpace()) throw new ArgumentException("path"); if (output == null) throw new ArgumentNullException("output"); var asyncResult = new SftpDownloadAsyncResult(asyncCallback, state); ExecuteThread(() => { try { InternalDownloadFile(path, output, asyncResult, offset => { asyncResult.Update(offset); if (downloadCallback != null) { downloadCallback(offset); } }); asyncResult.SetAsCompleted(null, false); } catch (Exception exp) { asyncResult.SetAsCompleted(exp, false); } }); return asyncResult; }
private void InternalDownloadFile(string path, Stream output, SftpDownloadAsyncResult asynchResult) { if (output == null) throw new ArgumentNullException("output"); if (path.IsNullOrWhiteSpace()) throw new ArgumentException("path"); // Ensure that connection is established. this.EnsureConnection(); var fullPath = this._sftpSession.GetCanonicalPath(path); var handle = this._sftpSession.RequestOpen(fullPath, Flags.Read); ulong offset = 0; var data = this._sftpSession.RequestRead(handle, offset, this.BufferSize); // Read data while available while (data.Length > 0) { output.Write(data, 0, data.Length); output.Flush(); offset += (ulong)data.Length; // Call callback to report number of bytes read if (asynchResult != null) { asynchResult.Update(offset); } data = this._sftpSession.RequestRead(handle, offset, this.BufferSize); } this._sftpSession.RequestClose(handle); }