From 360b755fb0270765b777e530388b88f4db2ab46c Mon Sep 17 00:00:00 2001 From: venizelou andreas Date: Thu, 18 Apr 2024 19:37:08 +0300 Subject: [PATCH] Cleanup --- Protest/Http/Cache.cs | 10 ++++------ Protest/Http/KeepAlive.cs | 2 +- Protest/Http/Listener.cs | 1 - Protest/Protocols/Icmp.cs | 5 +---- Protest/Tasks/Fetch.cs | 2 +- Protest/Tasks/Lifeline.cs | 2 +- Protest/Tasks/Watchdog.cs | 2 +- Protest/Tools/Monitor.cs | 5 +---- 8 files changed, 10 insertions(+), 19 deletions(-) diff --git a/Protest/Http/Cache.cs b/Protest/Http/Cache.cs index e61455a1..1f8005d3 100644 --- a/Protest/Http/Cache.cs +++ b/Protest/Http/Cache.cs @@ -367,14 +367,12 @@ public static byte[] Minify(byte[] bytes, bool softMinify) { public static byte[] GZip(byte[] bytes) { if (bytes is null) return Array.Empty(); - MemoryStream ms = new MemoryStream(); - using (GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true)) { - zip.Write(bytes, 0, bytes.Length); - } + using MemoryStream ms = new MemoryStream(); + using GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true); + zip.Write(bytes, 0, bytes.Length); byte[] array = ms.ToArray(); - ms.Dispose(); - + return array; } public static byte[] UnGZip(byte[] bytes) { diff --git a/Protest/Http/KeepAlive.cs b/Protest/Http/KeepAlive.cs index cfe4c17e..3ba35fc7 100644 --- a/Protest/Http/KeepAlive.cs +++ b/Protest/Http/KeepAlive.cs @@ -132,7 +132,7 @@ public static async Task CloseConnection(string sessionId) { pair.Value.ws.SendAsync(MSG_FORCE_RELOAD, WebSocketMessageType.Text, true, CancellationToken.None); } await pair.Value.ws.CloseAsync(WebSocketCloseStatus.NormalClosure, null, CancellationToken.None); - pair.Value.ws.Dispose(); + pair.Value.ws?.Dispose(); } connections.TryRemove(pair.Key, out _); diff --git a/Protest/Http/Listener.cs b/Protest/Http/Listener.cs index 3fafd270..a5929f2b 100644 --- a/Protest/Http/Listener.cs +++ b/Protest/Http/Listener.cs @@ -143,7 +143,6 @@ private static readonly Dictionary Logger.List(parameters) }, }; - public Listener(string ip, ushort port, string path) { if (!HttpListener.IsSupported) throw new NotSupportedException(); cache = new Cache(path); diff --git a/Protest/Protocols/Icmp.cs b/Protest/Protocols/Icmp.cs index 3af92470..af58c154 100644 --- a/Protest/Protocols/Icmp.cs +++ b/Protest/Protocols/Icmp.cs @@ -170,7 +170,7 @@ private static async Task PingArrayAsync(string[] name, string[] id, int return String.Join((char)127, result); } private static async Task PingAsync(string hostname, string id, int timeout) { - Ping p = new Ping(); + using Ping p = new Ping(); try { PingReply reply = await p.SendPingAsync(hostname, timeout, ICMP_PAYLOAD); @@ -195,9 +195,6 @@ private static async Task PingAsync(string hostname, string id, int time catch (Exception) { return id + ((char)127).ToString() + "Unknown error"; } - finally { - p.Dispose(); - } } private static async Task ArpPingArrayAsync(string[] name, string[] id) { diff --git a/Protest/Tasks/Fetch.cs b/Protest/Tasks/Fetch.cs index e013e9f3..9cfb66a7 100644 --- a/Protest/Tasks/Fetch.cs +++ b/Protest/Tasks/Fetch.cs @@ -569,7 +569,7 @@ public static byte[] DevicesTask(string[] hosts, bool dns, bool wmi, bool kerber KeepAlive.Broadcast("{\"action\":\"abort-fetch\",\"type\":\"devices\"}"u8.ToArray(), "/fetch/status"); Logger.Action(origin, "Devices fetch task aborted"); - task.Dispose(); + task?.Dispose(); task = null; return; } diff --git a/Protest/Tasks/Lifeline.cs b/Protest/Tasks/Lifeline.cs index 9a8915f0..4de593ca 100644 --- a/Protest/Tasks/Lifeline.cs +++ b/Protest/Tasks/Lifeline.cs @@ -136,7 +136,7 @@ private static void LifelineLoop() { task.Sleep(Math.Max((int)((FOUR_HOURS_IN_TICKS - (DateTime.UtcNow.Ticks - startTimeStamp)) / 10_000), 0)); if (task.cancellationToken.IsCancellationRequested) { - task.Dispose(); + task?.Dispose(); task = null; return; } diff --git a/Protest/Tasks/Watchdog.cs b/Protest/Tasks/Watchdog.cs index 35312fd9..b10e711e 100644 --- a/Protest/Tasks/Watchdog.cs +++ b/Protest/Tasks/Watchdog.cs @@ -159,7 +159,7 @@ private static void WatchLoop() { task.Sleep(Math.Max(nextSleep - (int)((DateTime.UtcNow.Ticks - loopStartTimeStamp) / 10_000), 0)); if (task.cancellationToken.IsCancellationRequested) { - task.Dispose(); + task?.Dispose(); task = null; return; } diff --git a/Protest/Tools/Monitor.cs b/Protest/Tools/Monitor.cs index 49971e61..e835a5ee 100644 --- a/Protest/Tools/Monitor.cs +++ b/Protest/Tools/Monitor.cs @@ -311,7 +311,7 @@ public static async void WebSocketHandler(HttpListenerContext ctx) { } private static long HandlePing(string host, int timeout) { - Ping p = new Ping(); + using Ping p = new Ping(); try { PingReply reply = p.Send(host, timeout); @@ -336,9 +336,6 @@ private static long HandlePing(string host, int timeout) { catch (Exception) { return -1; } - finally { - p.Dispose(); - } } [SupportedOSPlatform("windows")]