We don't need a MasterServer class
This commit is contained in:
125
Server/Server.cs
125
Server/Server.cs
@ -17,72 +17,6 @@ namespace CoopServer
|
|||||||
public string Country { get; set; }
|
public string Country { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
class MasterServer
|
|
||||||
{
|
|
||||||
private Thread MainThread;
|
|
||||||
|
|
||||||
public void Start()
|
|
||||||
{
|
|
||||||
MainThread = new Thread(Listen);
|
|
||||||
MainThread.Start();
|
|
||||||
}
|
|
||||||
|
|
||||||
private async void Listen()
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
IpInfo info;
|
|
||||||
try
|
|
||||||
{
|
|
||||||
using HttpClient httpClient = new();
|
|
||||||
string data = await httpClient.GetStringAsync("https://ipinfo.io/json");
|
|
||||||
|
|
||||||
info = JsonConvert.DeserializeObject<IpInfo>(data);
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
info = new() { Country = "?" };
|
|
||||||
}
|
|
||||||
|
|
||||||
bool responseError = false;
|
|
||||||
HttpClient client = new();
|
|
||||||
|
|
||||||
while (!responseError)
|
|
||||||
{
|
|
||||||
string msg =
|
|
||||||
"{ " +
|
|
||||||
"\"port\": \"" + Server.MainSettings.ServerPort + "\", " +
|
|
||||||
"\"name\": \"" + Server.MainSettings.ServerName + "\", " +
|
|
||||||
"\"version\": \"" + Server.CurrentModVersion.Replace("_", ".") + "\", " +
|
|
||||||
"\"players\": \"" + Server.MainNetServer.ConnectionsCount + "\", " +
|
|
||||||
"\"maxPlayers\": \"" + Server.MainSettings.MaxPlayers + "\", " +
|
|
||||||
"\"allowlist\": \"" + Server.MainSettings.Allowlist + "\", " +
|
|
||||||
"\"country\": \"" + info.Country + "\"" +
|
|
||||||
" }";
|
|
||||||
|
|
||||||
HttpResponseMessage response = await client.PostAsync(Server.MainSettings.MasterServer, new StringContent(msg, Encoding.UTF8, "application/json"));
|
|
||||||
|
|
||||||
string responseContent = await response.Content.ReadAsStringAsync();
|
|
||||||
|
|
||||||
if (responseContent != "OK!")
|
|
||||||
{
|
|
||||||
Logging.Error(responseContent);
|
|
||||||
responseError = true;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// Sleep for 10s
|
|
||||||
Thread.Sleep(10000);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
Logging.Error(ex.Message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class Server
|
class Server
|
||||||
{
|
{
|
||||||
public static readonly string CurrentModVersion = Enum.GetValues(typeof(ModVersion)).Cast<ModVersion>().Last().ToString();
|
public static readonly string CurrentModVersion = Enum.GetValues(typeof(ModVersion)).Cast<ModVersion>().Last().ToString();
|
||||||
@ -93,8 +27,6 @@ namespace CoopServer
|
|||||||
|
|
||||||
public static NetServer MainNetServer;
|
public static NetServer MainNetServer;
|
||||||
|
|
||||||
private readonly MasterServer MainMasterServer = new();
|
|
||||||
|
|
||||||
private static readonly Dictionary<string, EntitiesPlayer> Players = new();
|
private static readonly Dictionary<string, EntitiesPlayer> Players = new();
|
||||||
|
|
||||||
public Server()
|
public Server()
|
||||||
@ -130,7 +62,62 @@ namespace CoopServer
|
|||||||
|
|
||||||
if (MainSettings.AnnounceSelf)
|
if (MainSettings.AnnounceSelf)
|
||||||
{
|
{
|
||||||
MainMasterServer.Start();
|
#region -- MASTERSERVER --
|
||||||
|
new Thread(async () =>
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
IpInfo info;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using HttpClient httpClient = new();
|
||||||
|
string data = await httpClient.GetStringAsync("https://ipinfo.io/json");
|
||||||
|
|
||||||
|
info = JsonConvert.DeserializeObject<IpInfo>(data);
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
info = new() { Country = "?" };
|
||||||
|
}
|
||||||
|
|
||||||
|
bool responseError = false;
|
||||||
|
HttpClient client = new();
|
||||||
|
|
||||||
|
while (!responseError)
|
||||||
|
{
|
||||||
|
string msg =
|
||||||
|
"{ " +
|
||||||
|
"\"port\": \"" + Server.MainSettings.ServerPort + "\", " +
|
||||||
|
"\"name\": \"" + Server.MainSettings.ServerName + "\", " +
|
||||||
|
"\"version\": \"" + Server.CurrentModVersion.Replace("_", ".") + "\", " +
|
||||||
|
"\"players\": \"" + Server.MainNetServer.ConnectionsCount + "\", " +
|
||||||
|
"\"maxPlayers\": \"" + Server.MainSettings.MaxPlayers + "\", " +
|
||||||
|
"\"allowlist\": \"" + Server.MainSettings.Allowlist + "\", " +
|
||||||
|
"\"country\": \"" + info.Country + "\"" +
|
||||||
|
" }";
|
||||||
|
|
||||||
|
HttpResponseMessage response = await client.PostAsync(Server.MainSettings.MasterServer, new StringContent(msg, Encoding.UTF8, "application/json"));
|
||||||
|
|
||||||
|
string responseContent = await response.Content.ReadAsStringAsync();
|
||||||
|
|
||||||
|
if (responseContent != "OK!")
|
||||||
|
{
|
||||||
|
Logging.Error(responseContent);
|
||||||
|
responseError = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Sleep for 10s
|
||||||
|
Thread.Sleep(10000);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Logging.Error(ex.Message);
|
||||||
|
}
|
||||||
|
}).Start();
|
||||||
|
#endregion
|
||||||
}
|
}
|
||||||
|
|
||||||
Listen();
|
Listen();
|
||||||
|
Reference in New Issue
Block a user