// 结束一个下载任务,归还并发数 public void FinishDownloadTask(DownloadInfo info) { info.isFinished = true; if (info.onComplete != null) { info.onComplete(info); } m_DownloadingTasks.Remove(info.fileName); --downloadThreads; }
// 重试下载任务 Warning:只有下载器需要调用这个借口,业务层不要调用 public void RetryDownload(DownloadInfo info) { if (info.retry > 0) { info.retry -= 1; pendingDownloads.Enqueue(info); m_DownloadingTasks.Remove(info.fileName); --downloadThreads; } else { FinishDownloadTask(info); } }
void Update() { if (pendingDownloads.Count > 0) { int freeThreads = maxDownloads - downloadThreads; for (int i = 0; i < freeThreads; ++i) { if (pendingDownloads.Count > 0) { DownloadInfo info = pendingDownloads.Dequeue(); StartTask(info); } } } }
static void DownloadCommonInitialABWithBreakDownloader(BaseResFakeInfo rrInfo) { DownloadInfo taskinfo = new DownloadInfo(rrInfo.LocalABPath, rrInfo.GetPlatformMd5(), null); taskinfo.savePath = FilePathTools.streamingAssetsPath_Platform + "/" + rrInfo.LocalABPath.ToLower(); if (File.Exists(taskinfo.savePath)) { File.Delete(taskinfo.savePath); } FilePathTools.CreateFolderByFilePath(taskinfo.savePath); if (!File.Exists(taskinfo.tempPath)) { FilePathTools.CreateFolderByFilePath(taskinfo.tempPath); File.Create(taskinfo.tempPath).Dispose(); } using (var sw = new FileStream(taskinfo.tempPath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)) { taskinfo.downloadedSize = (int)sw.Length; } DownloadingTask tmpTask = new DownloadingTask(taskinfo); HTTPRequest httprequest = new HTTPRequest(new Uri(tmpTask.DownloadInfo.url), HTTPMethods.Head, (req, rep) => { tmpTask.HeadRequest = null; if (rep == null) { CommonResLog("Download failed due to network for :{0}", tmpTask.DownloadInfo.fileName); tmpTask.DownloadInfo.result = DownloadResult.ServerUnreachable; DownloadCommonInitialABWithBreakDownloader(rrInfo); } else if (rep.StatusCode == 200) { try { string firstHeaderValue = rep.GetFirstHeaderValue("Content-Length"); tmpTask.DownloadInfo.downloadSize = int.Parse(firstHeaderValue); CommonResLog("Will download {0} bytes for '{1}'", tmpTask.DownloadInfo.downloadSize, tmpTask.DownloadInfo.fileName); BreakPointDownloader downloader = new BreakPointDownloader(tmpTask.DownloadInfo, new Dictionary <string, DownloadingTask>()); tmpTask.Downloader = downloader; downloader.StartDownload(); } catch (Exception ex) { CommonResLog("An error occured during download '{0}' due to {1}", tmpTask.DownloadInfo.fileName, ex); tmpTask.DownloadInfo.result = DownloadResult.Failed; DownloadCommonInitialABWithBreakDownloader(rrInfo); } } else { CommonResLog("Response is not ok! for: {0}", tmpTask.DownloadInfo.url); tmpTask.DownloadInfo.result = DownloadResult.Failed; DownloadCommonInitialABWithBreakDownloader(rrInfo); } }) { DisableCache = true }; httprequest.Send(); }
public BreakPointDownloader(DownloadInfo info, Dictionary <string, DownloadingTask> tasks) { this.m_DownloadInfo = info; //DebugUtil.Log("start breakpoint download:" + this.m_DownloadInfo.tempPath); }
public DownloadingTask(DownloadInfo info) { this.DownloadInfo = info; }