-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathProgram.cs
71 lines (65 loc) · 2.17 KB
/
Program.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
using System;
using System.Threading;
namespace DevelopersHub.RealtimeNetworking.Server
{
class Program
{
private static bool isRunning = false;
private const float updatePeriod = 1000f / Terminal.updates_per_second;
static void Main(string[] args)
{
AppDomain.CurrentDomain.ProcessExit += new EventHandler(OnExit);
if (Manager.enabled)
{
Manager.Initialize();
}
AppDomain.CurrentDomain.UnhandledException += GlobalUnhandledExceptionHandler;
try
{
Console.Title = "Server Console";
isRunning = true;
Thread mainThread = new Thread(new ThreadStart(MainThread));
mainThread.Start();
Server.Start(Terminal.max_players, Terminal.port);
}
catch (Exception ex)
{
Tools.LogError(ex.Message, ex.StackTrace);
}
}
private static void MainThread()
{
DateTime nextLoop = DateTime.Now;
while (isRunning)
{
while (nextLoop < DateTime.Now)
{
Terminal.Update();
if (Manager.enabled)
{
Manager.Update();
}
Threading.UpdateMain();
nextLoop = nextLoop.AddMilliseconds(updatePeriod);
if (nextLoop > DateTime.Now)
{
Thread.Sleep((int)Math.Clamp((nextLoop - DateTime.Now).TotalMilliseconds, 0, Int32.MaxValue));
}
}
}
}
private static void GlobalUnhandledExceptionHandler(object sender, UnhandledExceptionEventArgs e)
{
Exception ex = default(Exception);
ex = (Exception)e.ExceptionObject;
Tools.LogError(ex.Message, ex.StackTrace, "Unhandled");
}
private static void OnExit(object sender, EventArgs e)
{
if (Manager.enabled)
{
Manager.OnExit();
}
}
}
}