From 83e56f745df8927630efbf05ba04164429a7a5da Mon Sep 17 00:00:00 2001 From: Robert Paciorek Date: Sat, 9 Mar 2024 19:05:03 +0000 Subject: [PATCH] fix possible race condition in JoinRoom --- src/Core/Client.cs | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/Core/Client.cs b/src/Core/Client.cs index 087025d..e7838e3 100644 --- a/src/Core/Client.cs +++ b/src/Core/Client.cs @@ -9,11 +9,12 @@ public class Client { public int ClientID { get; private set; } public PlayerData PlayerData { get; set; } = new(); - public Room Room { get; set; } + public Room Room { get; private set; } private readonly Socket socket; SocketBuffer socketBuffer = new(); private volatile bool scheduledDisconnect = false; + private object ClientLock = new(); public Client(Socket clientSocket) { socket = clientSocket; @@ -60,10 +61,12 @@ public class Client { } public void JoinRoom(Room room) { - LeaveRoom(); - InvalidatePlayerData(); - Room = room; - Room.AddClient(this); + lock(ClientLock) { + LeaveRoom(); + PlayerData.IsValid = false; + Room.AddClient(this); + Room = room; + } Send(Room.SubscribeRoom()); UpdatePlayerUserVariables(); } @@ -79,10 +82,6 @@ public class Client { } } - public void InvalidatePlayerData() { - PlayerData.IsValid = false; - } - public void Disconnect() { try { socket.Shutdown(SocketShutdown.Both);