private async void ExecuteSpeedTest(SpeedTestSource source, Server server = null) { try { SpeedGrid.IsEnabled = false; DownloadSpeed speed = null; if (source == SpeedTestSource.Speedtest) { speed = await SpeedTestClient.GetDownloadSpeed(server, SpeedTestUnit.KiloBitsPerSecond); } else { speed = await FastClient.GetDownloadSpeed(SpeedTestUnit.KiloBitsPerSecond); } var message = $"Source: {speed.Source} Download Speed: {speed?.Speed} {speed.Unit}"; if (speed?.Server?.Id != null) { message += $" (Server Id = {speed?.Server?.Id})"; } ShowMessage(message); SpeedGrid.IsEnabled = true; } catch (System.Exception ex) { ShowMessage(ex.Message); SpeedGrid.IsEnabled = true; } }
protected override void StartNextDownload() { try { Directory.CreateDirectory(DownloadsController.CurrentDownload.Destination.FullPath); //Http is way more simple than FTP, since it has a built in background worker, no authentication and no folders using (client = new CustomWebClient()) { speed = new DownloadSpeed(client); speed.Start(); client.Credentials = Credentials; client.DownloadProgressChanged += CustomWebClientDownloadProgressChanged; client.DownloadFileCompleted += CustomWebClientDownloadCompleted; OnDownloadStarted(); //downloads the file and save in the selected destination client.DownloadFileAsync(new Uri(DownloadsController.CurrentDownload.RemoteFileInfo.Url), UpdateFileNameWithDistinguishedName()); } } catch (Exception e) { DownloadsController.CurrentDownload.ChangeState(DownloadState.Error, true); OnProcessError(new DownloadErrorEventArgs(ErrorType.GeneralErrorOnDownload, DownloadsController.CurrentDownload.RemoteFileInfo.FileFullName, e)); } finally { //the progress of the download list has changed OnOverallProgressChanged(); } }
public void DownloadSpeedWithoutData_EstimatedDuration_ReturnsInfinity() { var startDate = DateTime.Now; var downloadSpeed = new DownloadSpeed(startDate); Assert.AreEqual(TimeSpan.FromHours(1), downloadSpeed.EstimatedDuration); }
/// <summary> /// Instantiates a new instance of the DownloadTask.DownloadProgress class. /// </summary> /// <param name="percent">Percent completed.</param> /// <param name="bytes">Number of bytes downloaded so far.</param> /// <param name="totalBytes">Total number of bytes to be downloaded, i.e. the file size.</param> /// <param name="speed">Download speed.</param> /// <param name="eta">Estimated time of arrival (completion).</param> public DownloadProgress(double percent, FileSize bytes, FileSize totalBytes, DownloadSpeed speed, TimeSpan eta, TimeSpan elapsed) { _percentComplete = percent; _bytesDownloaded = bytes; _totalBytes = totalBytes; _speed = speed; _eta = eta; _elapsed = elapsed; }
public void DownloadSpeed_SettingsCapacity_SetsNewCapacity() { var startDate = DateTime.Now; var downloadSpeed = new DownloadSpeed(startDate); downloadSpeed.Capacity = 10; Assert.AreEqual(10, downloadSpeed.Capacity); }
public void DownloadSpeed() { const int totalBytes = 10000; var startDate = DateTime.Now; var downloadSpeed = new DownloadSpeed(startDate); downloadSpeed.ReportProgress(startDate, totalBytes, 0); Assert.AreEqual(0, downloadSpeed.BytesPerSecond); }
public void DownloadSpeed_WithSingleItem_CalculatesCorrectSpeed() { const int totalBytes = 10000; var startDate = DateTime.Now; var downloadSpeed = new DownloadSpeed(startDate); downloadSpeed.ReportProgress(startDate.AddMilliseconds(1000), totalBytes, 500); Assert.AreEqual(500, downloadSpeed.BytesPerSecond); }
private void CheckAllConversionsAgainstOneBitPerSecond(DownloadSpeed downloadSpeed) { Assert.AreEqual(1, downloadSpeed.BitsPerSecond); Assert.AreEqual(0.125, downloadSpeed.BytesPerSecond); Assert.AreEqual(0.001, downloadSpeed.Kbps); Assert.AreEqual(0.000125, downloadSpeed.KBps); Assert.AreEqual(1e-6, downloadSpeed.Mbps); Assert.AreEqual(1.25e-7, downloadSpeed.MBps); Assert.AreEqual(1e-9, downloadSpeed.Gbps); Assert.AreEqual(1.25e-10, downloadSpeed.GBps); }
public void DownloadSpeedWithTwoItems_EstimatedRemainingDuration_CalculatesCorrectDuration() { const int totalBytes = 10000; var startDate = DateTime.Now; var downloadSpeed = new DownloadSpeed(startDate); downloadSpeed.ReportProgress(startDate.AddMilliseconds(500), totalBytes, 500); downloadSpeed.ReportProgress(startDate.AddMilliseconds(1000), totalBytes, 1000); Assert.AreEqual(TimeSpan.FromSeconds(9), downloadSpeed.EstimatedRemainingDuration); }
public override string ToString() { string toReturn = string.Empty; toReturn += "FileName: " + FileName + Environment.NewLine; toReturn += "FileLocation: " + FileLocation + Environment.NewLine; toReturn += "DownloadSpeed: " + DownloadSpeed.ToString() + Environment.NewLine; toReturn += "FileSize: " + FileSize.ToString() + Environment.NewLine; toReturn += "DownloadProgress: " + DownloadProgress.ToString() + Environment.NewLine; toReturn += "DownloadStatus " + DownloadStatus.ToString() + Environment.NewLine; return(toReturn); }
public void DownloadSpeedWithTwoItems_AfterReset_ValuesAreResetted() { const int totalBytes = 10000; var startDate = DateTime.Now; var downloadSpeed = new DownloadSpeed(startDate); downloadSpeed.ReportProgress(startDate.AddMilliseconds(500), totalBytes, 500); downloadSpeed.ReportProgress(startDate.AddMilliseconds(1000), totalBytes, 1000); downloadSpeed.Reset(); Assert.AreEqual(0, downloadSpeed.BytesPerSecond); }
private void StartDownload(string downloadUrl) { _webClient = new WebClient(); _webClient.DownloadProgressChanged += WebClientOnDownloadProgressChanged; _webClient.DownloadFileCompleted += WebClientOnDownloadFileCompleted; var uri = new Uri(downloadUrl); var filename = _pathSafe.GetFileName(uri.LocalPath); _downloadSpeed = new DownloadSpeed(_webClient); _downloadLocation = _tempFolderProvider.TempFolder; _directory.CreateDirectory(_downloadLocation); _downloadLocation = _pathSafe.Combine(_downloadLocation, filename); _webClient.DownloadFileAsync(uri, _downloadLocation); }
private void StartDownload(string downloadUrl) { _webClient = new WebClient(); _webClient.DownloadProgressChanged += WebClientOnDownloadProgressChanged; _webClient.DownloadFileCompleted += WebClientOnDownloadFileCompleted; var uri = new Uri(downloadUrl); string filename = Path.GetFileName(uri.LocalPath); _downloadSpeed = new DownloadSpeed(_webClient); _downloadLocation = GetTempPath(); Directory.CreateDirectory(_downloadLocation); _downloadLocation = Path.Combine(_downloadLocation, filename); _webClient.DownloadFileAsync(uri, _downloadLocation); }
public void DownloadSpeed_WithCapacity_WithDoubleCapacityItems_CalculatesCorrectSpeedForSecondHalf() { const int totalBytes = 10000; var startDate = DateTime.Now; var downloadSpeed = new DownloadSpeed(startDate); downloadSpeed.Capacity = 2; downloadSpeed.ReportProgress(startDate.AddMilliseconds(500), totalBytes, 500); downloadSpeed.ReportProgress(startDate.AddMilliseconds(1000), totalBytes, 1000); downloadSpeed.ReportProgress(startDate.AddMilliseconds(1500), totalBytes, 2000); downloadSpeed.ReportProgress(startDate.AddMilliseconds(2000), totalBytes, 3000); Assert.AreEqual(2000, downloadSpeed.BytesPerSecond); }
public async Task StartDownloadAsync(IApplicationVersion version) { if (_downloadTask == null) { _cancellationSource = _cancellationSourceFactory.CreateSource(); _downloadTask = Task.Run(() => { _httpClient = new HttpClient(); DownloadSpeed = new DownloadSpeed(); OnProgressChanged += DownloadSpeed.DownloadProgressChanged; OnDownloadFinished += DownloadSpeed.webClient_DownloadFileCompleted; var downloadFileWithRange = DownloadFileWithRange(version); OnDownloadFinished?.Invoke(this, new UpdateProgressChangedEventArgs(downloadFileWithRange, 0, 0, 0)); _downloadTask = null; }, _cancellationSource.Token); } await _downloadTask; }
public void StartDownload(IApplicationVersion version) { if (isDownloading) { return; } _httpClient = new HttpClient(); Task.Run(() => { isDownloading = true; DownloadSpeed = new DownloadSpeed(); OnProgressChanged += DownloadSpeed.DownloadProgressChanged; OnDownloadFinished += DownloadSpeed.webClient_DownloadFileCompleted; var downloadFileWithRange = DownloadFileWithRange(version); OnDownloadFinished?.Invoke(this, new UpdateProgressChangedEventArgs(downloadFileWithRange, 0, 0, 0)); isDownloading = false; }); }
public void VerifyDownloadSpeedConversionsFromGigaBytePerSecond() { var downloadSpeed = new DownloadSpeed(DownloadSpeed.Type.GBps, 1.25e-10); CheckAllConversionsAgainstOneBitPerSecond(downloadSpeed); }
public void VerifyDownloadSpeedConversionsFromMegaBitPerSecond() { var downloadSpeed = new DownloadSpeed(DownloadSpeed.Type.Mbps, 1e-6); CheckAllConversionsAgainstOneBitPerSecond(downloadSpeed); }
public void VerifyDownloadSpeedConversionsFromKiloBytePerSecond() { var downloadSpeed = new DownloadSpeed(DownloadSpeed.Type.KBps, 0.000125); CheckAllConversionsAgainstOneBitPerSecond(downloadSpeed); }
/// <summary> /// Starts downloading the file. /// </summary> /// <param name="saveLocation">Path to where the file will be saved.</param> /// <param name="onProgressChanged">Interface to track download progress.</param> /// <returns>A System.Threading.Task that represents the download operation.</returns> /// <exception cref="System.NotSupportedException"> /// Invalid URI scheme. /// </exception> /// <exception cref="System.Security.SecurityException"> /// Permission is not granted to connect to remote server. /// </exception> /// <exception cref="System.UriFormatException">The download URL is invalid.</exception> /// <exception cref="System.Net.WebException">An error occurred while processing the request.</exception> /// <exception cref="System.ArgumentException"> /// path is an empty string (""), contains only white space, or contains one /// or more invalid characters. /// </exception> /// <exception cref="System.IO.IOException"> /// An I/O error, such as specifying FileMode.CreateNew when the file specified /// by path already exists, occurred. -or-The stream has been closed. /// </exception> /// <exception cref="System.Security.SecurityException"> /// The caller does not have the required permission. /// </exception> /// <exception cref="System.IO.DirectoryNotFoundException"> /// The specified path is invalid, such as being on an unmapped drive. /// </exception> /// <exception cref="System.UnauthorizedAccessException"> /// The access requested is not permitted by the operating system for the specified /// path, such as when access is Write or ReadWrite and the file or directory /// is set for read-only access. /// </exception> /// <exception cref="System.IO.PathTooLongException"> /// The specified path, file name, or both exceed the system-defined maximum /// length. For example, on Windows-based platforms, paths must be less than /// 248 characters, and file names must be less than 260 characters. /// </exception> public Task DownloadAsync(string saveLocation, IProgress <DownloadProgress> onProgressChanged = null) { if (!IsInitialized) { throw new InvalidOperationException("The object has not been initialized. You must call DownloadTask.Initialize() first."); } if (isDisposed) { throw new ObjectDisposedException("DownloadTask", "Cannot start because this instance has been disposed."); } if (Status == DownloadTaskStatus.Failed || Status == DownloadTaskStatus.Canceled) { downloadWatch.Reset(); } Status = DownloadTaskStatus.Starting; downloadWatch.Start(); elapsedTimer.Start(); FullPath = saveLocation; // filename after download FileName = Path.GetFileName(FullPath); _saveToIncomlete = FullPath + IncompleteDlExt; // save here temporarily (until download completes) progressReporter = onProgressChanged; // save so that it can be used for resumption cancelSource = new CancellationTokenSource(); // disposed after paused, canceled, completed. a new one is use when resuming return(Task.Factory.StartNew(() => { HttpWebResponse response = null; FileStream fStream = null; try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_url); request.UseDefaultCredentials = true; request.Proxy = WebRequest.GetSystemWebProxy(); byte[] buffer = new byte[BufferSize]; FileMode mode; long bytesDownloaded = 0; if (File.Exists(_saveToIncomlete) && ResumeSupported) // dl has been started before; try to resume { FileInfo fInfo = new FileInfo(_saveToIncomlete); if (fInfo.Length < this.Size.Value) // incomplete file must be smaller than total download size // add range header to resume where left off { request.AddRange(fInfo.Length); bytesDownloaded = fInfo.Length; mode = FileMode.Append; } else // otherwise, it's another file with the same name or it got currupted { File.Delete(_saveToIncomlete); mode = FileMode.Create; } } else // resume not supported; start from beginning { mode = FileMode.Create; } // begin download fStream = new FileStream(_saveToIncomlete, mode, FileAccess.Write); response = (HttpWebResponse)request.GetResponse(); using (Stream dlStream = response.GetResponseStream()) { // download stream // for measuring speed const int reportInterval = 200; // ms int tmpBytes = 0; Stopwatch watch = new Stopwatch(); Func <DownloadProgress> ProgressSnapshot = () => { double percent; TimeSpan eta; FileSize totalSize; FileSize curSize = new FileSize(bytesDownloaded); DownloadSpeed speed; if (Status == DownloadTaskStatus.Completed) { speed = new DownloadSpeed(Size.Value, Elapsed); } else { speed = new DownloadSpeed(tmpBytes, watch.Elapsed); // current speed } if (Size.Value > 0) // got file size from server { totalSize = new FileSize(this.Size.Value); percent = (double)bytesDownloaded / Size.Value * 100; eta = TimeSpan.FromSeconds((this.Size.Value - bytesDownloaded) / (bytesDownloaded / Elapsed.TotalSeconds)); // eta from average speed } else // size is not known (happens with webpages) { totalSize = new FileSize(bytesDownloaded); // use number of bytes downloaded so far as size percent = -1; eta = TimeSpan.FromSeconds(-1); } return new DownloadProgress(percent, curSize, totalSize, speed, eta, Elapsed); }; while (true) { Status = DownloadTaskStatus.Downloading; watch.Start(); int bytesRead = dlStream.Read(buffer, 0, buffer.Length); watch.Stop(); if (watch.ElapsedMilliseconds >= reportInterval) { lock (Progress) { Progress.Update(ProgressSnapshot()); } if (onProgressChanged != null) { onProgressChanged.Report(Progress); } tmpBytes = 0; watch.Reset(); } bytesDownloaded += bytesRead; // total downloaded so far tmpBytes += bytesRead; // total since last progress report if (bytesRead < 1) // dl completed or failed { if (fStream.Length < Size.Value) // downloaded less than required { Status = DownloadTaskStatus.Failed; } else { Status = DownloadTaskStatus.Completed; } lock (Progress) { Progress.Update(ProgressSnapshot()); } if (onProgressChanged != null) { onProgressChanged.Report(Progress); } break; } // write block to file fStream.Write(buffer, 0, bytesRead); fStream.Flush(); try { // check for cancellation (pause or cancel) cancelSource.Token.ThrowIfCancellationRequested(); } catch (OperationCanceledException) { if (Status == DownloadTaskStatus.Pausing) { Status = DownloadTaskStatus.Paused; } else { Status = DownloadTaskStatus.Canceled; } throw; } } } } catch (Exception ex) { if (!(ex is OperationCanceledException)) // something went wrong { Status = DownloadTaskStatus.Failed; throw; } } finally { downloadWatch.Stop(); elapsedTimer.Stop(); if (response != null) { response.Dispose(); } if (fStream != null) { fStream.Dispose(); } if (cancelSource != null) { cancelSource.Dispose(); cancelSource = null; } if (Status == DownloadTaskStatus.Completed) { File.Move(_saveToIncomlete, _saveTo); // rename temporary file } else if (Status == DownloadTaskStatus.Canceled) { if (DeleteWhenCanceled) { File.Delete(_saveToIncomlete); } } } }, TaskCreationOptions.LongRunning)); }
/// <summary> /// Instantiates a new instance of the DownloadTask.DownloadSpeed class. /// </summary> /// <param name="ds">An existing DownloadSpeed instance.</param> public DownloadSpeed(DownloadSpeed ds) : this() { Value = ds.Value; }