From 4802b94b8891a29d00295a6e90ac1a96e7c11fb8 Mon Sep 17 00:00:00 2001 From: "Robin C. Ladiges" Date: Thu, 25 Apr 2024 01:56:30 +0200 Subject: [PATCH] fix: construct tag packet instead of caching it in memory Because the tag packet received from the client could have an UpdateType that isn't both State and Time. (Though currently the client always updates both together.) --- Server/Client.cs | 13 ++++++++++++- Server/Program.cs | 1 - Server/Server.cs | 20 ++++++++++++-------- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/Server/Client.cs b/Server/Client.cs index 4b53525..ea436ef 100644 --- a/Server/Client.cs +++ b/Server/Client.cs @@ -83,11 +83,22 @@ public class Client : IDisposable { Metadata.TryRemove("seeking", out tmp); Metadata.TryRemove("lastCostumePacket", out tmp); Metadata.TryRemove("lastCapturePacket", out tmp); - Metadata.TryRemove("lastTagPacket", out tmp); Metadata.TryRemove("lastGamePacket", out tmp); Metadata.TryRemove("lastPlayerPacket", out tmp); } + public TagPacket? GetTagPacket() { + var time = (Time?) this.Metadata?["time"]; + var seek = (bool?) this.Metadata?["seeking"]; + if (time == null && seek == null) { return null; } + return new TagPacket { + UpdateType = (seek != null ? TagPacket.TagUpdate.State : 0) | (time != null ? TagPacket.TagUpdate.Time: 0), + IsIt = seek ?? false, + Seconds = (byte) (time?.Seconds ?? 0), + Minutes = (ushort) (time?.Minutes ?? 0), + }; + } + public static bool operator ==(Client? left, Client? right) { return left is { } leftClient && right is { } rightClient && leftClient.Id == rightClient.Id; } diff --git a/Server/Program.cs b/Server/Program.cs index c9728d8..4c9df86 100644 --- a/Server/Program.cs +++ b/Server/Program.cs @@ -172,7 +172,6 @@ server.PacketHandler = (c, p) => { case TagPacket tagPacket: { // c.Logger.Info($"Got tag packet: {tagPacket.IsIt}"); - c.Metadata["lastTagPacket"] = tagPacket; if ((tagPacket.UpdateType & TagPacket.TagUpdate.State) != 0) c.Metadata["seeking"] = tagPacket.IsIt; if ((tagPacket.UpdateType & TagPacket.TagUpdate.Time) != 0) c.Metadata["time"] = new Time(tagPacket.Minutes, tagPacket.Seconds, DateTime.Now); diff --git a/Server/Server.cs b/Server/Server.cs index fea3b8a..483c2e7 100644 --- a/Server/Server.cs +++ b/Server/Server.cs @@ -349,22 +349,26 @@ public class Server { } private async Task ResendPackets(Client client) { - async Task trySend(Client other, string packetType) where T : struct, IPacket { - if (! other.Metadata.ContainsKey(packetType)) { return; } + async Task trySendPack(Client other, T? packet) where T : struct, IPacket { + if (packet == null) { return; } try { - await client.Send((T) other.Metadata[packetType]!, other); + await client.Send((T) packet, other); } catch { // lol who gives a fuck } }; + async Task trySendMeta(Client other, string packetType) where T : struct, IPacket { + if (! other.Metadata.ContainsKey(packetType)) { return; } + await trySendPack(other, (T) other.Metadata[packetType]!); + }; await Parallel.ForEachAsync(this.ClientsConnected, async (other, _) => { if (client.Id == other.Id) { return; } - await trySend(other, "lastCostumePacket"); - await trySend(other, "lastCapturePacket"); - await trySend(other, "lastTagPacket"); - await trySend(other, "lastGamePacket"); - await trySend(other, "lastPlayerPacket"); + await trySendMeta(other, "lastCostumePacket"); + await trySendMeta(other, "lastCapturePacket"); + await trySendPack(other, other.GetTagPacket()); + await trySendMeta(other, "lastGamePacket"); + await trySendMeta(other, "lastPlayerPacket"); }); }