Revert "use ReaderWriterLockSlim for lock Room.clients"

This reverts commit a4039451f30ef2c1bb90b7ca47b42ba4a3076de6.
This commit is contained in:
Spirtix 2024-04-07 16:40:34 +02:00
parent 90457b6c78
commit c60fe8a2fa
2 changed files with 11 additions and 27 deletions

View File

@ -85,8 +85,7 @@ public class GauntletRoom : Room {
}
public bool ProcessResult(Client client, string resultA, string resultB) {
base.roomLock.EnterWriteLock();
try {
lock (base.roomLock) {
players[client].resultA = resultA;
players[client].resultB = resultB;
@ -113,8 +112,6 @@ public class GauntletRoom : Room {
Send(packet);
return true;
} finally {
base.roomLock.ExitWriteLock();
}
}

View File

@ -1,5 +1,4 @@
using System;
using System.Threading;
using sodoffmmo.Data;
namespace sodoffmmo.Core;
@ -9,7 +8,7 @@ public class Room {
protected static Dictionary<string, Room> rooms = new();
List<Client> clients = new();
protected ReaderWriterLockSlim roomLock = new ReaderWriterLockSlim();
protected object roomLock = new object();
public int Id { get; private set; }
public string Name { get; private set; }
@ -40,53 +39,41 @@ public class Room {
public IEnumerable<Client> Clients {
get {
roomLock.EnterReadLock();
try {
return new List<Client>(clients);
} finally {
roomLock.ExitReadLock();
List<Client> list;
lock (roomLock) {
list = new List<Client>(clients);
}
return list;
}
}
public void AddClient(Client client) {
roomLock.EnterWriteLock();
try {
lock (roomLock) {
if (IsRemoved)
throw new Exception("Call AddClient on removed room");
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);
} finally {
roomLock.ExitWriteLock();
}
}
public void RemoveClient(Client client) {
roomLock.EnterWriteLock();
try {
lock (roomLock) {
clients.Remove(client);
if (AutoRemove && ClientsCount == 0) {
IsRemoved = true;
rooms.Remove(Name);
}
} finally {
roomLock.ExitWriteLock();
}
}
public void Send(NetworkPacket packet, Client? skip = null) {
roomLock.EnterReadLock();
try {
foreach (var roomClient in clients) {
if (roomClient != skip) {
roomClient.Send(packet);
}
}
} finally {
roomLock.ExitReadLock();
}
}
public static bool Exists(string name) => rooms.ContainsKey(name);