/// <summary>
        /// 创建下载器.
        /// </summary>
        /// <returns>下载器.</returns>
        /// <param name="iTarget">目标信息.</param>
        private DownloaderBase CreateDownloader(DownloadTargetInfo iTarget)
        {
            DownloaderBase downloader  = null;
            TDownloadWay   downloadWay = ServersConf.GetInstance().DownloadWay;

            switch (downloadWay)
            {
            case TDownloadWay.WWW:
            {
                downloader = WWWDownloader.Create(
                    iTarget, OnStartDownload, OnDownloadSuccessed, OnDownloadFail);
            }
            break;

            case TDownloadWay.None:
            case TDownloadWay.Http:
            default:
            {
                downloader = HttpDownloader.Create(
                    iTarget, OnStartDownload, OnDownloadSuccessed, OnDownloadFail);
            }
            break;
            }
            return(downloader);
        }
        /// <summary>
        /// 下载失败回调函数.
        /// </summary>
        /// <param name="iDownloadInfo">下载信息.</param>
        /// <param name="iIsManifest">Manifest文件标志位.</param>
        /// <param name="iErrors">错误信息.</param>
        public void OnDownloadFail(DownloaderBase iDownloader, DownloadTargetInfo iDownloadInfo, bool iIsManifest, List <ErrorDetail> iErrors)
        {
            // 线程安全:添加错误信息至列表
            lock (_downloaderErrorLock) {
                string errsStr = null;
                foreach (ErrorDetail error in iErrors)
                {
                    if (string.IsNullOrEmpty(errsStr) == true)
                    {
                        errsStr = string.Format("Type:{0} State:{1} Detail:{2} Retries:{3}",
                                                error.Type.ToString(), error.State.ToString(), error.Detail, error.Retries.ToString());
                    }
                    else
                    {
                        errsStr = string.Format("{0} \n Type:{1} State:{2} Detail:{3} Retries:{4}",
                                                errsStr, error.Type.ToString(), error.State.ToString(), error.Detail, error.Retries.ToString());
                    }
                }

                this.Error("OnDownloadFail()::Download Failed!!! {0} \n Detail:{1}",
                           iDownloadInfo.toString(), errsStr);

                this._errors.AddRange(iErrors);
                this._State = TRunState.Error;

                if (iDownloader != null)
                {
                    iDownloader.Dispose();
                    GC.Collect();
                }
            }
        }
        /// <summary>
        /// 创建下载器(Url).
        /// </summary>
        /// <returns>The downloader by URL.</returns>
        /// <param name="iDownloadUrl">下载Url.</param>
        /// <param name="iOnStart">开始事件委托.</param>
        /// <param name="iOnSuccessed">成功事件委托.</param>
        /// <param name="iOnFailed">失败事件委托.</param>
        /// <param name="iType">下载对象类型.</param>
        public static DownloaderBase CreateDownloaderByUrl(string iUrl,
                                                           OnStart iOnStart, OnSuccessedByUrl iOnSuccessed, OnFailedByUrl iOnFailed,
                                                           TargetType iType)
        {
            DownloaderBase downloader  = null;
            TDownloadWay   downloadWay = ServersConf.GetInstance().DownloadWay;

            switch (downloadWay)
            {
            case TDownloadWay.WWW:
            {
                downloader = WWWDownloader.Create(
                    iUrl, iOnStart, iOnSuccessed, iOnFailed, iType);
            }
            break;

            case TDownloadWay.None:
            case TDownloadWay.Http:
            default:
            {
                downloader = HttpDownloader.Create(
                    iUrl, iOnStart, iOnSuccessed, iOnFailed, iType);
            }
            break;
            }
            return(downloader);
        }
