0
0
Fork 0
mirror of https://github.com/Sanae6/SmoOnlineServer.git synced 2024-11-21 18:55:17 +00:00

Removed fire-and-forget warnings, null-forgave some metadata.

This commit is contained in:
TheUbMunster 2022-07-28 02:05:32 -06:00 committed by Sanae
parent 66114bdecb
commit 95c918b5c4
4 changed files with 56 additions and 18 deletions

View file

@ -20,7 +20,7 @@ public class Client : IDisposable {
public Guid Id; public Guid Id;
public Socket? Socket; public Socket? Socket;
public Server Server { get; init; } public Server Server { get; init; } = null!; //init'd in object initializer
public Logger Logger { get; } public Logger Logger { get; }
public Client(Socket socket) { public Client(Socket socket) {
@ -72,4 +72,15 @@ public class Client : IDisposable {
public static bool operator !=(Client? left, Client? right) { public static bool operator !=(Client? left, Client? right) {
return !(left == 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.
}
} }

View file

@ -141,7 +141,10 @@ server.PacketHandler = (c, p) => {
if (Settings.Instance.Scenario.MergeEnabled) { if (Settings.Instance.Scenario.MergeEnabled) {
server.BroadcastReplace(gamePacket, c, (from, to, gp) => { server.BroadcastReplace(gamePacket, c, (from, to, gp) => {
gp.ScenarioNum = (byte?) to.Metadata["scenario"] ?? 200; 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; return false;
} }
@ -155,12 +158,14 @@ server.PacketHandler = (c, p) => {
break; break;
} }
case CostumePacket: 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; c.Metadata["loadedSave"] = true;
break; break;
case ShinePacket shinePacket: { case ShinePacket shinePacket: {
if (c.Metadata["loadedSave"] is false) break; if (c.Metadata["loadedSave"] is false) break;
ConcurrentBag<int> playerBag = (ConcurrentBag<int>) c.Metadata["shineSync"]; ConcurrentBag<int> playerBag = (ConcurrentBag<int>)c.Metadata["shineSync"]!;
shineBag.Add(shinePacket.ShineId); shineBag.Add(shinePacket.ShineId);
if (playerBag.Contains(shinePacket.ShineId)) break; if (playerBag.Contains(shinePacket.ShineId)) break;
c.Logger.Info($"Got moon {shinePacket.ShineId}"); c.Logger.Info($"Got moon {shinePacket.ShineId}");
@ -171,10 +176,13 @@ server.PacketHandler = (c, p) => {
case PlayerPacket playerPacket when Settings.Instance.Flip.Enabled case PlayerPacket playerPacket when Settings.Instance.Flip.Enabled
&& Settings.Instance.Flip.Pov is FlipOptions.Both or FlipOptions.Others && Settings.Instance.Flip.Pov is FlipOptions.Both or FlipOptions.Others
&& Settings.Instance.Flip.Players.Contains(c.Id): { && 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)) playerPacket.Rotation *= Quaternion.CreateFromRotationMatrix(Matrix4x4.CreateRotationX(MathF.PI))
* Quaternion.CreateFromRotationMatrix(Matrix4x4.CreateRotationY(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; return false;
} }
case PlayerPacket playerPacket when Settings.Instance.Flip.Enabled case PlayerPacket playerPacket when Settings.Instance.Flip.Enabled
@ -182,12 +190,14 @@ server.PacketHandler = (c, p) => {
&& !Settings.Instance.Flip.Players.Contains(c.Id): { && !Settings.Instance.Flip.Players.Contains(c.Id): {
server.BroadcastReplace(playerPacket, c, (from, to, sp) => { server.BroadcastReplace(playerPacket, c, (from, to, sp) => {
if (Settings.Instance.Flip.Players.Contains(to.Id)) { 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)) sp.Rotation *= Quaternion.CreateFromRotationMatrix(Matrix4x4.CreateRotationX(MathF.PI))
* Quaternion.CreateFromRotationMatrix(Matrix4x4.CreateRotationY(MathF.PI)); * Quaternion.CreateFromRotationMatrix(Matrix4x4.CreateRotationY(MathF.PI));
} }
#pragma warning disable CS4014
to.Send(sp, from); to.Send(sp, from)
.ContinueWith(x => { if (x.Exception != null) { consoleLogger.Error(x.Exception.ToString()); } });
#pragma warning restore CS4014
}); });
return false; return false;
} }
@ -512,7 +522,7 @@ CommandHandler.RegisterCommand("shine", args => {
case "clear" when args.Length == 1: case "clear" when args.Length == 1:
shineBag.Clear(); shineBag.Clear();
foreach (ConcurrentBag<int> playerBag in server.Clients.Select(serverClient => foreach (ConcurrentBag<int> playerBag in server.Clients.Select(serverClient =>
(ConcurrentBag<int>) serverClient.Metadata["shineSync"])) playerBag.Clear(); (ConcurrentBag<int>)serverClient.Metadata["shineSync"]!)) playerBag?.Clear();
return "Cleared shine bags"; return "Cleared shine bags";
case "sync" when args.Length == 1: case "sync" when args.Length == 1:
@ -553,6 +563,7 @@ CommandHandler.RegisterCommandAliases(_ => {
return "Shutting down"; return "Shutting down";
}, "exit", "quit", "q"); }, "exit", "quit", "q");
#pragma warning disable CS4014
Task.Run(() => { Task.Run(() => {
consoleLogger.Info("Run help command for valid commands."); consoleLogger.Info("Run help command for valid commands.");
while (true) { 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; await listenTask;

View file

@ -32,7 +32,10 @@ public class Server {
Logger.Warn($"Accepted connection for client {socket.RemoteEndPoint}"); Logger.Warn($"Accepted connection for client {socket.RemoteEndPoint}");
try { 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) { catch (Exception e) {
Logger.Error($"Error occured while setting up socket handler? {e}"); Logger.Error($"Error occured while setting up socket handler? {e}");
@ -279,8 +282,10 @@ public class Server {
catch (Exception e) { catch (Exception e) {
client.Logger.Error($"Packet handler warning: {e}"); client.Logger.Error($"Packet handler warning: {e}");
} }
#pragma warning disable CS4014
Broadcast(memory, client); Broadcast(memory, client)
.ContinueWith(x => { if (x.Exception != null) { Logger.Error(x.Exception.ToString()); } });
#pragma warning restore CS4014
} }
} }
catch (Exception e) { catch (Exception e) {
@ -288,7 +293,12 @@ public class Server {
client.Logger.Info($"Disconnected from the server: Connection reset"); client.Logger.Info($"Disconnected from the server: Connection reset");
} else { } else {
client.Logger.Error($"Disconnecting due to exception: {e}"); 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(); memory?.Dispose();
@ -304,7 +314,10 @@ public class Server {
} }
catch { /*lol*/ } 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<byte> data) { private static PacketHeader GetHeader(Span<byte> data) {

View file

@ -21,7 +21,7 @@ PacketType[] reboundPackets = {
// PacketType.Shine // PacketType.Shine
}; };
string lastCapture = ""; //string lastCapture = ""; //not referenced anywhere
List<TcpClient> clients = new List<TcpClient>(); List<TcpClient> clients = new List<TcpClient>();
async Task S(string n, Guid otherId, Guid ownId) { async Task S(string n, Guid otherId, Guid ownId) {
@ -77,13 +77,15 @@ async Task S(string n, Guid otherId, Guid ownId) {
continue; continue;
} }
if (type == PacketType.Player) { if (type == PacketType.Player) {
#pragma warning disable CS4014
Task.Run(async () => { Task.Run(async () => {
await Task.Delay(1000); await Task.Delay(1000);
header.Id = ownId; header.Id = ownId;
MemoryMarshal.Write(owner.Memory.Span[..Constants.HeaderSize], ref header); MemoryMarshal.Write(owner.Memory.Span[..Constants.HeaderSize], ref header);
await stream.WriteAsync(owner.Memory[..(Constants.HeaderSize + header.PacketSize)]); await stream.WriteAsync(owner.Memory[..(Constants.HeaderSize + header.PacketSize)]);
owner.Dispose(); owner.Dispose();
}); }).ContinueWith(x => { if (x.Exception != null) { logger.Error(x.Exception.ToString()); } });
#pragma warning restore CS4014
continue; continue;
} }
header.Id = ownId; header.Id = ownId;