상속: ICancelable
예제 #1
0
        public IEnumerator ExecuteCoroutine(TaskExecutionContext context, CancellationToken cancellationToken = null)
        {
            PSAssert.Validate(IsCoroutine, "ExecuteCoroutine called on action");

            _cancellationToken = cancellationToken ?? new CancellationToken();
            return DoExecuteCoroutine(context);
        }
예제 #2
0
        public void Execute(TaskExecutionContext context, CancellationToken cancellationToken = null)
        {
            PSAssert.Validate(!IsCoroutine, "Execute called on coroutine");

            _cancellationToken = cancellationToken ?? new CancellationToken();
            DoExecute(context);
        }
예제 #3
0
        public IEnumerator DoGet(string path, Hash urlParams, CancellationToken cancellationToken, bool verifyResponse, Action<Dictionary<string, object>> responseCallback)
        {
            if (IsCanceled)
            {
            return Enumerable.Empty<object>().GetEnumerator();
            }
            else
            {
            PSDebug.Log("Performing Request: {0}", path);
            if (responseCallback == null)
            {
                responseCallback = DevNullCallback;
            }

            var url = (urlParams != null) ? path + '?' + WwwHelper.CreateQueryString(urlParams) : path;
            var www = new WWW(AbsURL(url), null, CreateRequestHeaders(path, null, urlParams));

            return DoRequest(www, cancellationToken, verifyResponse, responseCallback);
            }
        }
예제 #4
0
        public static IEnumerator GetResponse(WWW www, CancellationToken cancellationToken, string expectedHash = null, Action<float> reportProgress = null)
        {
            var monitor = new WWWConnectionMonitor(www);
            var progress = 0.0f;

            while (!www.isDone && !cancellationToken.IsCanceled)
            {
            monitor.AssertLiveliness();

            if (reportProgress != null && www.progress != progress)
            {
                progress = www.progress;
                reportProgress(progress);
            }
            yield return null;
            }

            if (reportProgress != null)
            {
            reportProgress(www.progress);
            }

            if (cancellationToken.IsCanceled) yield break;

            if (!string.IsNullOrEmpty(www.error))
            {
            throw new WwwErrorException(www);
            }

            if (expectedHash != null)
            {
            using (var md5 = MD5.Create())
            {
                if (expectedHash != FileHelper.GetFingerprint(md5.ComputeHash(www.bytes)))
                {
                    throw new WwwErrorException(HttpStatusCode.ExpectationFailed);
                }
            }
            }
        }
예제 #5
0
        public IEnumerator DoPostData(string path, string postDataBody, CancellationToken cancellationToken, bool verifyResponse, Action<Dictionary<string, object>> responseCallback = null)
        {
            if (IsCanceled)
            {
            return Enumerable.Empty<object>().GetEnumerator();
            }
            else
            {
            PSDebug.Log("Performing Request: {0}, {1}", path, postDataBody);
            if (responseCallback == null)
            {
                responseCallback = DevNullCallback;
            }

            var headers = CreateRequestHeaders(path, postDataBody);
            headers["Content-Type"] = "application/json";

            var www = new WWW(AbsURL(path), Encoding.UTF8.GetBytes(postDataBody), headers);

            return DoRequest(www, cancellationToken, verifyResponse, responseCallback);
            }
        }
예제 #6
0
        private IEnumerator DoRequest(WWW www, CancellationToken cancellationToken, bool verifyResponse, Action<Dictionary<string, object>> responseCallback)
        {
            using (www)
            {
            var monitor = new WWWConnectionMonitor(www);

            while (!www.isDone && !cancellationToken.IsCanceled)
            {
                monitor.AssertLiveliness();
                yield return null;
            }

            if (cancellationToken.IsCanceled)
            {
                yield break;
            }

            if (!string.IsNullOrEmpty(www.error))
            {
                throw new WwwErrorException(www);
            }

            PSDebug.Log("Request returned: {0}, text: {1}", www.url, www.text);
            var response = ResponseAsDictionary(www);
            if (verifyResponse)
            {
                var password = JSONDict.Wrap(response).Get<string>("meta", "password");
                var responseDigest = ResponseDigest(www.text, password);
                var sigHeader = www.responseHeaders.Get(X_SIGNATURE.ToUpper());
                if (sigHeader != responseDigest)
                {
                    throw new Exception(
                        string.Format("Singature mismatch in {0}. {1} vs {2}", www.url, sigHeader, responseDigest));
                }
            }

            if (www.responseHeaders.ContainsKey("SET-COOKIE"))
            {
                SetCookie(www.responseHeaders["SET-COOKIE"]);
            }

            responseCallback(response);
            }
        }
예제 #7
0
        public IEnumerator DoPostForm(string path, Hash formData, CancellationToken cancellationToken, bool verifyResponse, Action<Dictionary<string, object>> responseCallback)
        {
            if (IsCanceled)
            {
            return Enumerable.Empty<object>().GetEnumerator();
            }
            else
            {
            PSDebug.Log("Performing Request: {0}, {1}", path, formData != null ? formData.ToDebugString() : null);
            if (responseCallback == null)
            {
                responseCallback = DevNullCallback;
            }

            var www = CreatePostFormWWW(path, formData);
            return DoRequest(www, cancellationToken, verifyResponse, responseCallback);
            }
        }