Exemplo n.º 1
0
        // 保证OpenUrl在主线程调用
        public static HttpClient OpenUrl <T>(string url, T listener, long filePos, Action <HttpClient, HttpListenerStatus> OnEnd = null,
                                             Action <HttpClient> OnProcess = null, float connectTimeOut = 5.0f, float readTimeOut = 5.0f, string postStr = "", bool isKeepAlive = true, List <X509Certificate> certs = null) where T : HttpClientResponse
        {
            if (string.IsNullOrEmpty(url) || listener == null || filePos < 0)
            {
                return(null);
            }

            HttpClient         ret      = CreateHttpClient();
            HttpClientCallBack callBack = null;

            if (OnEnd != null || OnProcess != null)
            {
                callBack           = new HttpClientCallBack();
                callBack.OnEnd     = OnEnd;
                callBack.OnProcess = OnProcess;
            }
            ret.UserData = callBack;
            byte[] postBuf = null;
            if (!string.IsNullOrEmpty(postStr))
            {
                try {
                    postBuf = System.Text.Encoding.UTF8.GetBytes(postStr);
                } catch {
                    postBuf = null;
                }
            }

            HttpClientType clientType = postBuf != null ? HttpClientType.httpPost : HttpClientType.httpGet;

            ret.Init(url, listener, filePos, connectTimeOut, readTimeOut, clientType, postBuf, isKeepAlive, certs);
            m_LinkList.AddLast(ret.LinkNode);

            return(ret);
        }
Exemplo n.º 2
0
        // 保证OpenUrl在主线程调用
        public static HttpClient OpenUrl <T>(string url, T listener, long filePos, Action <HttpClient, HttpListenerStatus> OnEnd = null, Action <HttpClient> OnProcess = null, float timeOut = 5.0f) where T : HttpClientResponse
        {
            if (string.IsNullOrEmpty(url) || listener == null || filePos < 0)
            {
                return(null);
            }

            HttpClient         ret      = CreateHttpClient();
            HttpClientCallBack callBack = null;

            if (OnEnd != null || OnProcess != null)
            {
                callBack           = new HttpClientCallBack();
                callBack.OnEnd     = OnEnd;
                callBack.OnProcess = OnProcess;
            }
            ret.UserData = callBack;
            ret.Init(url, listener, filePos, timeOut);
            m_LinkList.AddLast(ret.LinkNode);

            return(ret);
        }
Exemplo n.º 3
0
        private static void OnTimerEvent(Timer obj, float timer)
        {
            int cnt  = m_LinkList.Count;
            int tick = 0;
            var node = m_LinkList.First;

            while (node != null)
            {
                if (tick >= cnt || tick >= 5)
                {
                    break;
                }

                var next = node.Next;

                if (node.Value.Listener == null || node.Value.Listener.IsEnd)
                {
                    HttpClientCallBack callBack = node.Value.UserData as HttpClientCallBack;
                    if (callBack != null)
                    {
                        if (node.Value.Listener != null)
                        {
                            // 再调用一次Process
                            if (callBack.OnProcess != null)
                            {
                                callBack.OnProcess(node.Value);
                            }

                            if (callBack.OnEnd != null)
                            {
                                callBack.OnEnd(node.Value, node.Value.Listener.Status);
                            }
                        }
                        else
                        {
                            // 外部主动调用关闭
                            callBack.OnEnd(node.Value, HttpListenerStatus.hsClosed);
                        }
                    }

                    m_LinkList.Remove(node);
                    // 断开HttpClient和Listener之间的链接
                    InPool(node.Value);
                }
                else
                {
                    HttpClientCallBack callBack = (HttpClientCallBack)node.Value.UserData;
                    if (callBack != null && callBack.OnProcess != null)
                    {
                        callBack.OnProcess(node.Value);
                    }

                    m_LinkList.Remove(node);
                    m_LinkList.AddLast(node);
                }

                node = next;

                ++tick;
            }
        }