Example #4
0
        /// <summary>
        /// 下载失败回调函数.
        /// </summary>
        /// <param name="iDownloadUrl">下载URL.</param>
        /// <param name="iErrors">错误信息.</param>
        public void OnDownloadFailedByUrl(DownloaderBase iDownloader, string iDownloadUrl, List <ErrorDetail> iErrors)
        {
            // 线程安全:添加错误信息至列表
            lock (_downloaderErrorLock) {
                string errsStr = null;
                foreach (ErrorDetail error in iErrors)
                {
                    if (string.IsNullOrEmpty(errsStr) == true)
                    {
                        errsStr = string.Format("Type:{0} State:{1} Detail:{2} Retries:{3}",
                                                error.Type.ToString(), error.State.ToString(), error.Detail, error.Retries.ToString());
                    }
                    else
                    {
                        errsStr = string.Format("{0} \n Type:{1} State:{2} Detail:{3} Retries:{4}",
                                                errsStr, error.Type.ToString(), error.State.ToString(), error.Detail, error.Retries.ToString());
                    }
                }

                UtilsLog.Error("OnDownloadFailedByUrl", "Download Failed!!! DownloadUrl:{0} Detail:{1}", iDownloadUrl, errsStr);

                this._errors.AddRange(iErrors);
                this._State = TRunState.Error;

                if (iDownloader != null)
                {
                    iDownloader.Dispose();
                    GC.Collect();
                }
            }
        }
        /// <summary>
        /// 下载上传列表.
        /// </summary>
        private IEnumerator DownloadUploadlist()
        {
            string         downloadUrl = ServersConf.GetInstance().GetDownloadUrlOfUploadList();
            DownloaderBase downloader  = this.CreateDownloader(downloadUrl);

            if (downloader != null)
            {
                downloader.ThreadDownLoadByUrl();
                yield return(new WaitUntil(() => (TRunState.Completed == downloader.State)));

                downloader.Dispose();
            }
            yield return(null);
        }
        /// <summary>
        /// 开始下载.
        /// </summary>
        private IEnumerator DownloadFiles()
        {
            TDownloadWay downloadWay = ServersConf.GetInstance().DownloadWay;

            while (this.DownloadQueue.Count > 0)
            {
                // 下载器个数控制
                if (this.DownloaderCount >= this.DownloaderMaxCount)
                {
                    yield return(new WaitForSeconds(1.0f));
                }

                // 下载出错则停止
                if (TRunState.OK != this._State)
                {
                    this.Error("DownloadFiles()::Download Failed!!! State:{0}", this._State.ToString());
                    // 取消现有下载线程
                    isCanceled = true;
                    yield break;
                }

                DownloaderBase downloader = this.DownloadQueue.Dequeue();
                if (downloader == null)
                {
                    continue;
                }
                // Bundle文件
                if (TDownloadWay.WWW == downloadWay)
                {
                    yield return(downloader.AsynDownLoadTarget());
                }
                else
                {
                    downloader.ThreadDownLoadTarget();
                }
                yield return(new WaitForEndOfFrame());

                // 下载出错则停止
                if (TRunState.OK != this._State)
                {
                    this.Error("DownloadFiles()::Download Failed!!! State:{0}", this._State.ToString());
                    // 取消现有下载线程
                    isCanceled = true;
                    yield break;
                }
            }
        }
        /// <summary>
        /// 初始化下载队列.
        /// </summary>
        private IEnumerator initDownloadQueue()
        {
            // 初始化清空
            this.DownloadQueue.Clear();

            List <DownloadTargetInfo> targets = DownloadList.GetInstance().Targets;

            DownloadTargetInfo[] downloadTargets = targets
                                                   .Where(o => (false == o.Downloaded))
                                                   .ToArray();
            if ((downloadTargets == null) || (downloadTargets.Length <= 0))
            {
                this._State = TRunState.NoDownloadTarget;
                this.Warning("initDownloadQueue()::There is no target to download!!!");
            }
            yield return(new WaitForEndOfFrame());

            if (TRunState.OK == this._State)
            {
                this.DownloaderCount = 0;
                int targetsCount = downloadTargets.Length;
                int maxCount     = ServersConf.GetInstance().ThreadMaxCount;
                this.DownloaderMaxCount = (targetsCount > maxCount) ? maxCount : targetsCount;
                // 遍历下载列表,并压进下载队列
                foreach (DownloadTargetInfo loop in downloadTargets)
                {
                    if (loop == null)
                    {
                        continue;
                    }

                    DownloaderBase downloader = CreateDownloader(loop);
                    if (downloader != null)
                    {
                        this.DownloadQueue.Enqueue(downloader);
                    }
                }
            }
        }
        /// <summary>
        /// 下载成功回调函数.
        /// </summary>
        /// <param name="iDownloadInfo">下载信息.</param>
        /// <param name="iRetries">重试次数.</param>
        public void OnDownloadSuccessed(DownloaderBase iDownloader, DownloadTargetInfo iDownloadInfo, int iRetries)
        {
            this.Info("OnDownloadSuccessed()::Download Successed. {0} Retries:{1}",
                      iDownloadInfo.toString(), iRetries);

            DownloadList.GetInstance().DownloadCompleted(iDownloadInfo.ID, iDownloadInfo.FileType);

            if (true == iDownloadInfo.Downloaded)
            {
                // 文件拷贝
                iDownloadInfo.CopyTargetWhenDownloadCompleted();

                if (iDownloader != null)
                {
                    iDownloader.Dispose();
                    GC.Collect();
                }

                lock (_downloaderCountLock) {
                    --this.DownloaderCount;
                }
            }
        }
 /// <summary>
 /// 下载成功回调函数.
 /// </summary>
 /// <param name="iDownloadUrl">下载URL.</param>
 public void OnDownloadSuccessedByUrl(DownloaderBase iDownloader, string iDownloadUrl)
 {
     this.Info("OnDownloadSuccessedByUrl()::Download Successed. DownloadUrl:{0}", iDownloadUrl);
 }
Example #10
0
 /// <summary>
 /// 下载成功回调函数.
 /// </summary>
 /// <param name="iDownloadUrl">下载URL.</param>
 public void OnDownloadSuccessedByUrl(DownloaderBase iDownloader, string iDownloadUrl)
 {
     UtilsLog.Info("OnDownloadSuccessedByUrl", "Download Successed. DownloadUrl:{0}", iDownloadUrl);
 }