private void DownloadNextItem() { while (true) { DownloadClient <DownloadItem> client = _clients.Take(); DownloadItem di; if (_filequeue.TryDequeue(out di)) { if (di.UseTempPath) { client.DownloadFileAsync(di.DownloadUri, di.TempPath, di); } else { client.DownloadFileAsync(di.DownloadUri, di.Path, di); } RaiseDownloadStarted(new FileDownloadStartedArgs { DownloadItem = di, ClientId = client.ClientId }); } if (_filequeue.Count == 0) { break; } } }
private void OnDownloadProgressChanged(DownloadClient <DownloadItem> sender, DownloadProgressChangedArgs e, DownloadItem userToken) { var item = new FileDownloadProgressChangedArgs(); item.ReceivedBytes = e.ReceivedBytes; item.ClientId = sender.ClientId; item.TotalBytesToBeRecieved = e.TotalBytesToBeRecieved; item.ProgressPercentage = e.ProgressPercentage; item.DownloadItem = userToken; item.DownloadSpeed = e.DownloadSpeed; RaiseDownloadProgressChanged(item); }
public DownloadMultipleFiles(ConcurrentQueue <DownloadItem> filequeue, int clientcount = 1, bool autoRedownload = false) { if (clientcount < CLIENT_COUNT_MIN) { clientcount = CLIENT_COUNT_MIN; } if (clientcount > CLIENT_COUNT_MAX) { clientcount = CLIENT_COUNT_MAX; } ClientCount = clientcount; _filequeue = filequeue; DownloadToComplete = filequeue.Count; _clients = new BlockingCollection <DownloadClient <DownloadItem> >(); for (int i = 0; i < clientcount; i++) { var client = new DownloadClient <DownloadItem>(i, 200); client.DownloadFileCompleted += OnDownloadCompleted; client.DownloadProgressChanged += OnDownloadProgressChanged; _clients.Add(client); } }
public async void OnDownloadCompleted(DownloadClient <DownloadItem> sender, DownloadCompletedArgs e, DownloadItem userToken) { _clients.Add(sender); //클라이언트 큐에 추가 Downloaded++; //다운로드된 아이템 추가(DownloadFileAsync는 비동기-동기화를 쓰기때문에 InterLocked를 쓸 필요가 없다.) DownloadItem di = userToken; FileDownloadCompletedArgs args = new FileDownloadCompletedArgs(); args.DownloadedItem = di; args.DownloadQueue = _filequeue.Count; args.ClientId = sender.ClientId; args.Downloaded = Downloaded; args.DownloadToComplete = DownloadToComplete; args.ProgressPercentage = Math.Round((double)(100 * Downloaded) / DownloadToComplete); if (di.UseFileIntegrityCheck) // 파일 무결성 검사 { args.DownloadedFileHash = await FileHashing.GetFileHashAsync(di.Path, di.HashType); if (di.FileHash != args.DownloadedFileHash) { args.FileIntegrity = false; } else { args.FileIntegrity = true; } } if (di.UseAutoRedownloadWhenFileIsCracked) //파일 깨졌다면 재다운로드 { if (!args.FileIntegrity) { if (di.UseTempPath) { File.Delete(di.TempPath); } else { File.Delete(di.Path); } EnqueueItem(userToken); } } if (di.UseTempPath) { string path = Path.GetDirectoryName(di.Path); if (!System.IO.Directory.Exists(path)) { Directory.CreateDirectory(path); } string temppath = Path.GetDirectoryName(di.TempPath); if (!System.IO.Directory.Exists(temppath)) { Directory.CreateDirectory(temppath); } File.Delete(di.Path); File.Move(di.TempPath, di.Path); } RaiseDownloadCompleted(args); if (Downloaded == DownloadToComplete) { RaiseAllDownloadCompleted(new AllFileDownloadCompletedArgs()); } }