コード例 #1
0
        /// <summary>
        /// 添加异步Web请求任务
        /// </summary>
        /// <param name="webRequestUri"></param>
        /// <param name="postData"></param>
        /// <param name="priority"></param>
        /// <returns></returns>
        public CFTask <WebRequestAwaitEventArgs> AwaitWebRequest(string webRequestUri, byte[] postData, int priority = ConstData.Priority.Default)
        {
            if (string.IsNullOrEmpty(webRequestUri))
            {
                throw new Exception("Web request uri is invalid.");
            }

            if (TotalAgentCount <= 0)
            {
                throw new Exception("You must add web request agent first.");
            }

            var tcs = new CFTaskCompletionSource <WebRequestAwaitEventArgs>();

            WebRequestTask webRequestTask = WebRequestTask.Create(webRequestUri, postData, priority, m_Timeout, (WebResponse) =>
            {
                try
                {
                    tcs.TrySetResult(WebResponse);
                }
                catch (Exception e)
                {
                    tcs.TrySetException(new Exception($"Rpc Error: {WebResponse.GetType().FullName}", e));
                }
            });

            m_TaskPool.AddTask(webRequestTask);

            return(tcs.Task);
        }
コード例 #2
0
ファイル: WebRequestTask.cs プロジェクト: wangjie0707/Temp_01
        /// <summary>
        /// 异步创建 Web 请求任务。
        /// </summary>
        /// <param name="webRequestUri">要发送的远程地址。</param>
        /// <param name="postData">用户自定义数据</param>
        /// <param name="priority">Web 请求任务的优先级。</param>
        /// <param name="timeout">下载超时时长,以秒为单位。</param>
        /// <param name="action">回调</param>
        /// <returns></returns>
        public static WebRequestTask Create(string webRequestUri, byte[] postData, int priority, float timeout, Action <WebRequestAwaitEventArgs> action)
        {
            WebRequestTask webRequestTask = ReferencePool.Acquire <WebRequestTask>();

            webRequestTask.Initialize(++s_Serial, priority);
            webRequestTask.m_WebRequestUri = webRequestUri;
            webRequestTask.m_PostData      = postData;
            webRequestTask.m_Timeout       = timeout;
            webRequestTask.m_AsyncAction   = action;
            return(webRequestTask);
        }