public void StartPoll() { new Thread(() => // Main Poll { runningPolls.Add(Thread.CurrentThread); while (true) { try { string rawInfo = ""; HttpWebRequest webRequest = parentSkype.mainFactory.createWebRequest_POST("https://client-s.gateway.messenger.live.com/v1/users/ME/endpoints/SELF/subscriptions/0/poll", new string[][] { new string[] { "RegistrationToken", parentSkype.authTokens.RegistrationToken } }, Encoding.ASCII.GetBytes(""), "application/json"); using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse()) { rawInfo = new StreamReader(webResponse.GetResponseStream()).ReadToEnd(); } string[] emptyResponses = { "{}", null, "" }; if (!(emptyResponses.Contains(rawInfo))) { ProcessPoll(rawInfo); } } catch (Exception thrownException) { if (!(thrownException.GetType().IsAssignableFrom(typeof(ThreadAbortException)))) { Console.WriteLine(thrownException.ToString()); if (thrownException.ToString().Contains("404")) { parentSkype.Login(true); } } } } }).Start(); new Thread(() => // Contact Requests { runningPolls.Add(Thread.CurrentThread); while (true) { try { HttpWebRequest webRequest = parentSkype.mainFactory.createWebRequest_GET("https://api.skype.com/users/self/contacts/auth-request", new string[][] { new string[] { "X-Skypetoken", parentSkype.authTokens.SkypeToken } }); string rawInfo = ""; using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse()) { rawInfo = new StreamReader(webResponse.GetResponseStream()).ReadToEnd(); } dynamic allData = JsonConvert.DeserializeObject(rawInfo); foreach (dynamic singleRequest in allData) { string senderName = (string)singleRequest.sender; if (!(processedContactRequests.Contains(senderName))) { processedContactRequests.Add(senderName); User requestSender = parentSkype.GetUser(senderName); ContactRequest newRequest = new ContactRequest(parentSkype); newRequest.Sender = requestSender; newRequest.Body = ((string)singleRequest.greeting).HtmlDecode(); parentSkype.invokeContactRequestReceived(newRequest); } } Thread.Sleep(10000); } catch (Exception thrownException) { if (!(thrownException.GetType().IsAssignableFrom(typeof(ThreadAbortException)))) { Console.WriteLine(thrownException.ToString()); } } } }).Start(); }
public void StartPoll() { // Create the token source. CancellationTokenSource mainCts = new CancellationTokenSource(); CancellationToken mainToken = mainCts.Token; tokenSources.Add(mainCts); Task.Factory.StartNew(() => // Main Poll { while (true) { string rawInfo = ""; HttpRequestMessage webRequest = parentSkype.mainFactory.createWebRequest_POST(clientGatewayMessengerDomain + "/v1/users/ME/endpoints/SELF/subscriptions/0/poll", new string[][] { new string[] { "RegistrationToken", parentSkype.authTokens.RegistrationToken } }, Encoding.ASCII.GetBytes(""), "application/json"); using (var handler = new HttpClientHandler() { CookieContainer = parentSkype.mainCookies }) using (var client = new HttpClient(handler)) { client.DefaultRequestHeaders.Add("User-Agent", parentSkype.userAgent); var result = client.SendAsync(webRequest).Result; rawInfo = result.Content.ReadAsStringAsync().Result; } string[] emptyResponses = { "{}", null, "" }; if (!(emptyResponses.Contains(rawInfo))) { ProcessPoll(rawInfo).Wait(); } if (mainToken.IsCancellationRequested) { break; } } }, mainToken); CancellationTokenSource requestsCts = new CancellationTokenSource(); CancellationToken requestsToken = requestsCts.Token; tokenSources.Add(requestsCts); Task.Factory.StartNew(() => // Contact Requests { while (true) { HttpRequestMessage webRequest = parentSkype.mainFactory.createWebRequest_GET("https://api.skype.com/users/self/contacts/auth-request", new string[][] { new string[] { "X-Skypetoken", parentSkype.authTokens.SkypeToken } }); string rawInfo = ""; using (var handler = new HttpClientHandler() { CookieContainer = parentSkype.mainCookies }) using (var client = new HttpClient(handler)) { client.DefaultRequestHeaders.Add("User-Agent", parentSkype.userAgent); var result = client.SendAsync(webRequest).Result; rawInfo = result.Content.ReadAsStringAsync().Result; } dynamic allData = JsonConvert.DeserializeObject(rawInfo); foreach (dynamic singleRequest in allData) { string senderName = (string)singleRequest.sender; if (!(processedContactRequests.Contains(senderName))) { processedContactRequests.Add(senderName); User requestSender = parentSkype.GetUser(senderName).Result; ContactRequest newRequest = new ContactRequest(parentSkype); newRequest.Sender = requestSender; newRequest.Body = ((string)singleRequest.greeting).HtmlDecode(); parentSkype.invokeContactRequestReceived(newRequest); } } Task.Delay(10000).Wait(); if (requestsToken.IsCancellationRequested) { break; } } }, requestsToken); }