/// <summary> /// creates a new download job /// </summary> /// <param name="dwnlSource">url to download from</param> /// <param name="dwnlTarget">url to download to</param> public Download(string dwnlSource, string dwnlTarget) { //set the download data DwnlSource = dwnlSource; DwnlTarget = dwnlTarget; DwnlSize = FindFileSize(DwnlSource); //create the virtual chunk download jobs and its scheduler DwnlChunks = new Chunks(DwnlSource, DwnlSize); DwnlScheduler = new DownloadScheduler(DwnlChunks); }
/// <summary> /// updates the download progress /// </summary> /// <param name="timeSpan">time span of update</param> public void UpdateDownloadProgress(double timeSpan) { //initial download progress parameters double bufferedSize; double downloadedSize = DwnlChunks.ChunkSize * windowStart; Chunks chunks = DwnlChunks; //adjust the start of the window if completed while (windowStart < chunks.ChunkCount) { bufferedSize = Interlocked.Read(ref chunks.ChunkProgress[windowStart]); if (bufferedSize == chunks.ChunkSize) { windowStart++; downloadedSize += chunks.ChunkSize; } else { break; } } //update the size of the active chunks for (long i = windowStart; i < chunks.ChunkCount; i++) { bufferedSize = Interlocked.Read(ref DwnlChunks.ChunkProgress[i]); if (bufferedSize != 0) { downloadedSize += bufferedSize; } else { break; } } //compute the speed and progress DwnlSpeed = Math.Max(0, downloadedSize - DwnlSizeCompleted) / timeSpan; DwnlSizeCompleted = downloadedSize; DwnlProgress = DwnlSizeCompleted / DwnlSize * 100; }
/// <summary> /// initialize the download scheduler /// </summary> /// <param name="chunks">the chunk download jobs</param> public DownloadScheduler(Chunks chunks) { Chunks = chunks; }