public void SftpDownloadAsyncResultConstructorTest() { AsyncCallback asyncCallback = null; // TODO: Initialize to an appropriate value object state = null; // TODO: Initialize to an appropriate value SftpDownloadAsyncResult target = new SftpDownloadAsyncResult(asyncCallback, state); Assert.Inconclusive("TODO: Implement code to verify target"); }
public void Update() { var target = new SftpDownloadAsyncResult(null, null); target.Update(123); target.Update(431); Assert.AreEqual(431UL, target.DownloadedBytes); }
public void SftpDownloadAsyncResultConstructorTest() { const AsyncCallback asyncCallback = null; var state = new object(); var target = new SftpDownloadAsyncResult(asyncCallback, state); Assert.IsFalse(target.CompletedSynchronously); Assert.IsFalse(target.EndInvokeCalled); Assert.IsFalse(target.IsCompleted); Assert.IsFalse(target.IsDownloadCanceled); Assert.AreEqual(0UL, target.DownloadedBytes); Assert.AreSame(state, target.AsyncState); }
public void EndInvoke_CompletedWithException() { object state = "STATE"; Exception exception = new IOException(); var target = new SftpDownloadAsyncResult(null, state); target.SetAsCompleted(exception, true); try { target.EndInvoke(); Assert.Fail(); } catch (IOException ex) { Assert.AreSame(exception, ex); } }
public void SetAsCompleted_Exception_CompletedSynchronously() { var downloadCompleted = new ManualResetEvent(false); object state = "STATE"; Exception exception = new IOException(); IAsyncResult callbackResult = null; var target = new SftpDownloadAsyncResult(asyncResult => { downloadCompleted.Set(); callbackResult = asyncResult; }, state); target.SetAsCompleted(exception, true); Assert.AreSame(target, callbackResult); Assert.IsFalse(target.IsDownloadCanceled); Assert.IsTrue(target.IsCompleted); Assert.IsTrue(target.CompletedSynchronously); Assert.IsTrue(downloadCompleted.WaitOne(TimeSpan.Zero)); }
/// <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; }
/// <summary> /// Internals the download file. /// </summary> /// <param name="path">The path.</param> /// <param name="output">The output.</param> /// <param name="asyncResult">An <see cref="IAsyncResult"/> that references the asynchronous request.</param> /// <param name="downloadCallback">The download callback.</param> /// <exception cref="ArgumentNullException"><paramref name="output" /> is <b>null</b>.</exception> /// <exception cref="ArgumentException"><paramref name="path" /> is <b>null</b> or contains whitespace.</exception> /// <exception cref="SshConnectionException">Client not connected.</exception> private void InternalDownloadFile(string path, Stream output, SftpDownloadAsyncResult asyncResult, Action<ulong> downloadCallback) { if (output == null) throw new ArgumentNullException("output"); if (path.IsNullOrWhiteSpace()) throw new ArgumentException("path"); if (_sftpSession == null) throw new SshConnectionException("Client not connected."); var fullPath = _sftpSession.GetCanonicalPath(path); var handle = _sftpSession.RequestOpen(fullPath, Flags.Read); ulong offset = 0; var optimalReadLength = _sftpSession.CalculateOptimalReadLength(_bufferSize); var data = _sftpSession.RequestRead(handle, offset, optimalReadLength); // Read data while available while (data.Length > 0) { // Cancel download if (asyncResult != null && asyncResult.IsDownloadCanceled) break; output.Write(data, 0, data.Length); output.Flush(); offset += (ulong)data.Length; // Call callback to report number of bytes read if (downloadCallback != null) { // Execute callback on different thread ExecuteThread(() => { downloadCallback(offset); }); } data = _sftpSession.RequestRead(handle, offset, optimalReadLength); } _sftpSession.RequestClose(handle); }
/// <summary> /// Begins an asynchronous file downloading into the stream. /// </summary> /// <param name="path">The path.</param> /// <param name="output">The output.</param> /// <param name="handler">The object that handles callbacks for transferred data.</param> /// <param name="state">A user-provided object that distinguishes this particular asynchronous write request from other requests.</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="Renci.SshNet.Common.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="Renci.SshNet.Common.SshException">A SSH error where <see cref="P:SshException.Message"/> is the message from the remote host.</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, IFileTransferHandler handler, object state) { if (path.IsNullOrWhiteSpace()) throw new ArgumentException("path"); if (output == null) throw new ArgumentNullException("output"); // Ensure that connection is established. this.EnsureConnection(); AsyncCallback callback; if (handler != null) { callback = (r) => handler.TransferCompleted(); } else { callback = (r) => { }; } var asyncResult = new SftpDownloadAsyncResult(callback, state); this.ExecuteThread(() => { try { this.InternalDownloadFile(path, output, handler); asyncResult.SetAsCompleted(null, false); } catch (Exception exp) { asyncResult.SetAsCompleted(exp, false); } }); return asyncResult; }
/// <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> /// <returns>An <see cref="IAsyncResult"/> that references the asynchronous operation.</returns> /// <exception cref="ArgumentNullException"><paramref name="output"/> is null.</exception> /// <exception cref="ArgumentException"><paramref name="path"/> is null or contains whitespace characters.</exception> /// <exception cref="SshConnectionException">Client is not connected.</exception> /// <exception cref="Renci.SshNet.Common.SftpPermissionDeniedException">Permission to perform the operation was denied by the remote host -or- a SSH command was denied by the server.</exception> /// <exception cref="Renci.SshNet.Common.SshException">A SSH error where <see cref="P:SshException.Message"/> is the message from the remote host.</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) { if (path.IsNullOrWhiteSpace()) throw new ArgumentException("path"); if (output == null) throw new ArgumentNullException("output"); // Ensure that connection is established. this.EnsureConnection(); var asyncResult = new SftpDownloadAsyncResult(asyncCallback, state); this.ExecuteThread(() => { try { this.InternalDownloadFile(path, output, asyncResult); 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); }