-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathClientRepository.cs
96 lines (84 loc) · 2.68 KB
/
ClientRepository.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using System;
using System.Collections.Generic;
namespace AltV.Net.EntitySync
{
public class ClientRepository : IClientRepository
{
private readonly IDictionary<string, IClient> clients = new Dictionary<string, IClient>();
private readonly ClientThreadRepository[] clientThreadRepositories;
public ClientRepository(ClientThreadRepository[] clientThreadRepositories)
{
this.clientThreadRepositories = clientThreadRepositories;
}
public void Add(IClient client)
{
if (client == null)
{
throw new ArgumentException("Client must not be null.");
}
lock (clients)
{
foreach (var clientThreadRepository in clientThreadRepositories)
{
clientThreadRepository.Add(client);
}
clients[client.Token] = client;
}
}
public void Replace(IClient client)
{
if (client == null)
{
throw new ArgumentException("Client must not be null.");
}
lock (clients)
{
if (clients.Remove(client.Token, out var oldClient))
{
foreach (var clientThreadRepository in clientThreadRepositories)
{
clientThreadRepository.Replace(client, oldClient);
}
}
else
{
foreach (var clientThreadRepository in clientThreadRepositories)
{
clientThreadRepository.Replace(client);
}
}
clients[client.Token] = client;
}
}
public IClient Remove(IClient client)
{
if (client == null)
{
throw new ArgumentException("Client must not be null.");
}
return Remove(client.Token);
}
public IClient Remove(string token)
{
lock (clients)
{
if (clients.Remove(token, out var client))
{
foreach (var clientThreadRepository in clientThreadRepositories)
{
clientThreadRepository.Remove(client);
}
return client;
}
}
return null;
}
public bool TryGet(string token, out IClient client)
{
lock (clients)
{
return clients.TryGetValue(token, out client);
}
}
}
}