Fix a connect packet read among us

This commit is contained in:
Sanae 2022-02-14 14:40:42 -06:00
parent d60aa07e1f
commit 9b2d508a20
2 changed files with 22 additions and 2 deletions

View File

@ -1,9 +1,11 @@
using System.Collections.Concurrent; using System.Collections.Concurrent;
using Server; using Server;
using Shared.Packet.Packets; using Shared.Packet.Packets;
using Timer = System.Timers.Timer;
Server.Server server = new Server.Server(); Server.Server server = new Server.Server();
ConcurrentBag<int> shineBag = new ConcurrentBag<int>(); HashSet<int> shineBag = new HashSet<int>();
int shineTx = 0; // used for logging
server.ClientJoined += async (c, type) => { server.ClientJoined += async (c, type) => {
c.Metadata["shineSync"] = new ConcurrentBag<int>(); c.Metadata["shineSync"] = new ConcurrentBag<int>();
@ -23,6 +25,14 @@ async void SyncShineBag() {
}); });
} }
Timer timer = new Timer(120000);
timer.AutoReset = true;
timer.Enabled = true;
timer.Elapsed += (_, _) => {
SyncShineBag();
};
timer.Start();
server.PacketHandler += async (c, p) => { server.PacketHandler += async (c, p) => {
switch (p) { switch (p) {
case CostumePacket: case CostumePacket:
@ -33,6 +43,8 @@ server.PacketHandler += async (c, p) => {
if (c.Metadata["loadedSave"] is false) return; if (c.Metadata["loadedSave"] is false) return;
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)) return;
Console.WriteLine($"{c.Name} got {shinePacket.ShineId}");
playerBag.Add(shinePacket.ShineId); playerBag.Add(shinePacket.ShineId);
SyncShineBag(); SyncShineBag();
break; break;

View File

@ -107,7 +107,8 @@ public class Server {
first = false; first = false;
if (header.Type != PacketType.Connect) throw new Exception($"First packet was not init, instead it was {header.Type}"); if (header.Type != PacketType.Connect) throw new Exception($"First packet was not init, instead it was {header.Type}");
ConnectPacket connect = MemoryMarshal.Read<ConnectPacket>(memory.Memory.Span[Constants.HeaderSize..size]); ConnectPacket connect = new ConnectPacket();
connect.Deserialize(memory.Memory.Span[Constants.HeaderSize..size]);
lock (Clients) { lock (Clients) {
bool firstConn = false; bool firstConn = false;
switch (connect.ConnectionType) { switch (connect.ConnectionType) {
@ -183,6 +184,13 @@ public class Server {
client.CurrentCostume = costumePacket; client.CurrentCostume = costumePacket;
} }
{
if (header.Type is not PacketType.Cap and not PacketType.Player) Logger.Warn($"lol {header.Type}");
IPacket packet = (IPacket) Activator.CreateInstance(Constants.PacketIdMap[header.Type])!;
packet.Deserialize(memory.Memory.Span[Constants.HeaderSize..]);
PacketHandler?.Invoke(client, packet);
}
await Broadcast(memory, client); await Broadcast(memory, client);
} }
} catch (Exception e) { } catch (Exception e) {