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 !HUGULA_NO_LOG 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 !HUGULA_NO_LOG 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 !HUGULA_NO_LOG 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; if (value.IsDown && !value.IsError) { this.loadQueue.Remove(value); //如果有错误需要保持在队列中 } } } } }
/// <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 !HUGULA_NO_LOG Debug.LogFormat("ReLoadError BackGroundQueue.Count = {0} ", value.Count); #endif fristNode = fristNode.Next; } } }
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 !HUGULA_NO_LOG 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(); } }
// void DispatchNetStateChange () { // //网络改变 // if (this.enabled && loadingTasks.Count > 0 && this.loadingCount > 0) // LoadingQueue (); // if (onNetSateChange != null) // onNetSateChange (this); // } 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 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_NO_LOG // 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); } } }