-
Notifications
You must be signed in to change notification settings - Fork 8
/
EzyClients.cs
105 lines (92 loc) · 2.58 KB
/
EzyClients.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
97
98
99
100
101
102
103
104
105
using System;
using System.Collections.Generic;
using com.tvd12.ezyfoxserver.client.config;
namespace com.tvd12.ezyfoxserver.client
{
public sealed class EzyClients
{
private String defaultClientName;
private readonly IDictionary<Object, EzyClient> clients;
private static readonly EzyClients INSTANCE = new EzyClients();
private EzyClients()
{
this.clients = new Dictionary<Object, EzyClient>();
}
public static EzyClients getInstance()
{
return INSTANCE;
}
public EzyClient newClient(EzyClientConfig config)
{
lock (clients)
{
return newClient0(config);
}
}
private EzyClient newClient0(EzyClientConfig config)
{
String clientName = config.getClientName();
if (clients.ContainsKey(clientName))
return clients[clientName];
EzyClient client = new EzyTcpClient(config);
addClient0(client);
if (defaultClientName == null)
defaultClientName = client.getName();
return client;
}
public EzyClient newDefaultClient(EzyClientConfig config)
{
lock (clients)
{
EzyClient client = newClient0(config);
defaultClientName = client.getName();
return client;
}
}
public void addClient(EzyClient client)
{
lock(clients)
{
addClient0(client);
}
}
private void addClient0(EzyClient client)
{
this.clients[client.getName()] = client;
}
public EzyClient getClient(Object name)
{
lock (clients)
{
return getClient0(name);
}
}
private EzyClient getClient0(Object name)
{
if (name == null)
throw new ArgumentException("can not get client with name: null");
if (clients.ContainsKey(name))
return clients[name];
return null;
}
public EzyClient getDefaultClient()
{
lock (clients)
{
if (defaultClientName == null)
return null;
EzyClient client = getClient0(defaultClientName);
return client;
}
}
public void getClients(IList<EzyClient> cachedClients)
{
cachedClients.Clear();
lock(clients)
{
foreach (EzyClient client in clients.Values)
cachedClients.Add(client);
}
}
}
}