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")); }
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")); }
private void TimeoutOpenTasksCheck() { while (Socket.State == WebSocketState.Open) { var now = DateTime.Now; int count = 0; foreach (var ttKey in OpenTaskTimes.Keys) { if ((now - OpenTaskTimes[ttKey]).TotalMilliseconds >= TimeoutMS) { TaskCompletionSource <JToken> openTask = OpenTasks[ttKey]; OpenTasks.Remove(ttKey); OpenTaskTimes.Remove(ttKey); openTask.SetResult(JToken.FromObject(new ApiError() { Code = "0", IsError = "true", Message = "Request timeout." })); } //sleep checking each 10 requests if ((count % 10) == 0) { Thread.Sleep(1000); } count++; } } }
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)); } }