forked from Skippeh/Oxide.GettingOverItMP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MasterServer.cs
123 lines (106 loc) · 4.1 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
using System;
using System.Collections.Specialized;
using System.Net;
using System.Text;
using System.Threading;
using Newtonsoft.Json.Linq;
using ServerShared.Logging;
namespace ServerShared
{
public static class MasterServer
{
public static GameServer Server { get; set; }
private static DateTime nextBeat;
private static int failedAttempts = 0;
private static Exception lastException;
private static bool started;
private static Thread updateThread;
private static WebClient webClient;
/// <summary>Max retries before giving up until the next heartbeat.</summary>
private const int MaxRetries = 2;
/// <summary>How often to send heartbeat to master server in minutes.</summary>
private const int BeatInterval = 1;
/// <summary>How often to retry sending heartbeat to master server in minutes.</summary>
private const double FailBeatInterval = 5 / 60d; // 5 seconds
public static void Start(GameServer server)
{
if (started)
throw new Exception("Start was called while already running.");
started = true;
Server = server ?? throw new ArgumentNullException(nameof(server));
webClient = new WebClient();
updateThread = new Thread(DoUpdateThread);
updateThread.Start();
Logger.LogDebug("Started beating to master server.");
}
public static void Stop()
{
webClient.Dispose();
webClient = null;
started = false;
}
private static void DoUpdateThread()
{
while (started)
{
if (DateTime.UtcNow >= nextBeat)
{
if (Beat(Server.Port))
{
nextBeat = DateTime.UtcNow.AddMinutes(BeatInterval);
failedAttempts = 0;
}
else
{
if (failedAttempts >= MaxRetries)
{
// Give up this time, try again at the next beat interval.
nextBeat = DateTime.UtcNow.AddMinutes(BeatInterval);
failedAttempts = 0;
Logger.LogException("Failed to send heartbeat to master server.", lastException);
}
else
{
// Try again after a shorter amount of time.
nextBeat = DateTime.UtcNow.AddMinutes(FailBeatInterval);
failedAttempts++;
}
}
}
Thread.Sleep(100);
}
}
private static bool Beat(int port)
{
try
{
byte[] byteResponse = webClient.UploadValues($"{SharedConstants.MasterServerUrl}/beat", "POST", new NameValueCollection
{
{"port", port.ToString()},
{"version", SharedConstants.Version.ToString() }
});
string json = Encoding.UTF8.GetString(byteResponse);
if (!webClient.ResponseHeaders["Content-Type"].StartsWith("application/json"))
return true;
JObject response = JObject.Parse(json);
string status = response["status"].ToObject<string>();
string message = response["message"].ToObject<string>();
if (status == "warning")
{
Logger.LogWarning($"MasterServer responded with a warning: {message}");
}
else if (status == "error")
{
Logger.LogError($"MasterServer responded with an error: {message}");
return false;
}
return true;
}
catch (Exception ex)
{
lastException = ex;
return false;
}
}
}
}