From 95c918b5c48989a3c64906a52ad89f908dba9bfc Mon Sep 17 00:00:00 2001 From: TheUbMunster <66451362+TheUbMunster@users.noreply.github.com> Date: Thu, 28 Jul 2022 02:05:32 -0600 Subject: [PATCH] Removed fire-and-forget warnings, null-forgave some metadata. --- Server/Client.cs | 13 ++++++++++++- Server/Program.cs | 32 ++++++++++++++++++++++---------- Server/Server.cs | 23 ++++++++++++++++++----- TestClient/Program.cs | 6 ++++-- 4 files changed, 56 insertions(+), 18 deletions(-) diff --git a/Server/Client.cs b/Server/Client.cs index bcbfb82..7ba967f 100644 --- a/Server/Client.cs +++ b/Server/Client.cs @@ -20,7 +20,7 @@ public class Client : IDisposable { public Guid Id; public Socket? Socket; - public Server Server { get; init; } + public Server Server { get; init; } = null!; //init'd in object initializer public Logger Logger { get; } public Client(Socket socket) { @@ -72,4 +72,15 @@ public class Client : IDisposable { public static bool operator !=(Client? left, Client? right) { return !(left == right); } + + public override bool Equals(object? obj) { + if (obj is Client) + return this == (Client)obj; + else + return false; + } + + public override int GetHashCode() { + return Id.GetHashCode(); //relies upon same info as == operator. + } } \ No newline at end of file diff --git a/Server/Program.cs b/Server/Program.cs index 045a05d..86c31ad 100644 --- a/Server/Program.cs +++ b/Server/Program.cs @@ -141,7 +141,10 @@ server.PacketHandler = (c, p) => { if (Settings.Instance.Scenario.MergeEnabled) { server.BroadcastReplace(gamePacket, c, (from, to, gp) => { gp.ScenarioNum = (byte?) to.Metadata["scenario"] ?? 200; - to.Send(gp, from); +#pragma warning disable CS4014 + to.Send(gp, from) + .ContinueWith(x => { if (x.Exception != null) { consoleLogger.Error(x.Exception.ToString()); } }); +#pragma warning restore CS4014 }); return false; } @@ -155,12 +158,14 @@ server.PacketHandler = (c, p) => { break; } case CostumePacket: - ClientSyncShineBag(c); +#pragma warning disable CS4014 + ClientSyncShineBag(c); //no point logging since entire def has try/catch +#pragma warning restore CS4014 c.Metadata["loadedSave"] = true; break; case ShinePacket shinePacket: { if (c.Metadata["loadedSave"] is false) break; - ConcurrentBag playerBag = (ConcurrentBag) c.Metadata["shineSync"]; + ConcurrentBag playerBag = (ConcurrentBag)c.Metadata["shineSync"]!; shineBag.Add(shinePacket.ShineId); if (playerBag.Contains(shinePacket.ShineId)) break; c.Logger.Info($"Got moon {shinePacket.ShineId}"); @@ -171,10 +176,13 @@ server.PacketHandler = (c, p) => { case PlayerPacket playerPacket when Settings.Instance.Flip.Enabled && Settings.Instance.Flip.Pov is FlipOptions.Both or FlipOptions.Others && Settings.Instance.Flip.Players.Contains(c.Id): { - playerPacket.Position += Vector3.UnitY * MarioSize((bool) c.Metadata["2d"]); + playerPacket.Position += Vector3.UnitY * MarioSize((bool) c.Metadata["2d"]!); playerPacket.Rotation *= Quaternion.CreateFromRotationMatrix(Matrix4x4.CreateRotationX(MathF.PI)) * Quaternion.CreateFromRotationMatrix(Matrix4x4.CreateRotationY(MathF.PI)); - server.Broadcast(playerPacket, c); +#pragma warning disable CS4014 + server.Broadcast(playerPacket, c) + .ContinueWith(x => { if (x.Exception != null) { consoleLogger.Error(x.Exception.ToString()); } }); +#pragma warning restore CS4014 return false; } case PlayerPacket playerPacket when Settings.Instance.Flip.Enabled @@ -182,12 +190,14 @@ server.PacketHandler = (c, p) => { && !Settings.Instance.Flip.Players.Contains(c.Id): { server.BroadcastReplace(playerPacket, c, (from, to, sp) => { if (Settings.Instance.Flip.Players.Contains(to.Id)) { - sp.Position += Vector3.UnitY * MarioSize((bool) c.Metadata["2d"]); + sp.Position += Vector3.UnitY * MarioSize((bool) c.Metadata["2d"]!); sp.Rotation *= Quaternion.CreateFromRotationMatrix(Matrix4x4.CreateRotationX(MathF.PI)) * Quaternion.CreateFromRotationMatrix(Matrix4x4.CreateRotationY(MathF.PI)); } - - to.Send(sp, from); +#pragma warning disable CS4014 + to.Send(sp, from) + .ContinueWith(x => { if (x.Exception != null) { consoleLogger.Error(x.Exception.ToString()); } }); +#pragma warning restore CS4014 }); return false; } @@ -512,7 +522,7 @@ CommandHandler.RegisterCommand("shine", args => { case "clear" when args.Length == 1: shineBag.Clear(); foreach (ConcurrentBag playerBag in server.Clients.Select(serverClient => - (ConcurrentBag) serverClient.Metadata["shineSync"])) playerBag.Clear(); + (ConcurrentBag)serverClient.Metadata["shineSync"]!)) playerBag?.Clear(); return "Cleared shine bags"; case "sync" when args.Length == 1: @@ -553,6 +563,7 @@ CommandHandler.RegisterCommandAliases(_ => { return "Shutting down"; }, "exit", "quit", "q"); +#pragma warning disable CS4014 Task.Run(() => { consoleLogger.Info("Run help command for valid commands."); while (true) { @@ -563,6 +574,7 @@ Task.Run(() => { } } } -}); +}).ContinueWith(x => { if (x.Exception != null) { consoleLogger.Error(x.Exception.ToString()); } }); +#pragma warning restore CS4014 await listenTask; \ No newline at end of file diff --git a/Server/Server.cs b/Server/Server.cs index f312ea3..43f278d 100644 --- a/Server/Server.cs +++ b/Server/Server.cs @@ -32,7 +32,10 @@ public class Server { Logger.Warn($"Accepted connection for client {socket.RemoteEndPoint}"); try { - Task.Run(() => HandleSocket(socket)); +#pragma warning disable CS4014 + Task.Run(() => HandleSocket(socket)) + .ContinueWith(x => { if (x.Exception != null) { Logger.Error(x.Exception.ToString()); } }); +#pragma warning restore CS4014 } catch (Exception e) { Logger.Error($"Error occured while setting up socket handler? {e}"); @@ -279,8 +282,10 @@ public class Server { catch (Exception e) { client.Logger.Error($"Packet handler warning: {e}"); } - - Broadcast(memory, client); +#pragma warning disable CS4014 + Broadcast(memory, client) + .ContinueWith(x => { if (x.Exception != null) { Logger.Error(x.Exception.ToString()); } }); +#pragma warning restore CS4014 } } catch (Exception e) { @@ -288,7 +293,12 @@ public class Server { client.Logger.Info($"Disconnected from the server: Connection reset"); } else { client.Logger.Error($"Disconnecting due to exception: {e}"); - if (socket.Connected) Task.Run(() => socket.DisconnectAsync(false)); + if (socket.Connected) { +#pragma warning disable CS4014 + Task.Run(() => socket.DisconnectAsync(false)) + .ContinueWith(x => { if (x.Exception != null) { Logger.Error(x.Exception.ToString()); } }); +#pragma warning restore CS4014 + } } memory?.Dispose(); @@ -304,7 +314,10 @@ public class Server { } catch { /*lol*/ } - Task.Run(() => Broadcast(new DisconnectPacket(), client)); +#pragma warning disable CS4014 + Task.Run(() => Broadcast(new DisconnectPacket(), client)) + .ContinueWith(x => { if (x.Exception != null) { Logger.Error(x.Exception.ToString()); } }); +#pragma warning restore CS4014 } private static PacketHeader GetHeader(Span data) { diff --git a/TestClient/Program.cs b/TestClient/Program.cs index b3f030a..3c476b1 100644 --- a/TestClient/Program.cs +++ b/TestClient/Program.cs @@ -21,7 +21,7 @@ PacketType[] reboundPackets = { // PacketType.Shine }; -string lastCapture = ""; +//string lastCapture = ""; //not referenced anywhere List clients = new List(); async Task S(string n, Guid otherId, Guid ownId) { @@ -77,13 +77,15 @@ async Task S(string n, Guid otherId, Guid ownId) { continue; } if (type == PacketType.Player) { +#pragma warning disable CS4014 Task.Run(async () => { await Task.Delay(1000); header.Id = ownId; MemoryMarshal.Write(owner.Memory.Span[..Constants.HeaderSize], ref header); await stream.WriteAsync(owner.Memory[..(Constants.HeaderSize + header.PacketSize)]); owner.Dispose(); - }); + }).ContinueWith(x => { if (x.Exception != null) { logger.Error(x.Exception.ToString()); } }); +#pragma warning restore CS4014 continue; } header.Id = ownId;