mirror of
https://github.com/Sanae6/SmoOnlineServer.git
synced 2024-11-22 03:05:16 +00:00
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:
parent
dd0de0da78
commit
20ee74d0d6
3 changed files with 24 additions and 10 deletions
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -349,22 +349,26 @@ public class Server {
|
|||
}
|
||||
|
||||
private async Task ResendPackets(Client client) {
|
||||
async Task trySend<T>(Client other, string packetType) where T : struct, IPacket {
|
||||
if (! other.Metadata.ContainsKey(packetType)) { return; }
|
||||
async Task trySendPack<T>(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<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, _) => {
|
||||
if (client.Id == other.Id) { return; }
|
||||
await trySend<CostumePacket>(other, "lastCostumePacket");
|
||||
await trySend<CapturePacket>(other, "lastCapturePacket");
|
||||
await trySend<TagPacket>(other, "lastTagPacket");
|
||||
await trySend<GamePacket>(other, "lastGamePacket");
|
||||
await trySend<PlayerPacket>(other, "lastPlayerPacket");
|
||||
await trySendMeta<CostumePacket>(other, "lastCostumePacket");
|
||||
await trySendMeta<CapturePacket>(other, "lastCapturePacket");
|
||||
await trySendPack<TagPacket>(other, other.GetTagPacket());
|
||||
await trySendMeta<GamePacket>(other, "lastGamePacket");
|
||||
await trySendMeta<PlayerPacket>(other, "lastPlayerPacket");
|
||||
});
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue