/// <summary> /// Event für das Ende des Datei-Downloads auslösen /// </summary> protected virtual void OnFileDownloadFinished() { FileDownloadFinished?.Invoke(this, new EventArgs()); }
/// <summary> /// Downloads a file. /// </summary> /// <param name="name">The name of the file.</param> /// <param name="bytesReadDelegate">A <see cref="FileDownloadBytesReadDelegate"/> which is called everytime an array of bytes has been read from the server. /></param> /// <param name="downloadFinished">A <see cref="FileDownloadFinished"/> which is called after the entire download has been completed.</param> /// <param name="taskCanceled">A <see cref="IOTaskIsCanceledDelegate"/> which is called before each reading cycle to cancel the download.</param> /// <param name="binaryMode">Indicates whether the file should be downloaded in binary or text mode.</param> public async void BeginDownloadFile(string name, FileDownloadBytesReadDelegate bytesReadDelegate, FileDownloadFinished downloadFinished, IOTaskIsCanceledDelegate taskCanceled, bool binaryMode = false) { IsIdled = false; checkConnected(); if (binaryMode) { EnterBinaryMode(); } if (_dataChannelSocket == null) { if (EnterPassiveModeAutomatic) { await EnterPassiveMode(); } } string answer; if (!TrySendCommandReadAnswer("RETR " + name, out answer)) { return; } if (answer == null) { return; } if (answer.Trim().StartsWith("425")) { _dataChannelSocket = null; if (EnterPassiveModeAutomatic) { await EnterPassiveMode(); if (!TrySendCommandReadAnswer("RETR " + name, out answer)) { return; } if (answer == null) { return; } } } if (answer.Trim().StartsWith("150") | answer.Trim().StartsWith("125")) { beginDownloadData(bytesReadDelegate, new FileDownloadFinished((ulong readbytes) => { EnterASCIIMode(); IsIdled = true; downloadFinished(readbytes); }), taskCanceled); } }
/// <summary> /// Internal method to download the content of the download's client connection. /// </summary> /// <param name="bytesReadDelegate">A <see cref="FileDownloadBytesReadDelegate"/> which is called everytime an array of bytes has been read from the server. /></param> /// <param name="downloadFinished">A <see cref="FileDownloadFinished"/> which is called after the entire download has been completed.</param> /// <param name="taskCanceled">A <see cref="IOTaskIsCanceledDelegate"/> which is called before each reading cycle to cancel the download.</param>> private void beginDownloadData(FileDownloadBytesReadDelegate bytesReadDelegate, FileDownloadFinished downloadFinished, IOTaskIsCanceledDelegate taskCanceled) { ulong totalReadBytes = 0; int readbytes = 1; byte[] buffer = new byte[10240]; while (readbytes > 0) { if (taskCanceled()) { break; } try { readbytes = _dataChannelSocketReader.Read(buffer, 0, 1024); } catch (Exception) { break; } finally { totalReadBytes += (ulong)readbytes; bytesReadDelegate(readbytes, totalReadBytes, buffer); } } _dataChannelSocket.Dispose(); _dataChannelSocket = null; string answer = string.Empty; try { answer = ReadAnswer(ClientType.ActiveClient); } catch (IOException) { throw; } downloadFinished(totalReadBytes); }
public async void BeginDownloadFile(string absoluteFileName, FileDownloadBytesReadDelegate bytesReadDelegate, FileDownloadFinished downloadFinished, IOTaskIsCanceledDelegate taskCanceled, bool binaryMode = false) { (await getIdledConnection()).BeginDownloadFile(absoluteFileName, bytesReadDelegate, downloadFinished, taskCanceled, binaryMode); }