예제 #1
0
        void TimeOutEvent(object o, int left)
        {
            WWW passwww = o as WWW;

            if (passwww != null)
            {
                tiemoutList.Add(passwww);

                if (binders.ContainsKey(passwww))
                {
                    var callback = binders[passwww];
                    if (callback != null)
                    {
                        HttpRespMessage resp = new HttpRespMessage();
                        resp.url       = passwww.url;
                        resp.errorType = NetError.TimeOut;

                        callback(resp);
                    }

                    binders.Remove(passwww);
                }

                passwww.Dispose();

                LogMgr.LogErrorFormat("WWW Http timeout :{0}", this);
            }
        }
예제 #2
0
        void ThreadTask(object obj)
        {
            KeyValuePair <NetMessage, Action <NetMessage> > tuple = default(KeyValuePair <NetMessage, Action <NetMessage> >);

            lock (lockObject)
            {
                tuple = httpqueue.Dequeue();
            }

            HttpMessage         httpmsg  = (HttpMessage)tuple.Key;
            Action <NetMessage> callback = tuple.Value;
            HttpWebRequest      request  = null;
            HttpWebResponse     response = null;
            Stream responseStream        = null;

            try
            {
                request = new HttpWebRequest(new Uri(httpmsg.url));
                if (httpmsg.method == HttpConnectionType.Post)
                {
                    request.Accept = "text/html, application/xhtml+xml, */*";
                    request.Method = "POST";
                    SimpleClsDictionary <string, string> fields = httpmsg.getFields();
                    if (fields != null && fields.Count > 0)
                    {
                        byte[] btBodys = System.Text.Encoding.UTF8.GetBytes(httpmsg.ToFieldString());
                        using (Stream respSb = request.GetRequestStream())
                        {
                            respSb.Write(btBodys, 0, btBodys.Length);
                        }
                    }
                }
                else
                {
                    request.Method      = "GET";
                    request.Accept      = "*/*";
                    request.ContentType = "application/x-www-form-urlencoded,application/json";
                }

                request.Timeout = (int)this.timeout;
                byte[] allbys = null;

                using (response = (HttpWebResponse)request.GetResponse())
                {
                    allbys = new byte[response.ContentLength];
                    int readpos = 0;
                    int readlen = 1;

                    using (responseStream = response.GetResponseStream())
                    {
                        while (readlen > 0 && !needclose && readpos < allbys.Length)
                        {
                            readlen  = responseStream.Read(allbys, readpos, Mathf.Min(allbys.Length - readpos, readconst));
                            readpos += readlen;
//#if UNITY_EDITOR
//                            LogMgr.LogFormat("下载:{0} /{1}", readpos, allbys.Length);
//#endif
                            if (callback != null)
                            {
                                HttpRespMessage respmsg = new HttpRespMessage();
                                respmsg.url      = httpmsg.url;
                                respmsg.total    = allbys.Length;
                                respmsg.reiceved = readpos;
                                respmsg.bys      = allbys;
                                lock (lockObject)
                                {
                                    this.receivedqueue.Enqueue(new KeyValuePair <Action <NetMessage>, NetMessage>(callback, respmsg));
                                }
                            }
                        }
                        // if(FrameWorkConfig.Open_DEBUG)
#if UNITY_EDITOR
                        LogMgr.Log("下载结束");
#endif
                    }
                }
            }
            catch (Exception ex)
            {
                LogMgr.LogError(ex);
                HttpRespMessage respmsg = new HttpRespMessage();
                respmsg.url       = httpmsg.url;
                respmsg.errorType = NetError.NetException;
                lock (lockObject)
                {
                    this.receivedqueue.Enqueue(new KeyValuePair <Action <NetMessage>, NetMessage>(callback, respmsg));
                }
            }
            finally
            {
                if (request != null)
                {
                    request.Abort();
                }

                if (response != null)
                {
                    response.Close();
                }

                if (responseStream != null)
                {
                    responseStream.Close();
                }

                this.ConnectionClosed(this);
            }
        }
예제 #3
0
        IEnumerator SendWWW(HttpMessage httpmsg, Action <NetMessage> msgCallback = null)
        {
            WWW localWWW = null;

            if (httpmsg.method == HttpConnectionType.Post)
            {
                WWWForm form = new WWWForm();
                SimpleClsDictionary <string, string> dicts = httpmsg.getFields();

                if (dicts != null)
                {
                    List <string> keys = dicts.Keys;
                    for (int i = 0; i < keys.Count; ++i)
                    {
                        form.AddField(keys[i], dicts[keys[i]]);
                    }
                }

                if (FrameWorkConfig.Open_DEBUG)
                {
                    LogMgr.LogFormat("发送http 请求 :{0}", httpmsg.url);
                }

                localWWW         = www = new WWW(httpmsg.url, form);
                this.isConnected = true;

                binders[localWWW] = msgCallback;
                yield return(localWWW);
            }
            else
            {
                if (FrameWorkConfig.Open_DEBUG)
                {
                    LogMgr.LogFormat("发送http 请求 :{0}", httpmsg.url);
                }
                localWWW          = www = new WWW(httpmsg.url);
                this.isConnected  = true;
                binders[localWWW] = msgCallback;
                yield return(localWWW);
            }

            if (localWWW != null && !tiemoutList.Remove(localWWW))
            {
                binders.Remove(localWWW);
                if (string.IsNullOrEmpty(localWWW.error))
                {
                    Schedule.mIns.UnScheduleInvoke(TimeOutEvent);

                    HttpRespMessage resp = new HttpRespMessage();
                    resp.url      = httpmsg.url;
                    resp.content  = localWWW.text;
                    resp.texture  = localWWW.texture;
                    resp.bys      = localWWW.bytes;
                    resp.total    = resp.bys == null ? 0 : resp.bys.Length;
                    resp.reiceved = resp.total;

                    MessageArrived(this, resp);

                    if (msgCallback != null)
                    {
                        msgCallback(resp);
                    }
                }
                else
                {
                    LogMgr.LogErrorFormat("Www Error :{0} to {1}", localWWW.error, localWWW.url);
                    Schedule.mIns.UnScheduleInvoke(TimeOutEvent);
                    if (wwwreferences.ContainsKey(httpmsg.url))
                    {
                        int value = wwwreferences[httpmsg.url];
                        if (value < ErrorRetryTime)
                        {
                            wwwreferences[httpmsg.url] = value + 1;
                            //retry
                            SendMessage(httpmsg, msgCallback);
                        }
                        else
                        {
                            wwwreferences.RemoveKey(httpmsg.url);

                            HttpRespMessage resp = new HttpRespMessage();
                            resp.url       = httpmsg.url;
                            resp.errorType = NetError.NetException;

                            if (msgCallback != null)
                            {
                                msgCallback(resp);
                            }
                        }
                    }
                    else
                    {
                        wwwreferences.Add(httpmsg.url, 0);
                    }
                }

                this.isConnected = false;

                localWWW.Dispose();
                localWWW = null;
            }
        }