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 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.
}
}

View File

@ -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<int> playerBag = (ConcurrentBag<int>) c.Metadata["shineSync"];
ConcurrentBag<int> playerBag = (ConcurrentBag<int>)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<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";
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;

View File

@ -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<byte> data) {

View File

@ -21,7 +21,7 @@ PacketType[] reboundPackets = {
// PacketType.Shine
};
string lastCapture = "";
//string lastCapture = ""; //not referenced anywhere
List<TcpClient> clients = new List<TcpClient>();
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;