// 开始一个下载任务 private void StartTask(DownloadInfo taskinfo) { if (this.m_DownloadingTasks.ContainsKey(taskinfo.fileName)) { DebugUtil.Log("task already in progress :{0}", taskinfo.fileName); } else { DownloadingTask value = new DownloadingTask(taskinfo); this.m_DownloadingTasks[taskinfo.fileName] = value; 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; } HttpDownload(value); } }
static void DownloadCommonInitialAB(BaseResFakeInfo rrInfo) { string fileUrl = rrInfo.GetDownLoadUrlForInitial(); string savePath = FilePathTools.streamingAssetsPath_Platform + "/" + rrInfo.LocalABPath.ToLower(); CommonResLog("qushuang -----> Start DownloadHomeInitialAB {0}", fileUrl); if (File.Exists(savePath)) { File.Delete(savePath); } FilePathTools.CreateFolderByFilePath(savePath); try { Stream outStream; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(fileUrl); WebResponse response = request.GetResponse(); Stream inStream = response.GetResponseStream(); //获取http byte[] b = new byte[1024 * 1024]; //每一次获取的长度 FileInfo fi = new FileInfo(savePath); outStream = fi.Create(); //创建文件 int num = 0; int readCount = inStream.Read(b, 0, b.Length); //读流 num += readCount; while (readCount > 0) { outStream.Write(b, 0, readCount); //写流 readCount = inStream.Read(b, 0, b.Length); //再读流 num += readCount; } outStream.Close(); inStream.Close(); response.Close(); CommonResLog("qushuang -----> Download successfully! {0}, total size is {1}", fileUrl, num); } catch (Exception ex) { CommonResLog("qushuang -----> An error occured while downloading {0} due to {1}. try again!", fileUrl, ex); DownloadCommonInitialAB(rrInfo); return; } }
static void DownloadCommonInitialABWithHttpRequest(BaseResFakeInfo rrInfo) { string fileUrl = rrInfo.GetDownLoadUrl(); string savePath = FilePathTools.streamingAssetsPath_Platform + "/" + rrInfo.LocalABPath.ToLower(); CommonResLog("qushuang -----> Start DownloadHomeInitialAB {0}", fileUrl); if (File.Exists(savePath)) { File.Delete(savePath); } FilePathTools.CreateFolderByFilePath(savePath); new HTTPRequest(new Uri(fileUrl), HTTPMethods.Get, (req, rep) => { if (rep == null) { CommonResLog("qushuang -----> Response null. try again! {0}", fileUrl); DownloadCommonInitialABWithHttpRequest(rrInfo); } else if (rep.StatusCode >= 200 && rep.StatusCode < 300) { string localfile = savePath; try { List <byte[]> streamedFragments = rep.GetStreamedFragments(); if (streamedFragments != null) { int num = 0; using (FileStream fileStream = new FileStream(localfile, FileMode.Append)) { foreach (byte[] array in streamedFragments) { num += array.Length; fileStream.Write(array, 0, array.Length); } } CommonResLog("qushuang -----> Download successfully! {0}, total size is {1}", fileUrl, num); } else { CommonResLog("qushuang -----> No streamedFragments! try again! {0}", localfile); DownloadCommonInitialABWithHttpRequest(rrInfo); } } catch (Exception ex) { CommonResLog("qushuang -----> An error occured while downloading {0} due to {1}. try again!", localfile, ex); DownloadCommonInitialABWithHttpRequest(rrInfo); return; } } else { CommonResLog("qushuang -----> Unexpected response from server: {0}, StatusCode = {1}, try again!", fileUrl, rep.StatusCode); DownloadCommonInitialABWithHttpRequest(rrInfo); } }) { DisableCache = true, IsCookiesEnabled = false, UseStreaming = true, StreamFragmentSize = 1 * 1024 * 1024 * 100, // 1k 1Mb 100Mb ConnectTimeout = TimeSpan.FromSeconds(5), Timeout = TimeSpan.FromSeconds(10) }.Send(); }
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(); }