Example #1
0
        private IEnumerator Request(string serverUrl, string cmd, byte[] data, Action <T> succCallback,
                                    Action <HttpErrorVo> errCallback, bool cache, string version, bool ignoreResponse)
        {
            bool hasTemp  = false;
            var  pathName = cmd.Split('/');
            var  path1    = Util.ToUnderlineName(pathName[0]);
            var  path2    = Util.ToUnderlineName(pathName[1]);

            //缓存数据
            if (cache)
            {
                byte[] dataFile = null;
                if (version == AppConfig.Instance.cacheVersion)
                {
                    dataFile = new AssetLoader().LoadBytes(
                        AssetLoader.GetProtoCachePath(path1 + "/" + path2 + ".info"));
                    Debug.Log("<color='#9900ff'>Load from ProtoCache:" + path1 + "/" + path2 + ".info</color>");
                }
                else
                {
                    dataFile = FileUtil.GetBytesFile(AssetLoader.CachePath + path1, path2 + version + ".info");
                }

                if (dataFile != null)
                {
                    Stopwatch sw = Stopwatch.StartNew();
                    Loom.RunAsync(() =>
                    {
                        MemoryStream m           = new MemoryStream(dataFile);
                        MessageParser <T> parser = new MessageParser <T>(() => new T());
                        var res = parser.ParseFrom(m);

                        var field = res.Descriptor.FindFieldByName("ret");
                        int ret   = (int)field.Accessor.GetValue(res);
                        Debug.Log("线程处理缓存数据时间1:" + sw.ElapsedMilliseconds);
                        Loom.QueueOnMainThread(() =>
                        {
                            sw.Stop();
                            Debug.Log("线程处理缓存数据时间2:" + sw.ElapsedMilliseconds);
                            if (ret == 0 || ret == -1)
                            {
                                succCallback(res);
                            }
                        });
                    });

                    hasTemp = true;
                }
            }

            if (!hasTemp)
            {
                var uri = serverUrl + cmd;
                Debug.Log("connect==>" + uri);

                UnityWebRequest www = new UnityWebRequest(uri, UnityWebRequest.kHttpVerbPOST);

                www.uploadHandler   = new UploadHandlerRaw(data);
                www.downloadHandler = new DownloadHandlerBuffer();


                StringBuilder sb = new StringBuilder();
                foreach (var header in NetWorkManager.Headers)
                {
                    sb.AppendLine("Key:" + header.Key + " Value:" + header.Value);
                    www.SetRequestHeader(header.Key, header.Value);
                }

                if (AppConfig.Instance.isTestMode)
                {
                    Debug.LogWarning("HttpMsg=> SetRequestHeader" + sb.ToString());
                }

                www.useHttpContinue = false;
                if (HttpTimeOut > 0)
                {
                    www.timeout = HttpTimeOut;
                }
                else
                {
                    www.timeout = NetWorkManager.HttpTimeOut;
                }

                if (www.timeout > 15)
                {
                    Debug.LogWarning("Http Time===>" + www.timeout + " Url=>" + uri);
                }

                string str = www.GetRequestHeader("Cookie");

                yield return(www.SendWebRequest());

                if (www.isNetworkError || www.isHttpError)
                {
                    Debug.LogError("<color=#00CAFFFF>" + "www_error:" + www.error + " Uri:" + uri + "</color> ");

                    if (_retryTimes < MaxRetryTimes && AutoRetry)
                    {
                        _retryTimes++;
                        Debug.LogError(www.error + "==RetryTimes:" + _retryTimes);
                        yield return(new WaitForSeconds(retryInterval));

                        RequestHandler();
                        yield break;
                    }
                    else
                    {
                        Debug.LogError("网络异常,重试" + _retryTimes + "次  " + uri);
                    }

                    HttpErrorVo vo = new HttpErrorVo
                    {
                        ErrorCode   = 0,
                        Cmd         = _cmd,
                        CustomData  = CustomerData,
                        ErrorString = www.error
                    };
                    errCallback?.Invoke(vo);

                    www.Dispose();

                    yield break;
                }
                else
                {
                    var bytes = www.downloadHandler.data;

                    MemoryStream      m      = new MemoryStream(bytes);
                    MessageParser <T> parser = new MessageParser <T>(() => new T());
                    var res   = parser.ParseFrom(m);
                    var field = res.Descriptor.FindFieldByName("ret");
                    int ret   = (int)field.Accessor.GetValue(res);

                    if (ret == 0 || ret == -1 || ret == ErrorCode.USER_NOT_CARD_CODE ||
                        ret == ErrorCode.SERVER_TOURIST_NOT_RECHARGE ||
                        ret == ErrorCode.SERVER_RECHARGE_UPPERLIMIT ||
                        ret == ErrorCode.SERVER_NOT_OPPEN_RECHARGE)
                    {
                        if (NetWorkManager.CookieStr == null)
                        {
                            Dictionary <string, string> responseHeaders = null;
                            try
                            {
                                responseHeaders = www.GetResponseHeaders();
                            }
                            catch (Exception e)
                            {
                                Debug.LogError(e.Message);
                            }

                            if (responseHeaders != null && responseHeaders.ContainsKey("SET-COOKIE"))
                            {
                                var cookie = responseHeaders["SET-COOKIE"];
                                if (NetWorkManager.CookieStr != null && cookie != NetWorkManager.CookieStr ||
                                    NetWorkManager.CookieStr == null)
                                {
                                    NetWorkManager.CookieStr = responseHeaders["SET-COOKIE"];
                                    NetWorkManager.InitHead();
                                }
                            }
                        }

                        if (cache)
                        {
                            FileUtil.SaveBytesFile(AssetLoader.CachePath + path1, path2 + version + ".info", bytes);
                        }

                        succCallback(res);
                    }
                    else
                    {
                        Debug.Log("<color=#00CAFFFF>" + "Error code:::::::" + ret + "        >>> " + uri + "</color> ");

                        HttpErrorVo vo = new HttpErrorVo
                        {
                            ErrorCode  = ret,
                            Cmd        = _cmd,
                            CustomData = CustomerData,
                        };
                        errCallback?.Invoke(vo);
                    }
                }

                www.Dispose();
            }
        }
Example #2
0
 void Awake()
 {
     _instance            = this;
     MonoGameObjectScript = transform.gameObject.AddComponent <MonoObject>();
     InitHead();
 }