From bfbd9adb82cb12c0f1243210ea3be0b81521e0bf Mon Sep 17 00:00:00 2001 From: Robert Paciorek Date: Fri, 1 Mar 2024 02:34:43 +0000 Subject: [PATCH] RespondJoinRoom in AddClient as atomic operation to avoid missed player in rooms due to joining race condition --- src/CommandHandlers/SetUserVariablesHandler.cs | 2 +- src/Core/Client.cs | 2 +- src/Core/Room.cs | 5 ++++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/CommandHandlers/SetUserVariablesHandler.cs b/src/CommandHandlers/SetUserVariablesHandler.cs index 343fd4e..b21c15e 100644 --- a/src/CommandHandlers/SetUserVariablesHandler.cs +++ b/src/CommandHandlers/SetUserVariablesHandler.cs @@ -47,7 +47,7 @@ class SetUserVariablesHandler : ICommandHandler { client.PlayerData.Fp = suvData.Get("FP"); Console.WriteLine($"SUV {client.Room.Name} IID: {client.ClientID}"); - client.Room.AddClient(client); + UpdatePlayersInRoom(); SendSUVToPlayerInRoom(); } diff --git a/src/Core/Client.cs b/src/Core/Client.cs index efb88fa..f4b36d7 100644 --- a/src/Core/Client.cs +++ b/src/Core/Client.cs @@ -63,7 +63,7 @@ public class Client { LeaveRoom(); InvalidatePlayerData(); Room = room; - Send(Room.RespondJoinRoom()); + Room.AddClient(this); Send(Room.SubscribeRoom()); UpdatePlayerUserVariables(); } diff --git a/src/Core/Room.cs b/src/Core/Room.cs index b864e3d..0007adb 100644 --- a/src/Core/Room.cs +++ b/src/Core/Room.cs @@ -6,7 +6,7 @@ public class Room { static Dictionary rooms = new(); List clients = new(); - public object roomLock = new object(); + protected object roomLock = new object(); public int Id { get; private set; } public string Name { get; private set; } @@ -38,6 +38,9 @@ public class Room { public void AddClient(Client client) { lock (roomLock) { + client.Send(RespondJoinRoom()); + // NOTE: send RespondJoinRoom() and add client to clients as atomic operation + // to make sure to client get full list of players in room clients.Add(client); } }