Exemplo n.º 1
0
        public void DownLoadAssetBundles(string server, string version, string[] assetBundles, string[] chekfileMd5s)
        {
            for (int i = 0; i < assetBundles.Length; ++i)
            {
                var key = string.Format(version + "_" + assetBundles[i]);
                if (mDownLoadingHandlers.ContainsKey(key))
                {
                    continue;
                }

                var bundleName = assetBundles[i];
                var url        = Function.getAssetBundleDownloadUrl(server, version, bundleName);
                Debug.LogFormat("<color=#ff00ff>[download]</color>:url:[<color=#00ffff>{0}</color>]", url);
                var storepath = Function.getAssetBundlePersistentPath(version, string.Empty, false);
                Debug.LogFormat("<color=#ff00ff>[download]</color>:storepath:[<color=#00ffff>{0}</color>]", storepath);

                var handler = HttpDownLoadHandle.Get(url, storepath, assetBundles[i], chekfileMd5s[i], () =>
                {
                    Debug.LogFormat("<color=#ff00ff>[download]</color>:bundleName:[<color=#00ffff>{0}</color>] succeed", bundleName);
                },
                                                     () =>
                {
                    Debug.LogFormat("<color=#ff00ff>[download]</color>:bundleName:[<color=#ff0000>{0}</color>] failed", bundleName);
                });

                mDownLoadingHandlers.Add(key, handler);
            }
        }
Exemplo n.º 2
0
        public static HttpDownLoadHandle Get(string url, string storepath, string fileName, string checkMd5, UnityAction onSucceed, UnityAction onFailed)
        {
            var key = System.IO.Path.Combine(url, fileName);

            if (null == threads[0])
            {
                for (int i = 0; i < threads.Length; ++i)
                {
                    threads[i] = new Thread(Run);
                    var thread = threads[i];
                    thread.IsBackground = true;
                    thread.Priority     = System.Threading.ThreadPriority.BelowNormal;
                    threads[i].Start();
                }
            }

            lock (lock_obj)
            {
                if (downloadList.ContainsKey(key))
                {
                    return(downloadList[key]);
                }
            }

            HttpDownLoadHandle downLoadHandler = new HttpDownLoadHandle();

            downLoadHandler.progress    = 0.0f;
            downLoadHandler.isDone      = false;
            downLoadHandler.url         = url;
            downLoadHandler.storepath   = storepath;
            downLoadHandler.onSucceed   = onSucceed;
            downLoadHandler.fileName    = fileName;
            downLoadHandler.checkMd5    = checkMd5;
            downLoadHandler.onFailed    = onFailed;
            downLoadHandler.downloadCnt = maxDownLoadCnt;

            lock (lock_obj)
            {
                downloadList.Add(key, downLoadHandler);
                unstartedList.Add(downLoadHandler);
            }

            return(downLoadHandler);
        }
Exemplo n.º 3
0
 protected void OnDestroy()
 {
     HttpDownLoadHandle.Abort();
 }
Exemplo n.º 4
0
 protected void Update()
 {
     HttpDownLoadHandle.Update();
     UpdateProcess();
     UpdateDownLoadProgress();
 }
