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 int ClientID { get; private set; }
public PlayerData PlayerData { get; set; } = new(); public PlayerData PlayerData { get; set; } = new();
public Room Room { get; set; } public Room Room { get; private set; }
private readonly Socket socket; private readonly Socket socket;
SocketBuffer socketBuffer = new(); SocketBuffer socketBuffer = new();
private volatile bool scheduledDisconnect = false; private volatile bool scheduledDisconnect = false;
private object ClientLock = new();
public Client(Socket clientSocket) { public Client(Socket clientSocket) {
socket = clientSocket; socket = clientSocket;
@ -60,10 +61,12 @@ public class Client {
} }
public void JoinRoom(Room room) { public void JoinRoom(Room room) {
LeaveRoom(); lock(ClientLock) {
InvalidatePlayerData(); LeaveRoom();
Room = room; PlayerData.IsValid = false;
Room.AddClient(this); Room.AddClient(this);
Room = room;
}
Send(Room.SubscribeRoom()); Send(Room.SubscribeRoom());
UpdatePlayerUserVariables(); UpdatePlayerUserVariables();
} }
@ -79,10 +82,6 @@ public class Client {
} }
} }
public void InvalidatePlayerData() {
PlayerData.IsValid = false;
}
public void Disconnect() { public void Disconnect() {
try { try {
socket.Shutdown(SocketShutdown.Both); socket.Shutdown(SocketShutdown.Both);