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.)
This commit is contained in:
Robin C. Ladiges 2024-04-25 01:56:30 +02:00 committed by Sanae
parent dd0de0da78
commit 20ee74d0d6
3 changed files with 24 additions and 10 deletions

View File

@ -83,11 +83,22 @@ public class Client : IDisposable {
Metadata.TryRemove("seeking", out tmp); Metadata.TryRemove("seeking", out tmp);
Metadata.TryRemove("lastCostumePacket", out tmp); Metadata.TryRemove("lastCostumePacket", out tmp);
Metadata.TryRemove("lastCapturePacket", out tmp); Metadata.TryRemove("lastCapturePacket", out tmp);
Metadata.TryRemove("lastTagPacket", out tmp);
Metadata.TryRemove("lastGamePacket", out tmp); Metadata.TryRemove("lastGamePacket", out tmp);
Metadata.TryRemove("lastPlayerPacket", 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) { public static bool operator ==(Client? left, Client? right) {
return left is { } leftClient && right is { } rightClient && leftClient.Id == rightClient.Id; return left is { } leftClient && right is { } rightClient && leftClient.Id == rightClient.Id;
} }

View File

@ -172,7 +172,6 @@ server.PacketHandler = (c, p) => {
case TagPacket tagPacket: { case TagPacket tagPacket: {
// c.Logger.Info($"Got tag packet: {tagPacket.IsIt}"); // 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.State) != 0) c.Metadata["seeking"] = tagPacket.IsIt;
if ((tagPacket.UpdateType & TagPacket.TagUpdate.Time) != 0) if ((tagPacket.UpdateType & TagPacket.TagUpdate.Time) != 0)
c.Metadata["time"] = new Time(tagPacket.Minutes, tagPacket.Seconds, DateTime.Now); c.Metadata["time"] = new Time(tagPacket.Minutes, tagPacket.Seconds, DateTime.Now);

View File

@ -349,22 +349,26 @@ public class Server {
} }
private async Task ResendPackets(Client client) { private async Task ResendPackets(Client client) {
async Task trySend<T>(Client other, string packetType) where T : struct, IPacket { async Task trySendPack<T>(Client other, T? packet) where T : struct, IPacket {
if (! other.Metadata.ContainsKey(packetType)) { return; } if (packet == null) { return; }
try { try {
await client.Send((T) other.Metadata[packetType]!, other); await client.Send((T) packet, other);
} }
catch { catch {
// lol who gives a fuck // lol who gives a fuck
} }
}; };
async Task trySendMeta<T>(Client other, string packetType) where T : struct, IPacket {
if (! other.Metadata.ContainsKey(packetType)) { return; }
await trySendPack<T>(other, (T) other.Metadata[packetType]!);
};
await Parallel.ForEachAsync(this.ClientsConnected, async (other, _) => { await Parallel.ForEachAsync(this.ClientsConnected, async (other, _) => {
if (client.Id == other.Id) { return; } if (client.Id == other.Id) { return; }
await trySend<CostumePacket>(other, "lastCostumePacket"); await trySendMeta<CostumePacket>(other, "lastCostumePacket");
await trySend<CapturePacket>(other, "lastCapturePacket"); await trySendMeta<CapturePacket>(other, "lastCapturePacket");
await trySend<TagPacket>(other, "lastTagPacket"); await trySendPack<TagPacket>(other, other.GetTagPacket());
await trySend<GamePacket>(other, "lastGamePacket"); await trySendMeta<GamePacket>(other, "lastGamePacket");
await trySend<PlayerPacket>(other, "lastPlayerPacket"); await trySendMeta<PlayerPacket>(other, "lastPlayerPacket");
}); });
} }