/// <summary> /// Set up a package object /// </summary> public DownloadPackage(DownloaderAgent ownweAgent, string url, string fileLocalPath = null, bool requestRemoteSize = true) { System.Console.WriteLine("Devhus.Downloader is creating new package.."); ParentAgent = ownweAgent; MyFile = new DownloadFileInfo() { DownloadUrl = url, LocalPath = fileLocalPath }; ReadRemoteSize = requestRemoteSize; System.Console.WriteLine("Devhus.Downloader fetching package info.."); FetchFileInfo(); PackageID = CreatePackageID(); System.Console.WriteLine("Devhus.Downloader defined package with {0} as id", PackageID); MyFile.LocalTempPath = Path.Combine(MyFile.LocalFolder, PackageID); Progress = new DownloaderProgressArgs() { PackageID = PackageID, TotalBytes = MyFile.RemoteSize }; }
void DownloadSpeed(object sender, DownloaderProgressArgs e) { string speed = ""; speed = (e.DownloadSpeed / 1024).ToString() + "kb/s"; label3.Text = speed; }
internal void CallSpeedUpdate(DownloaderProgressArgs args) { if (OnSpeedUpdate == null) { return; } OnSpeedUpdate(this, args); }
internal void CallBuildingFileProgress(DownloaderProgressArgs args) { if (OnBuildingFileProgress == null) { return; } OnBuildingFileProgress(this, args); }
/// <summary> /// Set up the chunks list for the owner package /// </summary> internal void CreateChunks() { if (MyFile.LocalSignature != null) { if (MyFile.LocalSignature == MyFile.RemoteSignature) { System.Console.WriteLine("Devhus.Downloader {0} package file is found and checksum is matched", PackageID); CompleteDownload(true); return; } } System.Console.WriteLine("Devhus.Downloader {0} package creating chunks...", PackageID); Progress = new DownloaderProgressArgs() { TotalBytes = MyFile.RemoteSize, PackageID = this.PackageID, }; State = PackageState.Preparing; //var neededChunks = (int)Math.Ceiling((double)MyFile.RemoteSize / DownloaderOptions.RequiredSizeForChunks); ChunkParts = DownloaderOptions.MaxCoresUsage; if (ChunkParts < 1) { ChunkParts = 1; } long chunkSize = MyFile.RemoteSize / ChunkParts; //var chunkRemainSize = MyFile.RemoteSize % ChunkParts; if (chunkSize < 1) { chunkSize = MyFile.RemoteSize; ChunkParts = 1; } Chunks = new List <DownloadChunk>(); Progress.Chunks = new DownloadChunkProgress[ChunkParts]; if (!Directory.Exists(MyFile.LocalFolder)) { Directory.CreateDirectory(MyFile.LocalFolder); } long chunkRangeStart = 0; long chunkRangeEnd = 0; for (var chunkIndex = 0; chunkIndex < ChunkParts; chunkIndex++) { System.Console.WriteLine("Devhus.Downloader {0} chunk is creating...", chunkIndex); chunkRangeStart += chunkIndex == 0 ? 0 : chunkSize; chunkRangeEnd += chunkIndex == 0 ? chunkSize - 1 : chunkSize; //long ChunkRangeStart = chunkIndex * chunkSize; //long ChunkRangeEnd = ((chunkIndex + 1) * chunkSize) - 1; //if (chunkIndex == (ChunkParts - 1) && chunkRemainSize != 0) // ChunkRangeEnd += chunkRemainSize; if (ChunkParts > 1 && chunkIndex == (ChunkParts - 1)) { //chunkRangeStart += 1; chunkRangeEnd = MyFile.RemoteSize - 1; } //if (chunkIndex != (ChunkParts - 1)) // ChunkRangeEnd -= 1; Progress.Chunks[chunkIndex] = new DownloadChunkProgress() { ChunkIndex = chunkIndex }; var chunk = new DownloadChunk(this, chunkIndex, chunkRangeStart, chunkRangeEnd); Progress.Chunks[chunkIndex].TotalBytes = chunkIndex != (ChunkParts - 1) ? chunk.Length + 1 : chunk.Length; Chunks.Add(chunk); } }
/// <summary> /// Merge chunks files using streams /// </summary> internal void BuildPackage() { if (State != PackageState.Downloading) { System.Console.WriteLine("Devhus.Downloader package {0} is not ready for building.... State {1}", PackageID, State); return; } System.Console.WriteLine("Devhus.Downloader package {0} is building....", PackageID); State = PackageState.Building; Progress = new DownloaderProgressArgs() { TotalBytes = MyFile.RemoteSize, PackageID = this.PackageID, IsBuilding = true }; if (Directory.Exists(MyFile.LocalFolder) == false) { Directory.CreateDirectory(MyFile.LocalFolder); } using (var destinationStream = new FileStream(MyFile.LocalPath, FileMode.Create, FileAccess.Write)) { for (int chunkIndex = 0; chunkIndex < ChunkParts; chunkIndex++) { System.Console.WriteLine("Devhus.Downloader package {0} is merging with {1} chunk....", PackageID, chunkIndex); var chunk = Chunks[chunkIndex] ?? null; if (chunk == null || !File.Exists(chunk.FilePath)) { System.Console.WriteLine("Devhus.Downloader {0} chunk file was not found!", chunkIndex); continue; } using (var chunkStream = new FileStream(chunk.FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { long bytesToWrite = chunk.LocalBytes; var bufferSize = bytesToWrite > DownloaderOptions.BufferBlockSize ? DownloaderOptions.BufferBlockSize : bytesToWrite; int readBytes = 0; byte[] readBuffer = new byte[bufferSize]; //while ((readBytes = chunkStream.Read(readBuffer, 0, readBuffer.Length)) > 0) while (bytesToWrite > 0) { //if((bytesToWrite - readBytes) <= 0 && chunkIndex == (Chunks.Count - 1)) //{ // readBytes -= 1; //} readBytes = chunkStream.Read(readBuffer, 0, readBuffer.Length); destinationStream.Write(readBuffer, 0, readBytes); bytesToWrite -= readBytes; ReportBuildingProgress(chunkIndex, readBytes); } } } } Progress.LocalBytes = 0; Progress.ReceivedBytes = 0; ParentAgent.ParentDownloder.CallBuildingFileProgress(Progress); }
void DownloadProgress(object sender, DownloaderProgressArgs d) { label2.Text = d.speedOutput.ToString(); }