forked from BeardedManStudios/ForgeNetworkingRemastered
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMasterServer.cs
243 lines (213 loc) · 6.46 KB
/
MasterServer.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
using BeardedManStudios;
using BeardedManStudios.Forge.Networking;
using BeardedManStudios.Forge.Networking.Frame;
using BeardedManStudios.Threading;
using BeardedManStudios.SimpleJSON;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading;
using System;
namespace MasterServer
{
public class MasterServer
{
private const int PING_INTERVAL = 10000;
public bool IsRunning { get; private set; }
private TCPServer server;
private List<Host> hosts = new List<Host>();
private Dictionary<string, int> _playerRequests = new Dictionary<string, int>();
private bool _eloRangeSet;
private int _eloRange;
public int EloRange
{
get { return _eloRange; }
set
{
if (value == 0)
_eloRangeSet = false;
else
_eloRangeSet = true;
_eloRange = value;
}
}
private bool _logging;
public bool ToggleLogging()
{
_logging = !_logging;
return _logging;
}
private void Log(object message)
{
if (!_logging)
return;
Console.WriteLine(message);
}
public MasterServer(string host, ushort port)
{
server = new TCPServer(2048);
server.Connect(host, port);
server.textMessageReceived += MessageReceived;
IsRunning = true;
server.disconnected += (sender) =>
{
IsRunning = false;
};
server.playerDisconnected += (player, sender) =>
{
for (int i = 0; i < hosts.Count; i++)
{
if (hosts[i].Player == player)
{
Log($"Host [{hosts[i].Address}] on port [{hosts[i].Port}] with name [{hosts[i].Name}] removed");
hosts.RemoveAt(i);
return;
}
}
};
Task.Queue(() =>
{
while (server.IsBound)
{
server.SendAll(server.GeneratePing());
Thread.Sleep(PING_INTERVAL);
}
}, PING_INTERVAL);
}
private void MessageReceived(NetworkingPlayer player, Text frame, NetWorker sender)
{
try
{
JSONNode data = JSONNode.Parse(frame.ToString());
if (data["register"] != null)
Register(player, data["register"]);
else if (data["update"] != null)
Update(player, data["update"]);
else if (data["get"] != null)
Get(player, data["get"]);
}
catch
{
// Ignore the message and disocnnect the requester
server.Disconnect(player, true);
}
}
private void Register(NetworkingPlayer player, JSONNode data)
{
string name = data["name"];
string address = ((IPEndPoint)player.TcpClientHandle.Client.RemoteEndPoint).Address.ToString();
ushort port = data["port"].AsUShort;
int maxPlayers = data["maxPlayers"].AsInt;
int playerCount = data["playerCount"].AsInt;
string comment = data["comment"];
string gameId = data["id"];
string gameType = data["type"];
string mode = data["mode"];
string protocol = data["protocol"];
int elo = data["elo"].AsInt;
bool useElo = data["useElo"].AsBool;
Host host = new Host()
{
Name = name,
Address = address,
Port = port,
MaxPlayers = maxPlayers,
PlayerCount = playerCount,
Comment = comment,
Id = gameId,
Type = gameType,
Mode = mode,
Protocol = protocol,
Player = player,
Elo = elo,
UseElo = useElo
};
hosts.Add(host);
Log(string.Format($"Host [{address}] registered on port [{port}] with name [{name}]"));
}
private void Update(NetworkingPlayer player, JSONNode data)
{
int playerCount = data["playerCount"].AsInt;
string comment = data["comment"];
string gameType = data["type"];
string mode = data["mode"];
ushort port = data["port"].AsUShort;
string address = ((IPEndPoint)player.TcpClientHandle.Client.RemoteEndPoint).Address.ToString();
for (int i = 0; i < hosts.Count; i++)
{
if (hosts[i].Address == address && hosts[i].Port == port)
{
Host host = hosts[i];
if(comment != null)
host.Comment = comment;
if(gameType != null)
host.Type = gameType;
if (mode != null)
host.Mode = mode;
host.PlayerCount = playerCount;
hosts[i] = host;
break;
}
}
}
private void Get(NetworkingPlayer player, JSONNode data)
{
// Pull the game id and the filters from request
string gameId = data["id"];
string gameType = data["type"];
string gameMode = data["mode"];
int playerElo = data["elo"].AsInt;
if (_playerRequests.ContainsKey(player.Ip))
_playerRequests[player.Ip]++;
else
_playerRequests.Add(player.Ip, 1);
int delta = _playerRequests[player.Ip];
// Get only the list that has the game ids
List<Host> filter = (from host in hosts where host.Id == gameId select host).ToList();
// If "any" is supplied use all the types for this game id otherwise select only matching types
if (gameType != "any")
filter = (from host in filter where host.Type == gameType select host).ToList();
// If "all" is supplied use all the modes for this game id otherwise select only matching modes
if (gameMode != "all")
filter = (from host in filter where host.Mode == gameMode select host).ToList();
// Prepare the data to be sent back to the client
JSONNode sendData = JSONNode.Parse("{}");
JSONArray filterHosts = new JSONArray();
foreach (Host host in filter)
{
if (host.UseElo)
{
if (host.PlayerCount >= host.MaxPlayers) //Ignore servers with max capacity
continue;
if (_eloRangeSet && (playerElo > host.Elo - (EloRange * delta) &&
playerElo < host.Elo + (EloRange * delta)))
continue;
}
JSONClass hostData = new JSONClass();
hostData.Add("name", host.Name);
hostData.Add("address", host.Address);
hostData.Add("port", new JSONData(host.Port));
hostData.Add("comment", host.Comment);
hostData.Add("type", host.Type);
hostData.Add("mode", host.Mode);
hostData.Add("players", new JSONData(host.PlayerCount));
hostData.Add("maxPlayers", new JSONData(host.MaxPlayers));
hostData.Add("protocol", host.Protocol);
hostData.Add("elo", new JSONData(host.Elo));
hostData.Add("useElo", new JSONData(host.UseElo));
hostData.Add("eloDelta", new JSONData(delta));
filterHosts.Add(hostData);
}
if (filterHosts.Count > 0)
_playerRequests.Remove(player.Ip);
sendData.Add("hosts", filterHosts);
// Send the list of hosts (if any) back to the requesting client
server.Send(player.TcpClientHandle, Text.CreateFromString(server.Time.Timestep, sendData.ToString(), false, Receivers.Target, MessageGroupIds.MASTER_SERVER_GET, true));
}
public void Dispose()
{
server.Disconnect(true);
IsRunning = false;
}
}
}