public void CancelTaskTest() { int val = 0; Task tsk = new Task(() => { Interlocked.Increment(ref val); }); Assert.IsFalse(tsk.IsCanceled, "Should not be cancelled"); TaskHelper.CancelTask(tsk, false); Assert.IsTrue(tsk.IsCanceled, "Should be cancelled"); }
internal static void Stop() { TaskHelper.CancelTask("YouTubeOAuthRefresh"); TaskHelper.CancelTask("YouTubeChannelRefresh"); TaskHelper.CancelTask("YouTubeLiveChatRefresh"); }
internal static void Refresh() { try { Plugin.Log($"Requesting live broadcast info..."); HttpWebRequest web = (HttpWebRequest)WebRequest.Create("https://www.googleapis.com/youtube/v3/liveBroadcasts?part=id%2Csnippet%2Cstatus&broadcastStatus=all&broadcastType=all&maxResults=50"); web.Method = "GET"; web.Headers.Add("Authorization", $"{YouTubeOAuthToken.tokenType} {YouTubeOAuthToken.accessToken}"); web.Accept = "application/json"; web.UserAgent = "StreamCoreClient"; using (HttpWebResponse resp = (HttpWebResponse)web.GetResponse()) { if (resp.StatusCode == HttpStatusCode.OK) { using (Stream dataStream = resp.GetResponseStream()) { using (StreamReader reader = new StreamReader(dataStream)) { string ret = reader.ReadToEnd(); if (Update(ret)) { channelName = GetChannelName(currentBroadcast.snippet.channelId); Plugin.Log("YouTube channel name: " + channelName); YouTubeMessageHandlers.InvokeHandler(new YouTubeMessage("youtube#onConnectedToLiveChat"), ""); TaskHelper.CancelTask("YouTubeChannelRefresh"); Plugin.Log($"There are currently {broadcasts.Count} broadcasts being tracked.");// Ret: {ret}"); } //Plugin.Log($"Broadcast \"{broadcast.Value.snippet.title}\" (ID: {broadcast.Value.id}, ChannelID: {broadcast.Value.snippet.channelId}) with description \"{broadcast.Value.snippet.description}\" status is \"{broadcast.Value.status.recordingStatus}\""); } } } else { Plugin.Log($"Error: {resp.StatusCode}"); } } } catch (WebException ex) { // Read the response and log it using (Stream dataStream = ex.Response.GetResponseStream()) { using (StreamReader reader = new StreamReader(dataStream)) { var response = reader.ReadToEnd(); Plugin.Log($"Status: {ex.Status}, Response: {response}"); switch (((HttpWebResponse)ex.Response).StatusCode) { // If we hit an unauthorized exception, the users auth token has expired case HttpStatusCode.Unauthorized: // Try to refresh the users auth token, forcing it through even if our local timestamp says it's not expired if (!YouTubeOAuthToken.Refresh(true)) { YouTubeOAuthToken.Invalidate(); YouTubeOAuthToken.Generate(); } break; case HttpStatusCode.Forbidden: var json = JSON.Parse(response); List <string> errorList = new List <string>(); foreach (var error in json["error"]["errors"].AsArray) { errorList.Add(error.Value["reason"].Value); } YouTubeConnection.lastError = "Unable to retrieve live broadcast data! Ensure you have configured the YouTube data API correctly, then try again. Error(s): " + string.Join(", ", errorList); YouTubeMessageHandlers.InvokeHandler(new YouTubeMessage("youtube#onError"), ""); currentBroadcast = null; YouTubeConnection.Stop(); break; } } } } catch (Exception ex) { Plugin.Log(ex.ToString()); } }