Пример #1
0
        /// <summary>
        /// 增加download下载任务(可等待)
        /// </summary>
        public CFTask <DownloadAwaitEventArgs> AwaitAddDownload(string downloadPath, string downloadUri, int priority, object userData)
        {
            if (string.IsNullOrEmpty(downloadPath))
            {
                throw new Exception("Download path is invalid.");
            }

            if (string.IsNullOrEmpty(downloadUri))
            {
                throw new Exception("Download uri is invalid.");
            }

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

            var tcs = new CFTaskCompletionSource <DownloadAwaitEventArgs>();

            DownloadTask downloadTask = DownloadTask.AwaitCreate(downloadPath, downloadUri, priority, m_FlushSize, m_Timeout, userData, (DownloadPath) =>
            {
                try
                {
                    tcs.TrySetResult(DownloadPath);
                }
                catch (Exception e)
                {
                    tcs.TrySetException(new Exception($"Rpc Error: {DownloadPath.GetType().FullName}", e));
                }
            });

            m_TaskPool.AddTask(downloadTask);

            return(tcs.Task);
        }
Пример #2
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);
        }