示例#1
0
        /// <summary>
        /// Checks the WWW timeout.
        /// </summary>
        /// <param name="www">Www.</param>
        /// <param name="checkProgressSec">Check progress sec.</param>
        /// <param name="timeoutCallback">Timeout callback.</param>
        public static void addCheckWWWTimeout(UnityWebRequest www, string url,
                                              float checkProgressSec, object timeoutCallback, object orgs,
                                              int maxFailTimes, int failedTimes, RedCallback redrectioncallback)
        {
            if (www == null || www.isDone)
            {
                return;
            }
            self.enabled           = true;
            self.isCheckWWWTimeOut = true;
            checkProgressSec       = checkProgressSec <= 0 ? 5 : checkProgressSec;
            //UnityEngine.Coroutine cor = self.StartCoroutine(doCheckWWWTimeout(www, url, checkProgressSec, timeoutCallback, 0, orgs));
            NewList list = ObjPool.listPool.borrowObject();

            list.Add(url);
            list.Add(timeoutCallback);
            list.Add(checkProgressSec);
            list.Add(orgs);
            list.Add(0f);
            list.Add(0f);
            list.Add(Time.realtimeSinceStartup + checkProgressSec);
            list.Add(maxFailTimes);
            list.Add(failedTimes);
            list.Add(redrectioncallback);
            wwwMap4Check[www] = list;//DateEx.nowMS + checkProgressSec*1000;
        }
示例#2
0
        IEnumerator exeWWW(UnityWebRequest www, string url, CLAssetType type,
                           object successCallback,
                           object failedCallback, object orgs,
                           bool isCheckTimeout,
                           int maxFailTimes, int failedTimes,
                           RedCallback redrectioncallback = null)
        {
            if (isPrintUrl && url.StartsWith("http://"))
            {
                Debug.LogError(url);
            }

            wwwMapUrl[url] = www;
            if (isCheckTimeout)
            {
                addCheckWWWTimeout(www, url, checkTimeOutSec, failedCallback, orgs, maxFailTimes, failedTimes, redrectioncallback);
            }
            using (www)
            {
                yield return(www.SendWebRequest());

                try
                {
                    uncheckWWWTimeout(www, url);

                    if (www.isNetworkError || www.isHttpError)
                    {
                        long retCode = www.responseCode;
                        Debug.LogError(www.error + ",retCode==" + retCode + "," + url);
                        if (retCode == 300 || retCode == 301 || retCode == 302)
                        {
                            // 重定向处理
                            string url2 = www.GetResponseHeader("Location");
                            if (string.IsNullOrEmpty(url2))
                            {
                                Utl.doCallback(failedCallback, null, orgs);
                            }
                            else
                            {
                                if (redrectioncallback != null)
                                {
                                    redrectioncallback(url2);
                                }
                            }
                        }
                        else
                        {
                            if (maxFailTimes > failedTimes + 1)
                            {
                                redrectioncallback(url);
                            }
                            else
                            {
                                Utl.doCallback(failedCallback, null, orgs);
                            }
                        }
                    }
                    else
                    {
                        object content = null;
                        switch (type)
                        {
                        case CLAssetType.text:
                            content = www.downloadHandler.text;
                            break;

                        case CLAssetType.bytes:
                            content = www.downloadHandler.data;
                            break;

                        case CLAssetType.texture:
                            content = DownloadHandlerTexture.GetContent(www);
                            break;

                        case CLAssetType.assetBundle:
                            content = DownloadHandlerAssetBundle.GetContent(www);
                            break;
                        }
                        Utl.doCallback(successCallback, content, orgs, www);
                    }
                }
                catch (System.Exception e)
                {
                    Debug.LogError(e);
                    if (maxFailTimes > failedTimes + 1)
                    {
                        redrectioncallback(url);
                    }
                    else
                    {
                        Utl.doCallback(failedCallback, null, orgs);
                    }
                }
            }

            wwwMap4Get.Remove(url);
            wwwMapUrl.Remove(url);

            if (www != null)
            {
                www.Dispose();
                www = null;
            }
        }
示例#3
0
        public static void doCheckWWWTimeout(UnityWebRequest www, NewList list)
        {
            //yield return new WaitForSeconds(checkProgressSec);
            string      url                = list[0] as string;
            object      timeoutCallback    = list[1];
            float       checkProgressSec   = (float)(list[2]);
            object      orgs               = list[3];
            float       oldProgress        = (float)(list[4]);
            float       oldSize            = (float)(list[5]);
            float       lastCheckTime      = (float)(list[6]);
            int         maxFailTimes       = (int)(list[7]);
            int         failedTimes        = (int)(list[8]);
            RedCallback redrectioncallback = list[9] as RedCallback;

            if (Time.realtimeSinceStartup - lastCheckTime < 0)
            {
                return;
            }
            try
            {
                if (www != null)
                {
                    if (www.isDone)
                    {
                        wwwMap4Check.Remove(www);
                        list.Clear();
                        ObjPool.listPool.returnObject(list);
                    }
                    else
                    {
                        float curProgress = 0;
                        float curSize     = 0;
                        if (www.method == "PUT")
                        {
                            curProgress = www.uploadProgress;
                            if (www.uploadHandler != null && www.uploadHandler.data != null)
                            {
                                curSize = www.uploadHandler.data.Length;
                            }
                        }
                        else
                        {
                            curProgress = www.downloadProgress;
                            if (www.downloadHandler != null && www.downloadHandler.data != null)
                            {
                                curSize = www.downloadHandler.data.Length;
                            }
                        }

                        if (Mathf.Abs(curProgress - oldProgress) < 0.0001f &&
                            Mathf.Abs(curSize - oldSize) < 0.0001f)
                        {
                            //说明没有变化,可能网络不给力
                            if (maxFailTimes > failedTimes + 1)
                            {
                                if (redrectioncallback != null)
                                {
                                    redrectioncallback(url);
                                }
                            }
                            else
                            {
                                Coroutine corout = wwwMap4Get[url] as Coroutine;
                                if (corout != null)
                                {
                                    self.StopCoroutine(corout);
                                }
                                wwwMap4Get.Remove(url);
                                wwwMapUrl.Remove(url);

                                list.Clear();
                                ObjPool.listPool.returnObject(list);
                                wwwMap4Check.Remove(www);

                                www.Abort();
                                www.Dispose();
                                www = null;
                                Debug.LogError("www time out! url==" + url);
                                Utl.doCallback(timeoutCallback, null, orgs);
                            }
                        }
                        else
                        {
                            //Coroutine cor = self.StartCoroutine(doCheckWWWTimeout(www, url, checkProgressSec, timeoutCallback, curProgress, orgs));
                            list[4]           = curProgress;
                            list[5]           = curSize;
                            list[6]           = Time.realtimeSinceStartup + checkProgressSec;
                            wwwMap4Check[www] = list;
                        }
                    }
                }
            }
            catch (System.Exception e)
            {
                Debug.LogError(e);
            }
        }