protected override void HandleApiResponse(IEnumerable <object> args)
        {
            var data = args.Cast <JToken>().First();

            if (data == null && data.Children().Count() != 2)
            {
                return;
            }

            var idToken = data.SelectToken("id");

            if (idToken == null)
            {
                throw new Exception("response id not found.");
            }

            string id = idToken.ToObject <string>();

            OpenTask openTask;

            if (!OpenTasks.TryGetValue(id, out openTask))
            {
                return;
            }

            OpenTasks.Remove(id);

            openTask.PromiseTask.SetResult(data.SelectToken("result"));
        }
예제 #2
0
        private void HandleApiResponse(JToken data)
        {
            if (data == null && data.Children().Count() != 2)
            {
                return;
            }

            var idToken = data.SelectToken("id");

            if (idToken == null)
            {
                throw new Exception("response id not found.");
            }

            string id = idToken.ToObject <string>();

            TaskCompletionSource <JToken> openTask;

            if (!OpenTasks.TryGetValue(id, out openTask))
            {
                return;
            }

            OpenTasks.Remove(id);
            OpenTaskTimes.Remove(id);

            openTask.SetResult(data.SelectToken("result"));
        }
        protected void HandlePairedResponse(bool?paired)
        {
            Paired = paired.GetValueOrDefault();

            if (Paired)
            {
                var storedAppKey = StorageProvider.GetAppkey();

                string hashed = storedAppKey.StartsWith("appkey:") ?
                                UtilsHelper.ByteArrayToHexString(Sha256Manager.GetHash(Encoding.UTF8.GetBytes(storedAppKey))) :
                                storedAppKey;

                if (string.IsNullOrWhiteSpace(storedAppKey) ||
                    storedAppKey != hashed)
                {
                    StorageProvider.SetAppkey(hashed);
                }
            }

            OpenTask openTask;

            if (!OpenTasks.TryGetValue(PairOpenId, out openTask))
            {
                return;
            }

            openTask.PromiseTask.SetResult(Paired);
            openTask.TaskTimeoutMS = int.MaxValue;
        }
        protected IEnumerator TimeoutOpenTasksCheck()
        {
            while (SockIO.GetState() == WebSocketState.Open)
            {
                var           now          = DateTime.Now;
                int           count        = 0;
                List <string> toRemoveKeys = new List <string>();

                foreach (var key in OpenTasks.Keys.ToList())
                {
                    OpenTask openTask;
                    if (!OpenTasks.TryGetValue(key, out openTask))
                    {
                        continue;
                    }

                    if ((now - openTask.TaskRequestTime).TotalMilliseconds >= openTask.TaskTimeoutMS)
                    {
                        toRemoveKeys.Add(key);
                    }

                    //sleep checking each 10 requests
                    if ((count % ScatterConstants.OPEN_TASK_NR_CHECK) == 0)
                    {
                        count = 0;
                        yield return(WaitForOpenTasksCheck(ScatterConstants.OPEN_TASK_CHECK_INTERVAL_SECS));
                    }

                    count++;
                }

                foreach (var key in toRemoveKeys)
                {
                    OpenTask openTask;
                    if (!OpenTasks.TryGetValue(key, out openTask))
                    {
                        continue;
                    }

                    OpenTasks.Remove(key);

                    openTask.PromiseTask.SetResult(BuildApiError());
                }
                yield return(WaitForOpenTasksCheck(ScatterConstants.OPEN_TASK_CHECK_INTERVAL_SECS));
            }
        }