/// <summary> /// /// </summary> /// <param name="hosts"></param> /// <param name="maxLoading"></param> /// <param name="oneDone"></param> /// <param name="allDone"></param> public void Init(string[] hosts, int maxLoading, System.Action<string, bool, object> oneDone, System.Action<bool> allDone) { this.hosts = hosts; this.maxLoading = maxLoading; //webClients = new WebDownload[maxLoading]; WebDownload item; for (int i = 0; i < maxLoading; i++) { item = new WebDownload(); //webClients[i] = item; webClients.Add(item); item.DownloadFileCompleted += OnDownloadFileCompleted; item.DownloadProgressChanged += OnDownloadProgressChanged; item.isFree = true; } this.onOneDone = oneDone; this.allDone = allDone; outputPath = CUtils.GetRealPersistentDataPath() + "/"; if (!Directory.Exists(outputPath)) Directory.CreateDirectory(outputPath); }
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); } } }
void ReleaseWebDonwLoad(WebDownload webd, ABInfo aBInfo) { lock (syncRoot) { WebDownload.Release(webd); } }
public static void Release(WebDownload toRelease) { objectPool.Release(toRelease); }
private static void m_ActionOnRelease(WebDownload item) { item.userData = null; item.Dispose(); }
private static void m_ActionOnGet(WebDownload item) { item.error = null; item.tryTimes = 0; }