diff --git a/Examples/Chat/Program.cs b/Examples/Chat/Program.cs index ebbc22e..9d607f1 100644 --- a/Examples/Chat/Program.cs +++ b/Examples/Chat/Program.cs @@ -32,45 +32,36 @@ private static async Task Demo() // Fetch your account's credentials from somewhere. var auth = new EmailAuthenticationProvider("", ""); - // Create an instance of the ActionScheduler. This will - // allow us to execute chat actions like: posting messages, - // kicking users, moving messages, etc. - using (var actionScheduler = new ActionScheduler(auth, roomUrl)) - { - // Create an instance of the RoomWatcher class. Here we - // specify (via the type parameter) what WebSocket implementation - // we'd like to use. This class allows you to subscribe to chat events. - using (var roomWatcher = new RoomWatcher(auth, roomUrl)) - { - // Subscribe to the UserMentioned event. - roomWatcher.AddUserMentionedEventHandler(async m => - { - await actionScheduler.CreateReplyAsync("hello!", m.MessageId); + // Create an instance of the ActionScheduler. This will + // allow us to execute chat actions like: posting messages, + // kicking users, moving messages, etc. + using var actionScheduler = new ActionScheduler(auth, roomUrl); + // Create an instance of the RoomWatcher class. Here we + // specify (via the type parameter) what WebSocket implementation + // we'd like to use. This class allows you to subscribe to chat events. + using var roomWatcher = new RoomWatcher(auth, roomUrl); + // Subscribe to the UserMentioned event. + _ = roomWatcher.AddUserMentionedEventHandler(async m => await actionScheduler.CreateReplyAsync("hello!", m.MessageId)); - /// Do stuff ... - }); + // Besides being able to subscribe to the default events, + // you can also create (and listen to) your own. Your class must + // implement the ChatEventDataProcessor class, you can also + // optionally implement IChatEventHandler or IChatEventHandler. + var customEventHanlder = new AllData(); - // Besides being able to subscribe to the default events, - // you can also create (and listen to) your own. Your class must - // implement the ChatEventDataProcessor class, you can also - // optionally implement IChatEventHandler or IChatEventHandler. - var customEventHanlder = new AllData(); + // Add a very basic handler. + customEventHanlder.OnEvent += data => Console.WriteLine(data); - // Add a very basic handler. - customEventHanlder.OnEvent += data => Console.WriteLine(data); + // Add our custom event handler so we can + // begin processing the incoming event data. + roomWatcher.EventRouter.AddProcessor(customEventHanlder); - // Add our custom event handler so we can - // begin processing the incoming event data. - roomWatcher.EventRouter.AddProcessor(customEventHanlder); + // Post a simple message. + var messageId = await actionScheduler.CreateMessageAsync("Hello world."); - // Post a simple message. - var messageId = await actionScheduler.CreateMessageAsync("Hello world."); + while (Console.ReadKey(true).Key != ConsoleKey.Q) + { - while (Console.ReadKey(true).Key != ConsoleKey.Q) - { - - } - } - } - } + } + } } \ No newline at end of file diff --git a/SharpExchange/Api/2.2/ApiRequestScheduler.MasterScheduler.cs b/SharpExchange/Api/2.2/ApiRequestScheduler.MasterScheduler.cs index 40b9089..ac6d25f 100644 --- a/SharpExchange/Api/2.2/ApiRequestScheduler.MasterScheduler.cs +++ b/SharpExchange/Api/2.2/ApiRequestScheduler.MasterScheduler.cs @@ -36,7 +36,7 @@ static MasterSheduler() { reqs = new Queue(); - Task.Run(() => QueueProcessorLoop()); + _ = Task.Run(() => QueueProcessorLoop()); } @@ -47,7 +47,7 @@ public static void Dispose() dispose = true; reqs.Clear(); - queueMre.Set(); + _ = queueMre.Set(); queueMre.Dispose(); } @@ -60,14 +60,14 @@ public static string Schedule(string endpoint) { result = x; - mre.Set(); + _ = mre.Set(); }); reqs.Enqueue(new QueuedRequest(callback, endpoint)); - queueMre.Set(); + _ = queueMre.Set(); - mre.WaitOne(Timeout); + _ = mre.WaitOne(Timeout); return result; } @@ -82,8 +82,8 @@ private static void QueueProcessorLoop() { if (reqs.Count == 0) { - queueMre.Reset(); - queueMre.WaitOne(); + _ = queueMre.Reset(); + _ = queueMre.WaitOne(); if (dispose) { @@ -94,8 +94,8 @@ private static void QueueProcessorLoop() var req = reqs.Dequeue(); var json = HttpRequest.GetAsync(req.Url).Result; - req.Callback.InvokeAsync(json); - mre.WaitOne(waitTime); + _ = req.Callback.InvokeAsync(json); + _ = mre.WaitOne(waitTime); } } } diff --git a/SharpExchange/Api/2.2/ApiRequestScheduler.MethodSheduler.cs b/SharpExchange/Api/2.2/ApiRequestScheduler.MethodSheduler.cs index 80b724e..f0c34c1 100644 --- a/SharpExchange/Api/2.2/ApiRequestScheduler.MethodSheduler.cs +++ b/SharpExchange/Api/2.2/ApiRequestScheduler.MethodSheduler.cs @@ -37,7 +37,7 @@ public MethodSheduler() { reqs = new Queue(); - Task.Run(() => QueueProcessorLoop()); + _ = Task.Run(() => QueueProcessorLoop()); } ~MethodSheduler() @@ -53,7 +53,7 @@ public void Dispose() dispose = true; reqs.Clear(); - queueMre.Set(); + _ = queueMre.Set(); queueMre.Dispose(); GC.SuppressFinalize(this); @@ -75,14 +75,14 @@ public async Task> ScheduleAsync(string fullUrl) //TODO: Log this somewhere. } - mre.Set(); + _ = mre.Set(); }); reqs.Enqueue(new QueuedRequest(callback, fullUrl)); - queueMre.Set(); + _ = queueMre.Set(); - await Task.Run(() => mre.WaitOne(Timeout)); + _ = await Task.Run(() => mre.WaitOne(Timeout)); return result; } @@ -97,8 +97,8 @@ private void QueueProcessorLoop() { if (reqs.Count == 0) { - queueMre.Reset(); - queueMre.WaitOne(); + _ = queueMre.Reset(); + _ = queueMre.WaitOne(); if (dispose) { @@ -109,7 +109,7 @@ private void QueueProcessorLoop() var req = reqs.Dequeue(); var json = MasterSheduler.Schedule(req.Url); - req.Callback.InvokeAsync(json); + _ = req.Callback.InvokeAsync(json); var backoff = 0; @@ -124,7 +124,7 @@ private void QueueProcessorLoop() if (backoff != 0) { - mre.WaitOne(backoff * 1000); + _ = mre.WaitOne(backoff * 1000); } } } diff --git a/SharpExchange/Api/2.2/ApiRequestScheduler.cs b/SharpExchange/Api/2.2/ApiRequestScheduler.cs index a477d45..f3e4e8c 100644 --- a/SharpExchange/Api/2.2/ApiRequestScheduler.cs +++ b/SharpExchange/Api/2.2/ApiRequestScheduler.cs @@ -49,8 +49,8 @@ private static string GetEndpointId(string url) { if (string.IsNullOrEmpty(s) || (s.Any(char.IsDigit) && s != "2.2")) continue; - id.Append(s); - id.Append('/'); + _ = id.Append(s); + _ = id.Append('/'); } return id.ToString(); diff --git a/SharpExchange/Chat/Actions/ActionScheduler.cs b/SharpExchange/Chat/Actions/ActionScheduler.cs index d8fe8f3..945be61 100644 --- a/SharpExchange/Chat/Actions/ActionScheduler.cs +++ b/SharpExchange/Chat/Actions/ActionScheduler.cs @@ -11,7 +11,7 @@ namespace SharpExchange.Chat.Actions { public class ActionScheduler : IDisposable { - private IAuthenticationProvider auth; + private readonly IAuthenticationProvider auth; private readonly ManualResetEvent queueMre; private readonly Queue actionQueue; private bool dispose; @@ -36,7 +36,7 @@ public ActionScheduler(IAuthenticationProvider authProvider, string roomUrl) Host = host.GetChatHost(); RoomId = id; - Task.Run(new Action(ProcessQueue)); + _ = Task.Run(new Action(ProcessQueue)); } public ActionScheduler(IAuthenticationProvider authProvider, string host, int roomId) @@ -56,7 +56,7 @@ public ActionScheduler(IAuthenticationProvider authProvider, string host, int ro Host = host.GetChatHost(); RoomId = roomId; - Task.Run(new Action(ProcessQueue)); + _ = Task.Run(new Action(ProcessQueue)); } ~ActionScheduler() @@ -71,7 +71,7 @@ public void Dispose() if (dispose) return; dispose = true; - queueMre.Set(); + _ = queueMre.Set(); queueMre.Dispose(); actionQueue.Clear(); @@ -93,14 +93,14 @@ public async Task ScheduleActionAsync(ChatAction act, TimeSpan timeout) act.CallBack = new Action(x => { data = x; - wait.Set(); + _ = wait.Set(); }); actionQueue.Enqueue(act); - queueMre.Set(); + _ = queueMre.Set(); - await Task.Run(() => wait.WaitOne(timeout)); + _ = await Task.Run(() => wait.WaitOne(timeout)); return (T)data; } @@ -113,7 +113,7 @@ private void ProcessQueue() { if (actionQueue.Count == 0) { - queueMre.Reset(); + _ = queueMre.Reset(); } if (queueMre.WaitOne() && dispose) diff --git a/SharpExchange/Chat/Events/EventRouter.cs b/SharpExchange/Chat/Events/EventRouter.cs index 8779d9a..d9d98ed 100644 --- a/SharpExchange/Chat/Events/EventRouter.cs +++ b/SharpExchange/Chat/Events/EventRouter.cs @@ -9,7 +9,7 @@ namespace SharpExchange.Chat.Events { public sealed class EventRouter : IDisposable { - private List processors; + private readonly List processors; private bool dispose; public int RoomId { get; private set; } @@ -99,7 +99,7 @@ private void HandleNewMessage(string json) continue; } - Task.Run(() => processor.ProcessEventData(ev)); + _ = Task.Run(() => processor.ProcessEventData(ev)); } } } diff --git a/SharpExchange/Chat/Message.cs b/SharpExchange/Chat/Message.cs index 41b39c6..7a4983a 100644 --- a/SharpExchange/Chat/Message.cs +++ b/SharpExchange/Chat/Message.cs @@ -236,7 +236,7 @@ private MessageStates GetStates(IHtmlDocument dom) for (var i = 0; i < childCount; i++) { - content.RemoveChild(content.Children[0]); + _ = content.RemoveChild(content.Children[0]); } var isPuregd = content.TextContent.Trim() == "(older data no longer available)"; diff --git a/SharpExchange/Chat/Room.cs b/SharpExchange/Chat/Room.cs index 5ef68e8..6fc5d2a 100644 --- a/SharpExchange/Chat/Room.cs +++ b/SharpExchange/Chat/Room.cs @@ -295,7 +295,7 @@ private User[] GetUsers(IHtmlCollection userElements) ?.Value .Split()[0]; - int.TryParse(msgCountStr ?? "0", out var msgCount); + _ = int.TryParse(msgCountStr ?? "0", out var msgCount); users[i] = new User { diff --git a/SharpExchange/Chat/User.cs b/SharpExchange/Chat/User.cs index 57a0600..94d76f4 100644 --- a/SharpExchange/Chat/User.cs +++ b/SharpExchange/Chat/User.cs @@ -319,7 +319,7 @@ private Room[] GetRooms(IHtmlCollection roomElements) ?.Value .Split()[0]; - int.TryParse(msgCountStr ?? "0", out var msgCount); + _ = int.TryParse(msgCountStr ?? "0", out var msgCount); rooms[i] = new Room { diff --git a/SharpExchange/Extensions.cs b/SharpExchange/Extensions.cs index b74e865..65eb5f0 100644 --- a/SharpExchange/Extensions.cs +++ b/SharpExchange/Extensions.cs @@ -127,13 +127,13 @@ public static string ToQueryString(this Dictionary d) { var encoded = WebUtility.UrlEncode(kv.Value); - builder.Append(kv.Key); - builder.Append('='); - builder.Append(encoded); - builder.Append('&'); + _ = builder.Append(kv.Key); + _ = builder.Append('='); + _ = builder.Append(encoded); + _ = builder.Append('&'); } - builder.Remove(builder.Length - 1, 1); + _ = builder.Remove(builder.Length - 1, 1); return builder.ToString(); } diff --git a/SharpExchange/Net/CookieManager.cs b/SharpExchange/Net/CookieManager.cs index c9543ad..0276659 100644 --- a/SharpExchange/Net/CookieManager.cs +++ b/SharpExchange/Net/CookieManager.cs @@ -6,7 +6,7 @@ namespace SharpExchange.Net { public class CookieManager { - private Dictionary cookies; + private readonly Dictionary cookies; public Cookie this[string name] => cookies[name]; @@ -59,7 +59,7 @@ public void Add(Cookie cookie) { if (cookie.Expired) { - cookies.Remove(cookie.Name); + _ = cookies.Remove(cookie.Name); } else { diff --git a/SharpExchange/Net/HttpRequest.cs b/SharpExchange/Net/HttpRequest.cs index ff1f77e..236ca65 100644 --- a/SharpExchange/Net/HttpRequest.cs +++ b/SharpExchange/Net/HttpRequest.cs @@ -98,7 +98,7 @@ public async Task SendAsync() foreach (var c in valIdCookies) { - request.AddCookie(c.Name, c.Value); + _ = request.AddCookie(c.Name, c.Value); } } @@ -106,7 +106,7 @@ public async Task SendAsync() { foreach (var k in Data.Keys) { - request.AddParameter(k, Data[k]); + _ = request.AddParameter(k, Data[k]); } } diff --git a/SharpExchange/Net/WebSocket/DefaultWebSocket.cs b/SharpExchange/Net/WebSocket/DefaultWebSocket.cs index 698a244..8aac853 100644 --- a/SharpExchange/Net/WebSocket/DefaultWebSocket.cs +++ b/SharpExchange/Net/WebSocket/DefaultWebSocket.cs @@ -14,7 +14,7 @@ public class DefaultWebSocket : IWebSocket { private const int bufferSize = 4 * 1024; private ClientWebSocket socket; - private CancellationTokenSource socketTokenSource; + private readonly CancellationTokenSource socketTokenSource; private bool dispose; public event Action OnOpen; @@ -83,8 +83,8 @@ public async Task ConnectAsync() await socket.ConnectAsync(Endpoint, socketTokenSource.Token); - Task.Run(() => Listen()); - OnOpen.InvokeAsync(); + _ = Task.Run(() => Listen()); + _ = OnOpen.InvokeAsync(); } public async Task SendAsync(string message) @@ -147,7 +147,7 @@ private void Listen() catch (AggregateException ex) when (ex.InnerException?.GetType() == typeof(TaskCanceledException)) { - OnClose.InvokeAsync(); + _ = OnClose.InvokeAsync(); return; } @@ -159,15 +159,15 @@ private void Listen() try { - socketTokenSource.Token.WaitHandle.WaitOne(1000); + _ = socketTokenSource.Token.WaitHandle.WaitOne(1000); ConnectAsync().Wait(); } catch (Exception e2) { - OnReconnectFailed.InvokeAsync(); - OnError.InvokeAsync(e2); - OnClose.InvokeAsync(); + _ = OnReconnectFailed.InvokeAsync(); + _ = OnError.InvokeAsync(e2); + _ = OnClose.InvokeAsync(); } return; @@ -180,10 +180,10 @@ private void Listen() buffer.AddRange(b); } - Task.Run(() => HandleNewMessage(msgInfo, buffer.ToArray())); + _ = Task.Run(() => HandleNewMessage(msgInfo, buffer.ToArray())); } - OnClose.InvokeAsync(); + _ = OnClose.InvokeAsync(); } private void HandleNewMessage(WebSocketReceiveResult msgInfo, byte[] buffer)