-
Notifications
You must be signed in to change notification settings - Fork 0
/
NetworkManager.cs
387 lines (336 loc) · 17.6 KB
/
NetworkManager.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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
using System;
using System.Text;
using System.Windows;
using System.Net;
using System.Net.Sockets;
using Nettention.Proud;
namespace MSB_SERVER
{
public class NetworkManager
{
private static NetworkManager INSTANCE;
private static App serverApplication;
public static bool IS_SERVER_RUNNING;
public DateTime serverStartTime;
private IPAddress SERVER_IP;
private int SERVER_PORT;
private NetServer networkServer;
private ThreadPool netWorkerThreadPool = new ThreadPool(4);
private ThreadPool userWorkerThreadPool = new ThreadPool(4);
private System.Guid guidVersion = new System.Guid("{0x27ad1634,0x381e,0x4228,{0x98,0xa,0xda,0xc8,0xeb,0x5f,0x4e,0x83}}");
internal MSBS2C.Proxy netS2CProxy = new MSBS2C.Proxy();
internal MSBC2S.Stub netC2SStub = new MSBC2S.Stub();
private NetworkManager()
{
serverApplication = (App) Application.Current;
IS_SERVER_RUNNING = false;
}
public static NetworkManager GetInstance()
{
if (INSTANCE == null) INSTANCE = new NetworkManager();
return INSTANCE;
}
public class StateObject
{
public Socket workSocket = null;
public const int bufferSize = 1024;
public byte[] buffer = new byte[bufferSize];
public StringBuilder stringBuilder = new StringBuilder();
}
public void ServerRun(string _SERVER_IP, int _SERVER_PORT)
{
try
{
SERVER_PORT = _SERVER_PORT;
try
{
serverApplication.logManager.ClearLog();
}
catch (Exception e)
{
Console.WriteLine(e);
}
if (_SERVER_IP.Equals("localhost"))
{
SERVER_IP = IPAddress.Parse("127.0.0.1");
}
else
{
SERVER_IP = IPAddress.Parse(_SERVER_IP);
}
serverApplication.logManager.NewLog(LogManager.LOG_LEVEL.LOG_NORMAL, LogManager.LOG_TARGET.LOG_SYSTEM, "NetworkManager", "서버 시작 [" + SERVER_IP + ":" + SERVER_PORT + "]");
// ReSharper disable once RedundantCheckBeforeAssignment
if (networkServer != null)
{
networkServer = null;
}
networkServer = new NetServer();
networkServer.AttachStub(netC2SStub);
networkServer.AttachProxy(netS2CProxy);
// ReSharper disable once RedundantAssignment
networkServer.ConnectionRequestHandler = (clientAddr, userData, reply) =>
{
reply = new ByteArray();
reply.Clear();
return true;
};
networkServer.ClientHackSuspectedHandler = (clientID, hackType) =>
{
serverApplication.logManager.NewLog(LogManager.LOG_LEVEL.LOG_NORMAL, LogManager.LOG_TARGET.LOG_SYSTEM, "NetworkManager", "Client Hack Suspected : " + clientID + ":" + hackType + "");
};
networkServer.ClientJoinHandler = clientInfo =>
{
serverApplication.serverManager.OnUserConnected(clientInfo.hostID);
};
networkServer.ClientLeaveHandler = (clientInfo, errorInfo, comment) =>
{
serverApplication.serverManager.OnUserDisconnected(clientInfo.hostID);
};
networkServer.ErrorHandler = errorInfo =>
{
serverApplication.logManager.NewLog(LogManager.LOG_LEVEL.LOG_NORMAL, LogManager.LOG_TARGET.LOG_SYSTEM, "NetworkManager", "Error : " + errorInfo);
};
networkServer.WarningHandler = errorInfo =>
{
serverApplication.logManager.NewLog(LogManager.LOG_LEVEL.LOG_NORMAL, LogManager.LOG_TARGET.LOG_SYSTEM, "NetworkManager", "Warning : " + errorInfo);
};
networkServer.ExceptionHandler = e =>
{
serverApplication.logManager.NewLog(LogManager.LOG_LEVEL.LOG_NORMAL, LogManager.LOG_TARGET.LOG_SYSTEM, "NetworkManager", "Exception : " + e);
};
networkServer.InformationHandler = errorInfo =>
{
serverApplication.logManager.NewLog(LogManager.LOG_LEVEL.LOG_NORMAL, LogManager.LOG_TARGET.LOG_SYSTEM, "NetworkManager", "Information : " + errorInfo);
};
networkServer.NoRmiProcessedHandler = rmiID =>
{
serverApplication.logManager.NewLog(LogManager.LOG_LEVEL.LOG_NORMAL, LogManager.LOG_TARGET.LOG_SYSTEM, "NetworkManager", "NoRmiProcessed : " + rmiID);
};
networkServer.P2PGroupJoinMemberAckCompleteHandler = (groupHostID, memberHostID, result) =>
{
serverApplication.logManager.NewLog(LogManager.LOG_LEVEL.LOG_NORMAL, LogManager.LOG_TARGET.LOG_SYSTEM, "NetworkManager", "P2P Group Complete : " + groupHostID);
};
networkServer.TickHandler = context =>
{
serverApplication.logManager.NewLog(LogManager.LOG_LEVEL.LOG_NORMAL, LogManager.LOG_TARGET.LOG_SYSTEM, "NetworkManager", "Tick : " + context);
};
networkServer.UserWorkerThreadBeginHandler = () =>
{
serverApplication.logManager.NewLog(LogManager.LOG_LEVEL.LOG_NORMAL, LogManager.LOG_TARGET.LOG_SYSTEM, "NetworkManager", "UserWorkerThread Start");
};
networkServer.UserWorkerThreadEndHandler = () =>
{
serverApplication.logManager.NewLog(LogManager.LOG_LEVEL.LOG_NORMAL, LogManager.LOG_TARGET.LOG_SYSTEM, "NetworkManager", "UserWorkerThread End");
};
StartServerParameter startParameter = new StartServerParameter();
startParameter.protocolVersion = new Nettention.Proud.Guid(guidVersion);
startParameter.tcpPorts = new IntArray();
startParameter.tcpPorts.Add(SERVER_PORT);
startParameter.serverAddrAtClient = "203.250.148.113";
startParameter.localNicAddr = "203.250.148.113";
startParameter.SetExternalNetWorkerThreadPool(netWorkerThreadPool);
startParameter.SetExternalUserWorkerThreadPool(userWorkerThreadPool);
InitializeListeners();
networkServer.Start(startParameter);
IS_SERVER_RUNNING = true;
serverApplication.graphicalManager.OnServerStatusChanged(true, IS_SERVER_RUNNING);
serverStartTime = DateTime.Now;
serverApplication.logManager.StartLogger();
serverApplication.databaseManager.StartDatabase();
serverApplication.serverManager.StartQueue();
serverApplication.serverManager.StartRoom();
serverApplication.serverManager.StartStatus();
serverApplication.statusManager.StartModules();
} catch (Exception e)
{
((App) Application.Current).MSBUnhandledException(e, "NetworkManager");
ServerStop();
}
}
public void ServerStop()
{
IS_SERVER_RUNNING = false;
serverApplication.graphicalManager.OnServerStatusChanged(false, IS_SERVER_RUNNING);
serverApplication.statusManager.StopModules();
serverApplication.serverManager.StopStatus();
serverApplication.serverManager.StopQueue();
serverApplication.serverManager.StopRoom();
serverApplication.databaseManager.StopDatabase();
serverApplication.logManager.StopLogger();
//networkServer.Dispose();
networkServer = null;
}
private void InitializeListeners()
{
netC2SStub.OnLoginRequest = OnUserLogin;
netC2SStub.OnStatusRequest = OnUserStatus;
netC2SStub.OnSystemRequest = OnSystemRequest;
netC2SStub.OnGameQueueRequest = OnGameQueue;
netC2SStub.OnGameInfoRequest = OnGameInfo;
netC2SStub.OnGameActionReady = OnGameUserActionReady;
netC2SStub.OnGameActionDamage = OnGameUserActionDamage;
netC2SStub.OnGameActionObject = OnGameUserActionObject;
netC2SStub.OnGameActionItem = OnGameUserActionItem;
netC2SStub.OnGameUserMove = OnGameUserMove;
netC2SStub.OnGameUserSync = OnGameUserSync;
}
private static bool OnUserLogin(HostID host, RmiContext rmiContext, string _data)
{
try
{
serverApplication.logManager.NewLog(LogManager.LOG_LEVEL.LOG_DEBUG, LogManager.LOG_TARGET.LOG_NETWORK, "NetworkManager", "OnUserLogin");
ServerManager.NetworkGate.EventUserLogin(host, _data);
}
catch (Exception e)
{
serverApplication.logManager.NewLog(LogManager.LOG_LEVEL.LOG_DEBUG, LogManager.LOG_TARGET.LOG_NETWORK, "NetworkManager", "OnUserLogin ERROR");
serverApplication.logManager.NewLog(LogManager.LOG_LEVEL.LOG_DEBUG, LogManager.LOG_TARGET.LOG_NETWORK, "NetworkManager", e.ToString());
}
return true;
}
private static bool OnUserStatus(HostID host, RmiContext rmiContext, string _data)
{
try
{
serverApplication.logManager.NewLog(LogManager.LOG_LEVEL.LOG_DEBUG, LogManager.LOG_TARGET.LOG_NETWORK, "NetworkManager", "OnUserStatus");
ServerManager.NetworkGate.EventUserStatus(host, _data);
}
catch (Exception e)
{
serverApplication.logManager.NewLog(LogManager.LOG_LEVEL.LOG_DEBUG, LogManager.LOG_TARGET.LOG_NETWORK, "NetworkManager", "OnUserStatus ERROR");
serverApplication.logManager.NewLog(LogManager.LOG_LEVEL.LOG_DEBUG, LogManager.LOG_TARGET.LOG_NETWORK, "NetworkManager", e.ToString());
}
return true;
}
private static bool OnSystemRequest(HostID host, RmiContext rmiContext, string _data)
{
try
{
serverApplication.logManager.NewLog(LogManager.LOG_LEVEL.LOG_DEBUG, LogManager.LOG_TARGET.LOG_NETWORK, "NetworkManager", "OnSystemRequest");
ServerManager.NetworkGate.EventUserSystem(host, _data);
}
catch (Exception e)
{
serverApplication.logManager.NewLog(LogManager.LOG_LEVEL.LOG_DEBUG, LogManager.LOG_TARGET.LOG_NETWORK, "NetworkManager", "OnSystemRequest ERROR");
serverApplication.logManager.NewLog(LogManager.LOG_LEVEL.LOG_DEBUG, LogManager.LOG_TARGET.LOG_NETWORK, "NetworkManager", e.ToString());
}
return true;
}
private static bool OnGameQueue(HostID host, RmiContext rmiContext, string _data)
{
try
{
serverApplication.logManager.NewLog(LogManager.LOG_LEVEL.LOG_DEBUG, LogManager.LOG_TARGET.LOG_NETWORK, "NetworkManager", "OnGameQueue");
ServerManager.NetworkGate.EventGameQueue(host, _data);
}
catch (Exception e)
{
serverApplication.logManager.NewLog(LogManager.LOG_LEVEL.LOG_DEBUG, LogManager.LOG_TARGET.LOG_NETWORK, "NetworkManager", "OnGameQueue ERROR");
serverApplication.logManager.NewLog(LogManager.LOG_LEVEL.LOG_DEBUG, LogManager.LOG_TARGET.LOG_NETWORK, "NetworkManager", e.ToString());
}
return true;
}
private static bool OnGameInfo(HostID host, RmiContext rmiContext, int _room, string _data)
{
try
{
serverApplication.logManager.NewLog(LogManager.LOG_LEVEL.LOG_DEBUG, LogManager.LOG_TARGET.LOG_NETWORK, "NetworkManager", "OnGameInfo");
ServerManager.NetworkGate.EventGameInfo(host, _room, _data);
}
catch (Exception e)
{
serverApplication.logManager.NewLog(LogManager.LOG_LEVEL.LOG_DEBUG, LogManager.LOG_TARGET.LOG_NETWORK, "NetworkManager", "OnGameInfo ERROR");
serverApplication.logManager.NewLog(LogManager.LOG_LEVEL.LOG_DEBUG, LogManager.LOG_TARGET.LOG_NETWORK, "NetworkManager", e.ToString());
}
return true;
}
private static bool OnGameUserActionReady(HostID host, RmiContext rmiContext, int _room, string _data)
{
try
{
serverApplication.logManager.NewLog(LogManager.LOG_LEVEL.LOG_DEBUG, LogManager.LOG_TARGET.LOG_NETWORK, "NetworkManager", "OnGameUserActionReady");
ServerManager.NetworkGate.EventGameActionReady(host, _room, _data);
}
catch (Exception e)
{
serverApplication.logManager.NewLog(LogManager.LOG_LEVEL.LOG_DEBUG, LogManager.LOG_TARGET.LOG_NETWORK, "NetworkManager", "OnGameUserActionReady ERROR");
serverApplication.logManager.NewLog(LogManager.LOG_LEVEL.LOG_DEBUG, LogManager.LOG_TARGET.LOG_NETWORK, "NetworkManager", e.ToString());
}
return true;
}
private static bool OnGameUserActionDamage(HostID host, RmiContext rmiContext, int _room, string _data)
{
try
{
serverApplication.logManager.NewLog(LogManager.LOG_LEVEL.LOG_DEBUG, LogManager.LOG_TARGET.LOG_NETWORK, "NetworkManager", "OnGameUserActionDamage");
ServerManager.NetworkGate.EventGameActionDamage(host, _room, _data);
}
catch (Exception e)
{
serverApplication.logManager.NewLog(LogManager.LOG_LEVEL.LOG_DEBUG, LogManager.LOG_TARGET.LOG_NETWORK, "NetworkManager", "OnGameUserActionDamage ERROR");
serverApplication.logManager.NewLog(LogManager.LOG_LEVEL.LOG_DEBUG, LogManager.LOG_TARGET.LOG_NETWORK, "NetworkManager", e.ToString());
}
return true;
}
private static bool OnGameUserActionObject(HostID host, RmiContext rmiContext, int _room, string _data)
{
try
{
serverApplication.logManager.NewLog(LogManager.LOG_LEVEL.LOG_DEBUG, LogManager.LOG_TARGET.LOG_NETWORK, "NetworkManager", "OnGameUserActionObject");
ServerManager.NetworkGate.EventGameActionObject(host, _room, _data);
}
catch (Exception e)
{
serverApplication.logManager.NewLog(LogManager.LOG_LEVEL.LOG_DEBUG, LogManager.LOG_TARGET.LOG_NETWORK, "NetworkManager", "OnGameUserActionObject ERROR");
serverApplication.logManager.NewLog(LogManager.LOG_LEVEL.LOG_DEBUG, LogManager.LOG_TARGET.LOG_NETWORK, "NetworkManager", e.ToString());
}
return true;
}
private static bool OnGameUserActionItem(HostID host, RmiContext rmiContext, int _room, string _data)
{
try
{
serverApplication.logManager.NewLog(LogManager.LOG_LEVEL.LOG_DEBUG, LogManager.LOG_TARGET.LOG_NETWORK, "NetworkManager", "OnGameUserActionItem");
ServerManager.NetworkGate.EventGameActionItem(host, _room, _data);
}
catch (Exception e)
{
serverApplication.logManager.NewLog(LogManager.LOG_LEVEL.LOG_DEBUG, LogManager.LOG_TARGET.LOG_NETWORK, "NetworkManager", "OnGameUserActionItem ERROR");
serverApplication.logManager.NewLog(LogManager.LOG_LEVEL.LOG_DEBUG, LogManager.LOG_TARGET.LOG_NETWORK, "NetworkManager", e.ToString());
}
return true;
}
private static bool OnGameUserMove(HostID host, RmiContext rmiContext, int _room, string _data)
{
try
{
serverApplication.logManager.NewLog(LogManager.LOG_LEVEL.LOG_DEBUG, LogManager.LOG_TARGET.LOG_NETWORK, "NetworkManager", "OnGameUserMove");
ServerManager.NetworkGate.EventGameUserMove(host, _room, _data);
}
catch (Exception e)
{
serverApplication.logManager.NewLog(LogManager.LOG_LEVEL.LOG_DEBUG, LogManager.LOG_TARGET.LOG_NETWORK, "NetworkManager", "OnGameUserMove ERROR");
serverApplication.logManager.NewLog(LogManager.LOG_LEVEL.LOG_DEBUG, LogManager.LOG_TARGET.LOG_NETWORK, "NetworkManager", e.ToString());
}
return true;
}
private static bool OnGameUserSync(HostID host, RmiContext rmiContext, int _room, string _data)
{
try
{
serverApplication.logManager.NewLog(LogManager.LOG_LEVEL.LOG_DEBUG, LogManager.LOG_TARGET.LOG_NETWORK, "NetworkManager", "OnGameUserSync");
ServerManager.NetworkGate.EventGameUserSync(host, _room, _data);
}
catch (Exception e)
{
serverApplication.logManager.NewLog(LogManager.LOG_LEVEL.LOG_DEBUG, LogManager.LOG_TARGET.LOG_NETWORK, "NetworkManager", "OnGameUserSync ERROR");
serverApplication.logManager.NewLog(LogManager.LOG_LEVEL.LOG_DEBUG, LogManager.LOG_TARGET.LOG_NETWORK, "NetworkManager", e.ToString());
}
return true;
}
public static bool IsServerRunning()
{
return IS_SERVER_RUNNING;
}
}
}