forked from MidLevel/MLAPI.Relay
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
315 lines (300 loc) · 16.1 KB
/
Copy pathProgram.cs
File metadata and controls
315 lines (300 loc) · 16.1 KB
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using System.Threading;
using UnetServerDll;
namespace Server.Core
{
class Program
{
public static NetLibraryManager unet;
public static List<Room> rooms = new List<Room>();
public static Dictionary<string, Room> addressToRoom = new Dictionary<string, Room>();
public static byte[] messageBuffer;
public static byte GetReliableChannel()
{
for (byte i = 0; i < RelayConfig.CurrentConfig.connectionConfig.Channels.Count; i++)
{
switch (RelayConfig.CurrentConfig.connectionConfig.Channels[i].QOS)
{
case QosType.Reliable:
case QosType.ReliableFragmented:
case QosType.ReliableFragmentedSequenced:
case QosType.ReliableSequenced:
return i;
}
}
throw new InvalidConfigException("A reliable channel is required");
}
internal static ushort connectedPeers = 0;
static void Main(string[] args)
{
Console.Title = "MLAPI.Relay";
string configPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "config.json");
RelayConfig relayConfig = null;
if (!File.Exists(configPath))
{
Console.WriteLine("================================");
Console.WriteLine("There is no config. Please select a template for the config file:");
Console.WriteLine("[M]LAPI");
Console.WriteLine("[H]LAPI");
Console.WriteLine("[E]mpty");
ConsoleKey key = ConsoleKey.Escape;
do key = Console.ReadKey(true).Key;
while (key != ConsoleKey.M && key != ConsoleKey.H && key != ConsoleKey.E);
if (key == ConsoleKey.M)
{
ConnectionConfig cConfig = new ConnectionConfig()
{
SendDelay = 0
};
cConfig.AddChannel(QosType.ReliableFragmentedSequenced);
cConfig.AddChannel(QosType.StateUpdate);
cConfig.AddChannel(QosType.ReliableSequenced);
cConfig.AddChannel(QosType.ReliableSequenced);
cConfig.AddChannel(QosType.StateUpdate);
cConfig.AddChannel(QosType.Unreliable);
relayConfig = new RelayConfig()
{
bufferSize = 4096,
connectionConfig = cConfig,
globalConfig = new GlobalConfig(),
maxConnections = ushort.MaxValue - 1,
relayPort = 8888
};
}
else if (key == ConsoleKey.H)
{
ConnectionConfig cConfig = new ConnectionConfig();
cConfig.AddChannel(QosType.ReliableSequenced);
cConfig.AddChannel(QosType.Unreliable);
relayConfig = new RelayConfig()
{
bufferSize = 4096,
connectionConfig = cConfig,
globalConfig = new GlobalConfig(),
maxConnections = ushort.MaxValue - 1,
relayPort = 8888
};
}
else if (key == ConsoleKey.E)
{
ConnectionConfig cConfig = new ConnectionConfig();
relayConfig = new RelayConfig()
{
bufferSize = 1024,
connectionConfig = cConfig,
globalConfig = new GlobalConfig(),
maxConnections = ushort.MaxValue - 1,
relayPort = 8888
};
}
relayConfig.UnSetChannels();
File.WriteAllText(configPath, JsonConvert.SerializeObject(relayConfig, Formatting.Indented).Replace(@",
""ChannelCount"": 0,
""SharedOrderChannelCount"": 0,
""Channels"": []", ""));
}
else
{
try
{
relayConfig = JsonConvert.DeserializeObject<RelayConfig>(File.ReadAllText(configPath));
relayConfig.SetChannels();
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("[ERROR] Error parsing config file: " + ex.Message);
Console.Read();
return;
}
}
try
{
Start(relayConfig);
}
catch(DllNotFoundException e)
{
ReportError("[FATAL] Could not locate one or more shared libraries! Message: \n" + e.Message);
}
catch(Exception e)
{
ReportError("[FATAL] An unexpected error occurred! Message: \n" + e.Message);
}
}
private static void ReportError(string errorMessage)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Error.WriteLine(errorMessage);
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
private static string ReadLine(int bufferSize)
{
byte[] inputBuffer = new byte[bufferSize];
Stream inputStream = Console.OpenStandardInput(inputBuffer.Length);
Console.SetIn(new StreamReader(inputStream, Console.InputEncoding, false, inputBuffer.Length));
return Console.ReadLine();
}
private static ushort lastConnectedClients;
private static long lastPrintedConnectedClientsTick;
private static Timer statisticsReportTimer = null;
static void Start(RelayConfig rConfig)
{
RelayConfig.CurrentConfig = rConfig;
Console.WriteLine("================================");
Console.WriteLine("[INFO] Starting relay...");
unet = new NetLibraryManager(rConfig.globalConfig);
messageBuffer = new byte[RelayConfig.CurrentConfig.bufferSize];
HostTopology hostTopology = new HostTopology(RelayConfig.CurrentConfig.connectionConfig, RelayConfig.CurrentConfig.maxConnections);
unet.AddHost(hostTopology, RelayConfig.CurrentConfig.relayPort, null);
Console.WriteLine("[INFO] Relay started!");
Console.WriteLine("[INFO] Press [Q] to quit the application");
while ((!Console.KeyAvailable || Console.ReadKey(true).Key != ConsoleKey.Q))
{
NetworkEventType @event = unet.Receive(out int hostId, out int connectionId, out int channelId, messageBuffer, RelayConfig.CurrentConfig.bufferSize, out int receivedSize, out byte errorByte);
NetworkError error = (NetworkError)errorByte;
if (error != NetworkError.Ok)
{
ConsoleColor previousColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("[ERROR] Relay encountered UNET transport error \"" + error.ToString() + "\" during a \"" + @event.ToString() + "\" event.");
Console.ForegroundColor = previousColor;
}
switch (@event)
{
case NetworkEventType.DataEvent:
{
MessageType mType = (MessageType)messageBuffer[receivedSize - 1];
switch (mType)
{
case MessageType.StartServer:
{
//Check if they are already connected or perhaps are already hosting, if so return
Client client;
Room room = new Room(client = new Client()
{
connectionId = (ushort)connectionId,
hostId = (byte)hostId,
isServer = true,
connectTick = DateTime.UtcNow.Ticks
});
rooms.Add(room);
unet.GetConnectionInfo(hostId, connectionId, out string address, out int port, out byte byteError);
if (RelayConfig.CurrentConfig.enableRuntimeMetaLogging) Console.WriteLine("[INFO] Server started from address " + address);
addressToRoom.Add(address + ":" + port, room);
}
break;
case MessageType.ConnectToServer:
{
//Check if they are already connected or perhaps are already hosting; if so, return
byte addressLength = (byte)(receivedSize - 1);//messageBuffer[1]; // address length + ip type
string addressAndPort = Encoding.ASCII.GetString(messageBuffer, 0, addressLength);
addressAndPort = addressAndPort.AsIPv6CompatString();
if (RelayConfig.CurrentConfig.enableRuntimeMetaLogging) Console.WriteLine("[INFO] Connection requested to address " + addressAndPort);
if (addressToRoom.ContainsKey(addressAndPort))
{
if (RelayConfig.CurrentConfig.enableRuntimeMetaLogging) Console.WriteLine("[INFO] Connection approved");
Room room = addressToRoom[addressAndPort];
Client client = new Client()
{
connectionId = (ushort)connectionId,
hostId = (byte)hostId,
isServer = false,
connectTick = DateTime.UtcNow.Ticks
};
room.HandleClientConnect(client);
}
}
break;
case MessageType.Data:
{
foreach (var room in rooms)
{
if (room.HasPeer((ushort)connectionId, out bool isServer))
{
// Found a matching client in room
if (isServer)
{
ushort destination = (ushort)(messageBuffer[receivedSize - 3] | (messageBuffer[receivedSize - 2] << 8));
//Safety check. Make sure who they want to send to ACTUALLY belongs to their room
if (room.HasPeer(destination, out isServer) && !isServer)
{
// TODO: Use unsafe to make messageBuffer look smaller
//ReverseOffset(messageBuffer, 2, receivedSize);
messageBuffer[receivedSize - 3] = (byte)MessageType.Data; // [data, data, data, dest1, dest2, mtype_r, none, none, none] => [{data, data, data, mtype_s}, dest2, mtype_r, none, none, none]
room.Send(hostId, destination, connectionId, channelId, messageBuffer, receivedSize - 2, out errorByte);
}
}
else
{
// Read client message
//ForwardOffset(messageBuffer, 2, receivedSize);
messageBuffer.ToBytes((ushort)connectionId, receivedSize - 1); // Put connection id at the end of the recieved message (because optimization)
messageBuffer[receivedSize + 1] = (byte)MessageType.Data; // Set message type
room.Send(hostId, room.ServerID, connectionId, channelId, messageBuffer, receivedSize + 2, out errorByte);
}
}
}
}
break;
case MessageType.ClientDisconnect:
{
ushort cid = messageBuffer.FromBytes(1);
if (RelayConfig.CurrentConfig.enableRuntimeMetaLogging) Console.WriteLine("[INFO] Client disconnect request");
foreach (Room room in rooms)
if (room.HandleClientDisconnect((ushort)cid, true))
{
--connectedPeers;
break;
}
}
break;
}
}
break;
case NetworkEventType.DisconnectEvent:
{
if (RelayConfig.CurrentConfig.enableRuntimeMetaLogging) Console.WriteLine("[INFO] Peer disconnected");
foreach (Room room in rooms)
if (room.HandleClientDisconnect((ushort)connectionId))
{
--connectedPeers;
break;
}
}
break;
case NetworkEventType.Nothing:
Thread.Sleep(1);
break;
case NetworkEventType.ConnectEvent:
{
connectedPeers++;
}
break;
}
if (lastConnectedClients != connectedPeers && (DateTime.UtcNow.Ticks - lastPrintedConnectedClientsTick) > 30 * TimeSpan.TicksPerSecond)
{
lastPrintedConnectedClientsTick = DateTime.UtcNow.Ticks;
lastConnectedClients = connectedPeers;
PrintStatusUpdate();
}
}
Console.WriteLine("[INFO] Relay shutting down...");
}
private static void PrintStatusUpdate()
{
if (RelayConfig.CurrentConfig.enableRuntimeMetaLogging) Console.WriteLine("[STATUS] Connected peers: " + connectedPeers + " / " + RelayConfig.CurrentConfig.maxConnections);
}
}
public class InvalidConfigException : Exception
{
public InvalidConfigException() { }
public InvalidConfigException(string issue) : base(issue) { }
}
}