fix possible race condition in JoinRoom

This commit is contained in:
Robert Paciorek 2024-03-09 19:05:03 +00:00
parent 6a34677ab2
commit 83e56f745d

View File

@ -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) {
lock(ClientLock) {
LeaveRoom();
InvalidatePlayerData();
Room = room;
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);