-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathProgram.cs
More file actions
351 lines (309 loc) · 12.2 KB
/
Program.cs
File metadata and controls
351 lines (309 loc) · 12.2 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
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
#region License
// ====================================================
// AsyncServerClient Copyright(C) 2015-2019 Furkan Türkal
// This program comes with ABSOLUTELY NO WARRANTY; This is free software,
// and you are welcome to redistribute it under certain conditions; See
// file LICENSE, which is part of this source code package, for details.
// ====================================================
#endregion
using System;
using System.IO;
using System.Threading;
using System.Reflection;
namespace ServerConsole
{
class Program
{
public static Config cfgGateway;
private static SQLProcess sqlProcessAccount;
private static SQLProcess sqlProcessShard;
private static SQLProcess sqlProcessLog;
private static bool bLoadModule = false;
static void Main(string[] args)
{
if (bLoadModule != true)
{
Console.Title = Assembly.GetExecutingAssembly().GetName().Name;
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Load Module Deactivated !!!");
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine();
Console.WriteLine("====================================");
Console.WriteLine("Console initialized successfully.");
Console.WriteLine("====================================");
Console.WriteLine();
Console.ResetColor();
if (SetupConfig() == false) {
Logger.DebugToLog("Config file load error !!!");
Console.ReadKey();
}
if (cfgGateway.boolDebugMode)
{
Logger.DebugToLog("Debug Mode Activated !!!");
}
else
{
Logger.DebugToLog("Debug Mode Deactivated !!!");
}
Console.ForegroundColor = ConsoleColor.Green;
Start();
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Load Module Activated !!!");
Console.WriteLine();
if (cfgGateway.boolDebugMode)
{
Logger.DebugToLog("Debug Mode Activated !!!");
}
else
{
Logger.DebugToLog("Debug Mode Deactivated !!!");
}
Console.WriteLine();
Console.ResetColor();
AppDomain currentDomain = AppDomain.CurrentDomain;
LoadAssembly(currentDomain);
}
}
private static void LoadAssembly(AppDomain domain)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("====================================");
Console.WriteLine("LOADED ASSEMBLIES:");
Console.WriteLine("====================================");
Console.WriteLine();
foreach (Assembly a in domain.GetAssemblies())
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write("Assembly Loaded: ");
Console.ForegroundColor = ConsoleColor.White;
Console.Write(a.ManifestModule.Name);
Console.WriteLine();
Thread.Sleep(100);
if (a.ManifestModule.Name.Contains(Assembly.GetExecutingAssembly().GetName().Name))
{
SetupConsole();
}
}
Console.ResetColor();
}
private static void SetupConsole()
{
Console.ForegroundColor = ConsoleColor.Green;
Console.Title = Assembly.GetExecutingAssembly().GetName().Name;
Console.WriteLine();
Console.WriteLine("====================================");
Console.WriteLine("Console initialized successfully.");
Console.WriteLine("====================================");
Console.WriteLine();
Thread.Sleep(100);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write("Assembly Location : ");
Console.ForegroundColor = ConsoleColor.White;
Console.Write(Assembly.GetExecutingAssembly().Location);
Console.WriteLine();
Thread.Sleep(100);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write("Assembly Name : ");
Console.ForegroundColor = ConsoleColor.White;
Console.Write(Assembly.GetExecutingAssembly().GetName().Name);
Console.WriteLine();
Thread.Sleep(100);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write("HashAlgorithm : ");
Console.ForegroundColor = ConsoleColor.White;
Console.Write(Assembly.GetExecutingAssembly().GetName().HashAlgorithm);
Console.WriteLine();
Thread.Sleep(100);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write("ProcessorArchitecture : ");
Console.ForegroundColor = ConsoleColor.White;
Console.Write(Assembly.GetExecutingAssembly().GetName().ProcessorArchitecture);
Console.WriteLine();
Thread.Sleep(100);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write("Version : ");
Console.ForegroundColor = ConsoleColor.White;
Console.Write(Assembly.GetExecutingAssembly().GetName().Version);
Console.WriteLine();
Thread.Sleep(100);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write("VersionCompatibility : ");
Console.ForegroundColor = ConsoleColor.White;
Console.Write(Assembly.GetExecutingAssembly().GetName().VersionCompatibility);
Console.WriteLine();
Thread.Sleep(100);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write("Buid Date : ");
Console.ForegroundColor = ConsoleColor.White;
Console.Write(BuildDate().ToString());
Console.WriteLine();
Thread.Sleep(100);
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine();
Console.WriteLine("====================================");
Console.WriteLine(Assembly.GetExecutingAssembly().GetName().Name + " started successfully.");
Console.WriteLine("====================================");
Console.WriteLine();
Thread.Sleep(100);
Console.ResetColor();
Awake();
}
private static DateTime BuildDate()
{
string filePath = Assembly.GetCallingAssembly().Location;
const int c_PeHeaderOffset = 60;
const int c_LinkerTimestampOffset = 8;
byte[] b = new byte[2048];
Stream s = null;
try
{
s = new FileStream(filePath, FileMode.Open, FileAccess.Read);
s.Read(b, 0, 2048);
}
finally
{
if (s != null)
{
s.Close();
}
}
int i = BitConverter.ToInt32(b, c_PeHeaderOffset);
int secondsSince1970 = BitConverter.ToInt32(b, i + c_LinkerTimestampOffset);
DateTime dt = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
dt = dt.AddSeconds(secondsSince1970);
dt = dt.ToLocalTime();
return dt;
}
private static bool SetupConfig()
{
cfgGateway = new Config();
if (cfgGateway.LoadConfig())
{
return true;
}
return false;
}
private static bool SetupDatabase()
{
sqlProcessAccount = new SQLProcess(cfgGateway.strSQLConnectionStringAccount);
sqlProcessShard = new SQLProcess(cfgGateway.strSQLConnectionStringShard);
sqlProcessLog = new SQLProcess(cfgGateway.strSQLConnectionStringLog);
int intIndex = 0;
if (sqlProcessAccount.isWorking())
{
intIndex += 1;
Logger.LogToStatus("Database connected successfully: Account", Logger.enLogLevel.INFO);
}
if (sqlProcessShard.isWorking())
{
intIndex += 1;
Logger.LogToStatus("Database connected successfully: Shard", Logger.enLogLevel.INFO);
}
if (sqlProcessLog.isWorking())
{
intIndex += 1;
Logger.LogToStatus("Database connected successfully: Log", Logger.enLogLevel.INFO);
}
if (intIndex == 3)
{
return true;
}
return false;
}
private static void Awake()
{
try
{
if (SetupConfig())
{
Logger.LogToStatus("Config has been loaded successfully.", Logger.enLogLevel.INFO);
Logger.LogToStatus("Settings loaded. Checking database connection...", Logger.enLogLevel.INFO);
}
else
{
Logger.WriteToConsole("INI file was an error on loading. Please try again.", Logger.enLogLevel.ERROR);
return;
}
Thread.Sleep(1000);
if (SetupDatabase())
{
Thread.Sleep(1000);
Logger.LogToStatus("Database connection functions working.", Logger.enLogLevel.INFO);
Thread.Sleep(1000);
}
else
{
Logger.LogToStatus("Database connection error. Please try again.", Logger.enLogLevel.ERROR);
return;
}
Start();
}
catch(Exception ex)
{
Logger.ExceptionToLog(ex, "[Program::Awake()] Config loading error.");
}
}
private static void Start()
{
try
{
//GatewayServer s_Tcp = new GatewayServer(cfgGateway.strIP, cfgGateway.strPort);
GatewayServer s_Tcp = new GatewayServer("192.168.1.2", "15550");
ConsoleUpdate();
ReadUpdate();
}
catch (Exception ex)
{
Logger.ExceptionToLog(ex, "[Program::Start()] -> TcpServer error.");
}
}
public static void CollectGarbage()
{
try
{
long n_BeforeGC = GC.GetTotalMemory(false);
double mb_BeforeGC = ConvertBytesToMegabytes(n_BeforeGC);
GC.Collect();
GC.WaitForPendingFinalizers();
long n_AfterGC = GC.GetTotalMemory(true);
double mb_AfterGC = ConvertBytesToMegabytes(n_AfterGC);
double mb_CleanedGC = mb_BeforeGC - mb_AfterGC;
Logger.LogToStatus("All garbage were collected. [Cleaned : " + mb_CleanedGC + " MB]", Logger.enLogLevel.INFO);
n_BeforeGC = 0;
n_AfterGC = 0;
}
catch
{
}
}
static double ConvertBytesToMegabytes(long bytes)
{
return Math.Round((bytes / 1024f) / 1024f,3);
}
public static void ConsoleUpdate()
{
while (true)
{
Console.Title = Assembly.GetExecutingAssembly().GetName().Name + "[Clients:" + GatewayServer.getClientCount() + "]";
Thread.Sleep(10);
//Thread.Sleep(cfgGateway.intGarbageCollectTime);
}
}
public static void ReadUpdate()
{
while (true)
{
Thread.Sleep(5000);
if (GatewayServer.getClientCount() == 0)
{
CollectGarbage();
break;
}
//Thread.Sleep(cfgGateway.intGarbageCollectTime);
}
}
}
}