void LoadingQueue() { LinkedListNode <BackGroundQueue> fristNode = this.loadQueue.First; for (int i = 0; i < this.loadingCount - this.loadingTasks.Count; i++) { while (fristNode != null) { BackGroundQueue value = fristNode.Value; if (value.Count > 0) { var abInfo = value.Dequeue(); if (!loadingTasks.ContainsKey(abInfo)) { RunningTask(abInfo, value); break; } } fristNode = fristNode.Next; this.loadQueue.Remove(value); } if (fristNode == null) { break; } } }
internal void RunningTask(ABInfo abInfo, BackGroundQueue bQueue) { var userData = new object[] { abInfo, bQueue, }; if (ManifestManager.CheckPersistentCrc(abInfo)) //验证crc { webClients.Add(userData); // completa loadingTasks[abInfo] = abInfo; #if UNITY_EDITOR Debug.LogFormat("RunningTask abName={0},Persistent is down frame={1}", abInfo.abName, Time.frameCount); #endif return; } else { if (abInfo.state == ABInfoState.Fail) { FileHelper.DeletePersistentFile(abInfo.abName); } var download = WebDownload.Get(); download.userData = userData; download.DownloadFileCompleted = OnDownloadFileCompleted; download.DownloadProgressChanged = OnDownloadProgressChanged; loadingTasks[abInfo] = download; string urlHost = hosts[0]; RealLoad(download, abInfo, bQueue, urlHost); } }
void LoadingQueue() { lock (syncRoot) { LinkedListNode <BackGroundQueue> fristNode = this.loadQueue.First; #if UNITY_EDITOR Debug.LogFormat("LoadingQueue.count={0},fristNode={1},canload={2},frame={3}", loadQueue.Count, fristNode, this.loadingCount - this.loadingTasks.Count > 0, Time.frameCount); #endif while (fristNode != null && this.loadingCount - this.loadingTasks.Count > 0) { BackGroundQueue value = fristNode.Value; #if UNITY_EDITOR Debug.LogFormat("BackGroundQueue.Count={0},LoadingQueue.count={1},loading={2},frame={3}", value.Count, loadQueue.Count, this.loadingCount - this.loadingTasks.Count, Time.frameCount); #endif if (value.Count > 0) { var abInfo = value.Dequeue(); if (!loadingTasks.ContainsKey(abInfo)) { RunningTask(abInfo, value); } } else { fristNode = fristNode.Next; this.loadQueue.Remove(value); } } } }
internal void RemoveTask(ABInfo abInfo, BackGroundQueue bQueue) { bool isError = abInfo.state != ABInfoState.Success; var mainAbInfo = ManifestManager.GetABInfo(abInfo.abName); if (mainAbInfo != null) { mainAbInfo.state = abInfo.state; } // Debug.LogFormat("task complete abName={0},size={1},loadingTasks.Count={2},bQueue.count={3}", abInfo.abName, abInfo.size, loadingTasks.Count, bQueue.Count); bQueue.Complete(abInfo, isError); }
/// <summary> /// begin download /// </summary> public void ReloadError() { lock (syncRoot) { LinkedListNode <BackGroundQueue> fristNode = this.loadQueue.First; while (fristNode != null) { BackGroundQueue value = fristNode.Value; value.ReLoadError(); #if UNITY_EDITOR Debug.LogFormat("ReLoadError BackGroundQueue.Count = {0} ", value.Count); #endif fristNode = fristNode.Next; } } }
void OnDownloadProgressChanged(object sender, DownloadingProgressChangedEventArgs e) { CalcReceiveBytes(e.BytesRead); WebDownload webd = (WebDownload)sender; object[] arr = (object[])webd.userData; ABInfo abInfo = ((ABInfo)arr[0]);; BackGroundQueue bQueue = (BackGroundQueue)arr[1];; bQueue.Progress(abInfo, e.ProgressPercentage); if (progressChangedEventArgs != null) { progressChangedEventArgs.received += e.BytesReceived; } // Debug.LogFormat ("background ab:{0}\r\n OnDownloadProgressChanged:{1}", ((ABInfo) ((object[]) webd.userData) [0]).abName, e.ProgressPercentage); }
internal void RealLoad(WebDownload download, ABInfo abInfo, BackGroundQueue bQueue, string urlHost = "") { string abName = abInfo.abName; bool appCrc = HugulaSetting.instance != null ? HugulaSetting.instance.appendCrcToFile : false; if (abInfo.crc32 > 0 && appCrc) { abName = CUtils.InsertAssetBundleName(abName, "_" + abInfo.crc32.ToString()); } Uri url = new Uri(CUtils.PathCombine(urlHost, abName)); string path = GetTmpFilePath(abInfo); FileHelper.CheckCreateFilePathDirectory(path); download.DownloadFileAsync(url, path); #if !HUGULA_RELEASE Debug.LogFormat(" background load {0} ,save path ={1},abInfo.state={2} ,webClient({3})", url.AbsoluteUri, path, abInfo.state, download); #endif }
internal void RemoveTask(ABInfo abInfo, BackGroundQueue bQueue) { loadingTasks.Remove(abInfo); bool isError = abInfo.state != ABInfoState.Success; var mainAbInfo = ManifestManager.GetABInfo(abInfo.abName); if (mainAbInfo != null) { mainAbInfo.state = abInfo.state; } #if UNITY_EDITOR Debug.LogFormat("task complete abName={0},size={1},isError={2},loadingTasks.Count={3},bQueue.count={4}", abInfo.abName, abInfo.size, isError, loadingTasks.Count, bQueue.Count); #endif bQueue.Complete(abInfo, isError); if (!bQueue.IsError) { LoadingQueue(); } }
private BackGroundQueue GetLoadQueue(int priority) { // priority BackGroundQueue bQueue = null; lock (syncRoot) { if (loadQueueDic.ContainsKey(priority)) { bQueue = this.loadQueueDic[priority]; } else { bQueue = new BackGroundQueue(priority); this.loadQueueDic[priority] = bQueue; } bool flag = false; for (LinkedListNode <BackGroundQueue> fristNode = this.loadQueue.First; fristNode != null; fristNode = fristNode.Next) { if (fristNode.Value.priority == priority) { this.loadQueue.AddAfter(fristNode, bQueue); //Debug.LogFormat(" fristNode.Value.priority={0}<priority{1}", fristNode.Value.priority, priority); flag = true; break; } if (fristNode.Value.priority < priority) //大的排前面 { this.loadQueue.AddBefore(fristNode, bQueue); // Debug.LogFormat(" fristNode.Value.priority={0}<priority{1}", fristNode.Value.priority, priority); flag = true; break; } } if (!flag) { this.loadQueue.AddLast(bQueue); } } return(bQueue); }
internal void RunningTask(ABInfo abInfo, BackGroundQueue bQueue) { var download = WebDownload.Get(); download.userData = new object[] { abInfo, bQueue, }; download.DownloadFileCompleted -= OnDownloadFileCompleted; download.DownloadProgressChanged -= OnDownloadProgressChanged; download.DownloadFileCompleted += OnDownloadFileCompleted; download.DownloadProgressChanged += OnDownloadProgressChanged; loadingTasks[abInfo] = download; if (ManifestManager.CheckPersistentCrc(abInfo)) //验证crc { webClients.Add(download); return; } else { string urlHost = hosts[0]; RealLoad(download, abInfo, bQueue, urlHost); } }
internal void RunningTask(ABInfo abInfo, BackGroundQueue bQueue, bool useHost1 = false) { var download = WebDownload.Get(); download.userData = new object[] { abInfo, bQueue, useHost1 ? "host1" : "host" }; download.DownloadFileCompleted -= OnDownloadFileCompleted; download.DownloadProgressChanged -= OnDownloadProgressChanged; download.DownloadFileCompleted += OnDownloadFileCompleted; download.DownloadProgressChanged += OnDownloadProgressChanged; loadingTasks[abInfo] = download; // download.CancelAsync() // Debug.LogFormat("RunningTask abName={0},state={1}", abInfo.abName, abInfo.state); if (ManifestManager.CheckPersistentCrc(abInfo)) //验证crc { webClients.Add(download); return; } else { string abName = abInfo.abName; bool appCrc = HugulaSetting.instance != null ? HugulaSetting.instance.appendCrcToFile : false; if (abInfo.crc32 > 0 && appCrc) { abName = CUtils.InsertAssetBundleName(abName, "_" + abInfo.crc32.ToString()); } string h = useHost1 ? host1 : host; Uri url = new Uri(CUtils.PathCombine(h, abName)); string path = CUtils.PathCombine(outputPath, abInfo.abName); FileHelper.CheckCreateFilePathDirectory(path); download.DownloadFileAsync(url, path); // #if UNITY_EDITOR // Debug.LogFormat(" begin load {0} ,save path ={1},abInfo.state={2} ,webClient({3})", url.AbsoluteUri, path, abInfo.state, download); // #endif } }
void OnDownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e) { WebDownload webd = (WebDownload)sender; webd.isError = (e.Error != null); if (webd.isError) { object[] arr = (object[])webd.userData; if (arr != null) { ABInfo abInfo = ((ABInfo)arr[0]); BackGroundQueue bQueue = (BackGroundQueue)arr[1]; bool useHost1 = arr[2].ToString().Equals("host1"); FileHelper.DeletePersistentFile(abInfo.abName); if (!useHost1 && !string.IsNullOrEmpty(host1)) { Debug.LogWarning(string.Format("web download url:{0}\r\n error:{1}\r\n reload={2}", host + "/" + abInfo.abName, host1, e.Error)); webd.needReload = true; } else { Debug.LogWarning(string.Format("web download url:{0}\r\n error:{1}", (useHost1 ? host1 : host) + "/" + abInfo.abName, e.Error)); } } else { Debug.LogWarning(string.Format("download url:{0}\r\n error:{1}", host, e.Error)); } } webClients.Add(webd); // #if UNITY_EDITOR // Debug.LogFormat("url:{0}\r\n is done", host); // #endif }
void LoadingQueue() { lock (syncRoot) { LinkedListNode <BackGroundQueue> fristNode = this.loadQueue.First; while (fristNode != null && this.loadingCount - this.loadingTasks.Count > 0) { BackGroundQueue value = fristNode.Value; if (value.Count > 0) { var abInfo = value.Dequeue(); if (!loadingTasks.ContainsKey(abInfo)) { RunningTask(abInfo, value); } } else { fristNode = fristNode.Next; this.loadQueue.Remove(value); } } } }
internal void RealLoad(WebDownload download, ABInfo abInfo, BackGroundQueue bQueue, string urlHost, string timestamp = "") { string abName = abInfo.abName; bool appCrc = HugulaSetting.instance != null ? HugulaSetting.instance.appendCrcToFile : false; if (abInfo.crc32 > 0 && appCrc) { abName = CUtils.InsertAssetBundleName(abName, "_" + abInfo.crc32.ToString()); } Uri url = null; if (string.IsNullOrEmpty(timestamp)) { url = new Uri(CUtils.PathCombine(urlHost, abName)); } else { url = new Uri(CUtils.PathCombine(urlHost, abName + "?" + timestamp)); } string path = GetTmpFilePath(abInfo); FileHelper.CheckCreateFilePathDirectory(path); if (abInfo.size < BreakPointLength) { download.DownloadFileAsync(url, path); } else { download.DownloadFileMultiAsync(url, path); } #if !HUGULA_NO_LOG Debug.LogFormat(" begin load {0} ,save path ={1},abInfo.state={2} ,webClient({3})", url.AbsoluteUri, path, abInfo.state, download); #endif }
void OnDownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e) { WebDownload webd = (WebDownload)sender; object[] arr = (object[])webd.userData; ABInfo abInfo = null; BackGroundQueue bQueue = null; webd.tryTimes++; int tryTimes = webd.tryTimes; if (arr != null && arr.Length >= 2) { abInfo = ((ABInfo)arr[0]); bQueue = (BackGroundQueue)arr[1]; } #if !HUGULA_RELEASE // Debug.LogFormat ("background ab:{0}\r\n is completed,error:{1}", abInfo.abName, e.Error==null? string.Empty : e.Error.Message); #endif if (e.Error != null) { if (tryTimes <= hosts.Length * 3) { int i = tryTimes % hosts.Length; Debug.LogWarning(string.Format("background download error ab:{0}, tryTimes={1},host={2},error:{3}", abInfo.abName, webd.tryTimes, hosts[i], e.Error)); RealLoad(webd, abInfo, bQueue, hosts[i], tryTimes.ToString()); return; } else { Debug.LogErrorFormat("background download error message {0} \r\n trace {1}", e.Error.Message, e.Error.StackTrace); abInfo.state = ABInfoState.None; // none or fail? webClients.Add(arr); ReleaseWebDonwLoad(webd, abInfo); } } else { string path = GetTmpFilePath(abInfo); FileInfo tmpFile = new FileInfo(path); if (tmpFile.Length == abInfo.size) //check size { abInfo.state = ABInfoState.Success; webClients.Add(arr); ReleaseWebDonwLoad(webd, abInfo); } else if (tryTimes <= hosts.Length * 3) { int i = tryTimes % hosts.Length; string error = string.Format("background complete length check is wrong ab:{0} ,(length:{1}!=size:{2}) crc={3},tryTimes{4},host:{5}", abInfo.abName, tmpFile.Length, abInfo.size, tryTimes, abInfo.crc32, hosts[i]); Debug.LogWarning(error); tmpFile.Delete(); //删除错误文件 RealLoad(webd, abInfo, bQueue, hosts[i], tryTimes.ToString()); return; } else { string error = string.Format("background complete length check is wrong tryMaxTimes:{4} .ab:{0} (length:{1}!=size:{2}) crc={3},host:{5}", abInfo.abName, tmpFile.Length, abInfo.size, abInfo.crc32, tryTimes, hosts[tryTimes % hosts.Length]); Debug.LogWarning(error); abInfo.state = ABInfoState.None; // none or fail? webClients.Add(arr); ReleaseWebDonwLoad(webd, abInfo); } } LoadingQueue(); }
void OnDownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e) { WebDownload webd = (WebDownload)sender; object[] arr = (object[])webd.userData; ABInfo abInfo = null; BackGroundQueue bQueue = null; webd.tryTimes++; int tryTimes = webd.tryTimes; if (arr != null && arr.Length >= 2) { abInfo = ((ABInfo)arr[0]); bQueue = (BackGroundQueue)arr[1]; } webd.error = e.Error == null ? string.Empty : e.Error.Message; if (!string.IsNullOrEmpty(webd.error)) { FileHelper.DeletePersistentFile(abInfo.abName + ".tmp"); if (tryTimes < hosts.Length) { Debug.Log(string.Format("web download url:{0}\r\n times={1},error:{2}\r\n", hosts[tryTimes] + "/" + abInfo.abName, webd.tryTimes, e.Error)); RealLoad(webd, abInfo, bQueue, hosts[tryTimes]); return; // } else { Debug.Log(string.Format("web download url:{0}\r\n error:{1}", hosts[hosts.Length - 1] + "/" + abInfo.abName, e.Error)); } } else { string path = GetTmpFilePath(abInfo); FileInfo tmpFile = new FileInfo(path); if (tmpFile.Length == abInfo.size) //check size { abInfo.state = ABInfoState.Success; string realPath = CUtils.PathCombine(outputPath, abInfo.abName); if (File.Exists(realPath)) { File.Delete(realPath); } tmpFile.MoveTo(realPath);//rename } else if (tryTimes < hosts.Length) { FileHelper.DeletePersistentFile(abInfo.abName + ".tmp"); RealLoad(webd, abInfo, bQueue, hosts[tryTimes]); return; } else { webd.error = string.Format("background ab:{0} crc check is wrong", hosts[hosts.Length - 1] + "/" + abInfo.abName); Debug.Log(webd.error); } } #if !HUGULA_RELEASE Debug.LogFormat("background ab:{0}\r\n is done", ((ABInfo)((object[])webd.userData)[0]).abName); #endif webClients.Add(webd); }