private void StartWait(MegaRequest cause, string handle, string waitUrl) { if (waitRetries > 1) { Thread.Sleep((int)(Math.Pow(2, waitRetries) * 100)); // 0,400,800,1600,3200ms etc } ScWc = new WebClient(); ScWc.DownloadStringCompleted += (s, e) => { if (e.Cancelled) { return; } // todo kinds of errors if (e.Error != null) { waitRetries++; StartWait(cause, handle, waitUrl); return; } waitRetries = 0; StartPoll(cause, handle); }; try { ScWc.DownloadStringAsync(new Uri(waitUrl)); } catch (WebException) { waitRetries++; StartWait(cause, handle, waitUrl); } }
public void StartPoll(MegaRequest cause, string handle) { ScWc = new WebClient(); ScWc.DownloadStringCompleted += (s, e) => { if (e.Cancelled) { return; } if (e.Error != null) { StartPoll(cause, handle); return; } try { var response = String.Format("{{root:{0}}}", e.Result); var r = JObject.Parse(response)["root"]; if (r["w"] != null) { StartWait(cause, handle, r["w"].ToString()); return; } else if (r["a"] != null) { if (ServerCommand != null) { Utility.Util.StartThread(() => { var str = r["a"].ToString(); var cmds = JsonConvert.DeserializeObject<List<ServerCommand>>(str, new ServerCommandConverter(user)); foreach (var cmd in cmds) { if (cmd.CommandId != null) { var track = tracking.Where(t => t == cmd.CommandId).FirstOrDefault(); if (track != null) { tracking.Remove(track); cmd.IsMine = true; } } } ServerCommand(this, new ServerRequestArgs { commands = cmds }); }, "server_request_handling"); } StartPoll(cause, r["sn"].ToString()); } } catch { StartPoll(cause, handle); } }; try { ScWc.DownloadStringAsync(BuildScUri(cause, handle)); } catch (System.Net.WebException) { StartPoll(cause, handle); } }
private void ProcessAgain(MegaRequest request, bool incrRetries = true) { if (incrRetries) { request.retries++; } ProcessRequest(request); }
private Uri BuildScUri(MegaRequest req, string handle) { var url = ServerClientUrl; url += "?sn=" + handle; url += req.Sid != null ? "&sid=" + req.Sid : ""; return(new Uri(url)); }
private Uri BuildCsUri(MegaRequest req) { var url = ClientServerUrl; url += "?id=" + req.Id.ToString(); url += req.Sid != null ? "&sid=" + req.Sid : ""; url += req.NodeSid != null ? "&n=" + req.NodeSid : ""; return(new Uri(url)); }
private string GetData(MegaRequest req) { var t = JsonConvert.SerializeObject(req, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, Converters = req.Converters }); return(string.Format("[{0}]", t)); }
private void ProcessError(MegaRequest req, int errno) { switch (errno) { case MegaApiError.EAGAIN: if (req.retries >= 30) { req.HandleError(MegaApiError.EAPI); ProcessNext(); break; } Debug.WriteLine("Received -3, retrying"); ProcessAgain(req); break; case MegaApiError.EARGS: req.HandleError(MegaApiError.EAPI); ProcessNext(); break; case MegaApiError.ESID: if (req.retries >= 3) { req.HandleError(MegaApiError.EBROKEN); ProcessNext(); } else { if (Auth == null) { req.HandleError(MegaApiError.EBROKEN); } else { var sidReq = new MRequestGetSid <MResponseGetSid>(Auth); sidReq.Success += (s, a) => { req.Sid = a.SessionId; ProcessAgain(req); }; sidReq.Error += (s, a) => req.HandleError(MegaApiError.EBROKEN); ProcessRequest(sidReq); } } break; default: req.HandleError(errno); ProcessNext(); break; } }
public void EnqueueRequest(MegaRequest request) { Util.StartThread(() => { lock (requests) { requests.Enqueue(request); if (requests.Count == 1) { ProcessNext(); } } }, "mega_api_request_start"); }
private void ProcessNext() { MegaRequest req = null; lock (requests) { if (requests.Count < 1) { return; } req = requests.Dequeue(); } ProcessRequest(req); }
private void ProcessError(MegaRequest req, int errno) { switch (errno) { case MegaApiError.EAGAIN: if (req.retries >= 30) { req.HandleError(MegaApiError.EAPI); ProcessNext(); break; } Debug.WriteLine("Received -3, retrying"); ProcessAgain(req); break; case MegaApiError.EARGS: req.HandleError(MegaApiError.EAPI); ProcessNext(); break; case MegaApiError.ESID: if (req.retries >= 3) { req.HandleError(MegaApiError.EBROKEN); ProcessNext(); } else { if (Auth == null) { req.HandleError(MegaApiError.EBROKEN); } else { var sidReq = new MRequestGetSid<MResponseGetSid>(Auth); sidReq.Success += (s, a) => { req.Sid = a.SessionId; ProcessAgain(req); }; sidReq.Error += (s, a) => req.HandleError(MegaApiError.EBROKEN); ProcessRequest(sidReq); } } break; default: req.HandleError(errno); ProcessNext(); break; } }
internal void StartPoll(MegaRequest cause, string handle) { Util.StartThread(() => { lock (pollingLock) { if (polling != null) { polling.Cancel(); } polling = new PollingTransport(Auth); polling.Proxy = Proxy; polling.tracking = tracking; polling.ServerCommand += (s, e) => { if (ServerRequest != null) { ServerRequest(s, e); } }; polling.StartPoll(cause, handle); } }, "polling_transport_start"); }
private Uri BuildScUri(MegaRequest req, string handle) { var url = ServerClientUrl; url += "?sn=" + handle; url += req.Sid != null ? "&sid=" + req.Sid : ""; return new Uri(url); }
public void StartPoll(MegaRequest cause, string handle) { ScWc = new WebClient(); ScWc.DownloadStringCompleted += (s, e) => { if (e.Cancelled) { return; } if (e.Error != null) { StartPoll(cause, handle); return; } try { var response = String.Format("{{root:{0}}}", e.Result); var r = JObject.Parse(response)["root"]; if (r["w"] != null) { StartWait(cause, handle, r["w"].ToString()); return; } else if (r["a"] != null) { if (ServerCommand != null) { Utility.Util.StartThread(() => { var str = r["a"].ToString(); var cmds = JsonConvert.DeserializeObject <List <ServerCommand> >(str, new ServerCommandConverter(user)); foreach (var cmd in cmds) { if (cmd.CommandId != null) { var track = tracking.Where(t => t == cmd.CommandId).FirstOrDefault(); if (track != null) { tracking.Remove(track); cmd.IsMine = true; } } } ServerCommand(this, new ServerRequestArgs { commands = cmds }); }, "server_request_handling"); } StartPoll(cause, r["sn"].ToString()); } } catch { StartPoll(cause, handle); } }; try { ScWc.DownloadStringAsync(BuildScUri(cause, handle)); } catch (System.Net.WebException) { StartPoll(cause, handle); } }
private void ProcessRequest(MegaRequest req) { if (req.retries > 1) { Thread.Sleep((int)(Math.Pow(2, req.retries) * 100)); // 0,400,800,1600,3200ms etc } var wc = new WebClient(); wc.Proxy = Proxy; wc.UploadStringCompleted += (s, e) => { if (e.Error == null) { try { var response = String.Format("{{root:{0}}}", e.Result); var r = JObject.Parse(response)["root"]; #region error handling if (r.Type == JTokenType.Integer && r.ToObject<int>() < 0) { ProcessError(req, r.ToObject<int>()); return; } if (r.Type == JTokenType.Array) { if (r[0].Type == JTokenType.Integer && r[0].ToObject<int>() < 0) { ProcessError(req, r[0].ToObject<int>()); return; } } else { req.HandleError(MegaApiError.EUNEXPECTED); ProcessNext(); return; } #endregion req.HandleSuccess(r[0]); ProcessNext(); } catch (JsonException) { req.HandleError(MegaApiError.EUNEXPECTED); ProcessNext(); } } else { ProcessAgain(req); } }; try { if (req.IsTracking) { tracking.Add(((ITrackingRequest)req).TrackingId); } wc.UploadStringAsync(BuildCsUri(req), GetData(req)); } catch (WebException) { ProcessAgain(req, false); } }
private Uri BuildCsUri(MegaRequest req) { var url = ClientServerUrl; url += "?id=" + req.Id.ToString(); url += req.Sid != null ? "&sid=" + req.Sid : ""; url += req.NodeSid != null ? "&n=" + req.NodeSid : ""; return new Uri(url); }
private string GetData(MegaRequest req) { var t = JsonConvert.SerializeObject(req, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, Converters = req.Converters }); return string.Format("[{0}]", t); }
private void ProcessRequest(MegaRequest req) { if (req.retries > 1) { Thread.Sleep((int)(Math.Pow(2, req.retries) * 100)); // 0,400,800,1600,3200ms etc } var wc = new WebClient(); wc.Proxy = Proxy; wc.UploadStringCompleted += (s, e) => { if (e.Error == null) { try { var response = String.Format("{{root:{0}}}", e.Result); var r = JObject.Parse(response)["root"]; #region error handling if (r.Type == JTokenType.Integer && r.ToObject <int>() < 0) { ProcessError(req, r.ToObject <int>()); return; } if (r.Type == JTokenType.Array) { if (r[0].Type == JTokenType.Integer && r[0].ToObject <int>() < 0) { ProcessError(req, r[0].ToObject <int>()); return; } } else { req.HandleError(MegaApiError.EUNEXPECTED); ProcessNext(); return; } #endregion req.HandleSuccess(r[0]); ProcessNext(); } catch (JsonException) { req.HandleError(MegaApiError.EUNEXPECTED); ProcessNext(); } } else { ProcessAgain(req); } }; try { if (req.IsTracking) { tracking.Add(((ITrackingRequest)req).TrackingId); } wc.UploadStringAsync(BuildCsUri(req), GetData(req)); } catch (WebException) { ProcessAgain(req, false); } }