Example #1
0
        private async void doPreload(String url, Func0 onSuccess, Func1 onError)
        {
            try
            {
                await media.getCachedObjectAsync(new Uri(base_url, url.TrimStart('/')));

                if (!runtime.IsRunning)
                {
                    return;
                }

                using (var ctx = new FlowRuntime.DeferredContext(runtime))
                {
                    onSuccess();
                }
            }
            catch (Exception e)
            {
                if (!runtime.IsRunning)
                {
                    return;
                }

                using (var ctx = new FlowRuntime.DeferredContext(runtime))
                {
                    onError(e.ToString());
                }
            }
        }
Example #2
0
        private async void execLater(int ms, Func0 cb)
        {
            if (!runtime.IsRunning)
            {
                return;
            }

            if (ms < 10)
            {
                await Task.Yield();
            }
            else
            {
                await Task.Delay(ms);
            }

            if (!runtime.IsRunning)
            {
                return;
            }

            using (var ctx = new FlowRuntime.DeferredContext(runtime))
            {
                cb();
            }
        }
Example #3
0
        private async void submitRequest(HttpRequestMessage message, Func1 onData, Func1 onError, Func1 onStatus)
        {
            try
            {
                string data    = null;
                bool   success = false;
                HttpResponseMessage response;

                try
                {
                    response = await client.SendAsync(message, HttpCompletionOption.ResponseHeadersRead);

                    try
                    {
                        if (!runtime.IsRunning)
                        {
                            return;
                        }

                        using (var ctx = new FlowRuntime.DeferredContext(runtime))
                        {
                            onStatus((int)response.StatusCode);
                        }

                        if (!response.IsSuccessStatusCode)
                        {
                            data = "HTTP status " + response.StatusCode + " " + response.ReasonPhrase;
                        }
                        else
                        {
                            var charset = response.Content.Headers.ContentType.CharSet;
                            var enc     = (charset != null) ? charset.ToLowerInvariant() : "";

                            if (enc == "utf-16")                             // binary
                            {
                                var bytes = await response.Content.ReadAsByteArrayAsync();

                                int bias  = (bytes.Length >= 2 && bytes[0] == 0xFF && bytes[1] == 0xFE) ? 1 : 0;
                                var chars = new char[bytes.Length / 2 - bias];

                                for (int i = 0, j = 2 * bias; i < chars.Length; i++, j += 2)
                                {
                                    chars[i] = (char)((int)bytes[j] | ((int)bytes[j + 1]) << 8);
                                }

                                data = new String(chars);
                            }
                            else
                            {
                                data = await response.Content.ReadAsStringAsync();
                            }

                            success = true;
                        }
                    }
                    finally
                    {
                        response.Dispose();
                    }
                }
                catch (Exception e)
                {
                    success = false;
                    data    = e.ToString();
                }

                if (!runtime.IsRunning)
                {
                    return;
                }

                using (var ctx = new FlowRuntime.DeferredContext(runtime))
                {
                    if (success)
                    {
                        onData(data);
                    }
                    else
                    {
                        onError(data);
                    }
                }
            }
            finally
            {
                message.Dispose();
            }
        }