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

View File

@ -1,5 +1,4 @@
using System; using System;
using System.Threading;
using sodoffmmo.Data; using sodoffmmo.Data;
namespace sodoffmmo.Core; namespace sodoffmmo.Core;
@ -9,7 +8,7 @@ public class Room {
protected static Dictionary<string, Room> rooms = new(); protected static Dictionary<string, Room> rooms = new();
List<Client> clients = new(); List<Client> clients = new();
protected ReaderWriterLockSlim roomLock = new ReaderWriterLockSlim(); protected object roomLock = new object();
public int Id { get; private set; } public int Id { get; private set; }
public string Name { get; private set; } public string Name { get; private set; }
@ -40,52 +39,40 @@ public class Room {
public IEnumerable<Client> Clients { public IEnumerable<Client> Clients {
get { get {
roomLock.EnterReadLock(); List<Client> list;
try { lock (roomLock) {
return new List<Client>(clients); list = new List<Client>(clients);
} finally {
roomLock.ExitReadLock();
} }
return list;
} }
} }
public void AddClient(Client client) { public void AddClient(Client client) {
roomLock.EnterWriteLock(); lock (roomLock) {
try {
if (IsRemoved) if (IsRemoved)
throw new Exception("Call AddClient on removed room"); throw new Exception("Call AddClient on removed room");
client.Send(RespondJoinRoom()); client.Send(RespondJoinRoom());
// NOTE: send RespondJoinRoom() and add client to clients as atomic operation // NOTE: send RespondJoinRoom() and add client to clients as atomic operation
// to make sure to client get full list of players in room // to make sure to client get full list of players in room
clients.Add(client); clients.Add(client);
} finally {
roomLock.ExitWriteLock();
} }
} }
public void RemoveClient(Client client) { public void RemoveClient(Client client) {
roomLock.EnterWriteLock(); lock (roomLock) {
try {
clients.Remove(client); clients.Remove(client);
if (AutoRemove && ClientsCount == 0) { if (AutoRemove && ClientsCount == 0) {
IsRemoved = true; IsRemoved = true;
rooms.Remove(Name); rooms.Remove(Name);
} }
} finally {
roomLock.ExitWriteLock();
} }
} }
public void Send(NetworkPacket packet, Client? skip = null) { public void Send(NetworkPacket packet, Client? skip = null) {
roomLock.EnterReadLock(); foreach (var roomClient in clients) {
try { if (roomClient != skip) {
foreach (var roomClient in clients) { roomClient.Send(packet);
if (roomClient != skip) {
roomClient.Send(packet);
}
} }
} finally {
roomLock.ExitReadLock();
} }
} }