Skip to content

Commit a506759

Browse files
committed
This might actually work
1 parent 77d6986 commit a506759

File tree

4 files changed

+29
-34
lines changed

4 files changed

+29
-34
lines changed
Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,49 @@
1-
using OpenShock.Common.Extensions;
1+
using System.Collections.Concurrent;
22

33
namespace OpenShock.Common.Websocket;
44

55
public sealed class SimpleWebsocketCollection<T, TR> where T : class, IWebsocketController<TR>
66
{
7-
private readonly SemaphoreSlim _semaphore = new SemaphoreSlim(1);
8-
private readonly Dictionary<Guid, List<T>> _websockets = [];
7+
private readonly ConcurrentDictionary<Guid, List<T>> _websockets = new();
98

10-
public async Task RegisterConnection(T controller)
9+
public void RegisterConnection(T controller)
1110
{
12-
using (await _semaphore.LockAsyncScoped())
13-
{
14-
if (!_websockets.TryGetValue(controller.Id, out var list))
15-
{
16-
list = [controller];
17-
_websockets.Add(controller.Id, list);
18-
}
11+
var list = _websockets.GetOrAdd(controller.Id, [controller]);
1912

20-
list.Add(controller);
13+
lock (list)
14+
{
15+
if (!list.Contains(controller)) list.Add(controller);
2116
}
2217
}
2318

24-
public async Task<bool> UnregisterConnection(T controller)
19+
public bool UnregisterConnection(T controller)
2520
{
26-
using (await _semaphore.LockAsyncScoped())
21+
var key = controller.Id;
22+
if (!_websockets.TryGetValue(key, out var list)) return false;
23+
24+
lock (list)
2725
{
28-
if (!_websockets.TryGetValue(controller.Id, out var list)) return false;
2926
if (!list.Remove(controller)) return false;
3027
if (list.Count == 0)
3128
{
32-
_websockets.Remove(controller.Id);
29+
_websockets.TryRemove(key, out _);
3330
}
3431
}
3532

3633
return true;
3734
}
3835

39-
public async Task<T[]> GetConnections(Guid id)
36+
public bool IsConnected(Guid id) => _websockets.ContainsKey(id);
37+
38+
public T[] GetConnections(Guid id)
4039
{
41-
using (await _semaphore.LockAsyncScoped())
40+
if (!_websockets.TryGetValue(id, out var list)) return [];
41+
42+
lock (list)
4243
{
43-
if (!_websockets.TryGetValue(id, out var list)) return [];
4444
return list.ToArray();
4545
}
4646
}
4747

48-
public async Task<uint> GetCount()
49-
{
50-
using (await _semaphore.LockAsyncScoped())
51-
{
52-
return (uint)_websockets.Sum(x => x.Value.Count);
53-
}
54-
}
48+
public uint Count => (uint)_websockets.Sum(kvp => { lock (kvp.Value) { return kvp.Value.Count; } });
5549
}

LiveControlGateway/Controllers/LiveControlController.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,17 @@ public LiveControlController(
8585
}
8686

8787
/// <inheritdoc />
88-
protected override async Task<bool> TryRegisterConnection()
88+
protected override Task<bool> TryRegisterConnection()
8989
{
90-
await WebsocketManager.LiveControlUsers.RegisterConnection(this);
91-
return true;
90+
WebsocketManager.LiveControlUsers.RegisterConnection(this);
91+
return Task.FromResult(true);
9292
}
9393

9494
/// <inheritdoc />
95-
protected override async Task UnregisterConnection()
95+
protected override Task UnregisterConnection()
9696
{
97-
await WebsocketManager.LiveControlUsers.UnregisterConnection(this);
97+
WebsocketManager.LiveControlUsers.UnregisterConnection(this);
98+
return Task.CompletedTask;
9899
}
99100

100101
/// <summary>

LiveControlGateway/LifetimeManager/HubLifetime.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ public async Task UpdateDevice()
209209
await using var db = await _dbContextFactory.CreateDbContextAsync(_cancellationSource.Token);
210210
await UpdateShockers(db, _cancellationSource.Token);
211211

212-
foreach (var websocketController in await WebsocketManager.LiveControlUsers.GetConnections(HubController.Id))
212+
foreach (var websocketController in WebsocketManager.LiveControlUsers.GetConnections(HubController.Id))
213213
await websocketController.UpdatePermissions(db);
214214
}
215215

LiveControlGateway/LifetimeManager/HubLifetimeManager.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public async Task<bool> TryAddDeviceConnection(byte tps, IHubController hubContr
101101
return false;
102102
}
103103

104-
foreach (var websocketController in await WebsocketManager.LiveControlUsers.GetConnections(hubLifetime.HubController.Id))
104+
foreach (var websocketController in WebsocketManager.LiveControlUsers.GetConnections(hubLifetime.HubController.Id))
105105
await websocketController.UpdateConnectedState(true);
106106
}
107107

@@ -154,7 +154,7 @@ public async Task RemoveDeviceConnection(IHubController hubController, bool noti
154154

155155
if (notifyLiveControlClients)
156156
{
157-
foreach (var websocketController in await WebsocketManager.LiveControlUsers.GetConnections(hubController.Id))
157+
foreach (var websocketController in WebsocketManager.LiveControlUsers.GetConnections(hubController.Id))
158158
await websocketController.UpdateConnectedState(false);
159159
}
160160

0 commit comments

Comments
 (0)