Exemplo n.º 5
0
        protected static void Run()
        {
            while (true)
            {
                if (unstartedList.Count <= 0)
                {
                    Thread.Sleep(150);
                    continue;
                }

                HttpDownLoadHandle handler = null;
                lock (lock_obj)
                {
                    if (unstartedList.Count > 0)
                    {
                        handler = unstartedList[0];
                        unstartedList.RemoveAt(0);
                    }
                }

                if (null == handler)
                {
                    continue;
                }

                if (!Directory.Exists(handler.storepath))
                {
                    Directory.CreateDirectory(handler.storepath);
                }

                var filePath = System.IO.Path.Combine(handler.storepath, handler.fileName);
                LogData.LogFormat("[文件下载]:[{0}]:第[{1}]次", handler.fileName, maxDownLoadCnt - handler.downloadCnt + 1);
                LogData.LogFormat("[文件下载]:[{0}]:[{1}]", handler.fileName, filePath);

                try
                {
                    using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                    {
                        long fileLength  = fs.Length;
                        long totalLength = HttpGetFileLength(handler.url);
                        LogData.LogFormat("[文件下载]:[{0}]:远端文件长度[{1}]:本地文件长度[{2}]", handler.fileName, totalLength, fileLength);

                        bool needReCheck = true;
                        if (fileLength == totalLength)
                        {
                            LogData.LogFormat("[文件下载]:[{0}]:文件长度相同", handler.fileName);
                            if (!VerifyFileMd5(fs, handler.fileName, handler.checkMd5))
                            {
                                fs.SetLength(0);
                                fileLength           = fs.Length;
                                needReCheck          = false;
                                handler.downloadCnt -= 1;
                                handler.progress     = 0.0f;
                                handler.isDone       = false;
                                LogData.LogFormat("[文件下载]:[{0}]:校验文件MD5码失败,需要重新下载,扔回队列等待重下", handler.fileName);

                                if (handler.downloadCnt <= 0)
                                {
                                    LogData.LogErrorFormat("[文件下载]:[{0}]:重下次数已经用完", handler.fileName);
                                    var actionFailed = handler.onFailed;
                                    handler.onFailed = null;
                                    lock (lock_obj)
                                    {
                                        downloadActions.Add(actionFailed);
                                    }
                                    continue;
                                }

                                fs.Flush();
                                fs.Close();

                                lock (lock_obj)
                                {
                                    unstartedList.Add(handler);
                                }
                                continue;
                            }
                            else
                            {
                                LogData.LogFormat("[文件下载]:[{0}]:校验文件MD5码成功,不需要重新下载", handler.fileName);
                                handler.progress = 1.0f;
                                handler.isDone   = true;
                                var actionSucceed = handler.onSucceed;
                                needReCheck = false;
                                lock (lock_obj)
                                {
                                    downloadActions.Add(() =>
                                    {
                                        if (null != actionSucceed)
                                        {
                                            actionSucceed.Invoke();
                                        }
                                    });
                                }
                                continue;
                            }
                        }
                        else if (fileLength > totalLength)
                        {
                            LogData.LogFormat("[文件下载]:[{0}]:校验文件长度不对,需要重新下载", handler.fileName);
                            fs.SetLength(0);
                            fileLength  = fs.Length;
                            needReCheck = false;
                        }
                        else
                        {
                            if (fileLength == 0)
                            {
                                needReCheck = false;
                            }
                            else
                            {
                                needReCheck = true;
                            }
                        }

                        fs.Seek(fileLength, SeekOrigin.Begin);

                        HttpWebRequest request = HttpWebRequest.Create(handler.url) as HttpWebRequest;
                        request.ReadWriteTimeout = ReadWriteTimeOut;
                        request.Timeout          = TimeOutWait;

                        if (fileLength > 0)
                        {
                            request.AddRange((int)fileLength);
                        }

                        bool checkOk = false;
                        using (Stream stream = request.GetResponse().GetResponseStream())
                        {
                            byte[] buffer = new byte[1024];
                            int    length = stream.Read(buffer, 0, buffer.Length);
                            while (length > 0)
                            {
                                //如果Unity客户端关闭,停止下载
                                //if (isStop) break;
                                //将内容再写入本地文件中
                                fs.Write(buffer, 0, length);
                                //计算进度
                                fileLength      += length;
                                handler.progress = (float)fileLength / (float)totalLength;
                                //类似尾递归
                                length = stream.Read(buffer, 0, buffer.Length);
                            }

                            stream.Close();
                        }

                        if (needReCheck)
                        {
                            LogData.LogFormat("[文件下载]:[{0}]:需要重新校验", handler.fileName);
                            checkOk = VerifyFileMd5(fs, handler.fileName, handler.checkMd5);
                        }
                        else
                        {
                            LogData.LogFormat("[文件下载]:[{0}]:不需要重新校验", handler.fileName);
                            checkOk = true;
                        }

                        fs.Flush();
                        fs.Close();

                        if (checkOk)
                        {
                            LogData.LogFormat("[文件下载]:[{0}]:文件下载成功", handler.fileName);
                            handler.progress = 1.0f;
                            handler.isDone   = true;
                            lock (lock_obj)
                            {
                                downloadActions.Add(handler.onSucceed);
                                handler.onSucceed = null;
                            }
                        }
                        else
                        {
                            LogData.LogFormat("[文件下载]:[{0}]:重新校验文件失败,扔回队列等待重下", handler.fileName);
                            handler.downloadCnt -= 1;
                            handler.progress     = 0.0f;
                            handler.isDone       = false;
                            fs.SetLength(0);

                            if (handler.downloadCnt <= 0)
                            {
                                LogData.LogErrorFormat("[文件下载]:[{0}]:重下次数已经用完", handler.fileName);
                                var actionFailed = handler.onFailed;
                                handler.onFailed = null;
                                handler.isDone   = true;
                                lock (lock_obj)
                                {
                                    downloadActions.Add(actionFailed);
                                }
                                continue;
                            }

                            lock (lock_obj)
                            {
                                unstartedList.Add(handler);
                            }
                        }
                    }
                }
                catch (System.Exception e)
                {
                    LogData.LogErrorFormat("[文件下载]:[{0}]:下载异常:[{1}]", handler.fileName, e.Message);
                    handler.downloadCnt -= 1;

                    if (handler.downloadCnt <= 0)
                    {
                        LogData.LogErrorFormat("[文件下载]:[{0}]:下载异常:重下次数已经用完", handler.fileName);
                        handler.isDone   = true;
                        handler.progress = 0.0f;
                        var actionFailed = handler.onFailed;
                        handler.onFailed = null;
                        lock (lock_obj)
                        {
                            downloadActions.Add(actionFailed);
                        }
                    }
                    else
                    {
                        LogData.LogErrorFormat("[文件下载]:[{0}]:下载异常:扔回队列等待重下", handler.fileName);
                        lock (lock_obj)
                        {
                            unstartedList.Add(handler);
                        }
                    }
                }
            }
        }