SanAndreasUnity/Assets/Scripts/Networking/MasterServerClient.cs

141 lines
4.1 KiB
C#
Raw Normal View History

2021-01-12 22:12:23 +00:00
using Newtonsoft.Json;
using SanAndreasUnity.Utilities;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
2021-01-12 22:12:23 +00:00
namespace SanAndreasUnity.Net
{
2021-01-12 22:12:23 +00:00
public class MasterServerClient : MonoBehaviour
{
2021-01-12 22:12:23 +00:00
private string _masterServerUrl;
private ServerInfo _serverInfo;
private bool _updating;
public static MasterServerClient Instance { get; private set; }
private HttpClient _client;
2021-01-27 00:25:43 +00:00
private const int RegisterInterval = 20;
public bool IsServerRegistrationEnabled { get; set; } = true;
2021-01-12 22:12:23 +00:00
private void Awake()
{
Instance = this;
}
2021-01-12 22:12:23 +00:00
private void Start()
{
_client = new HttpClient();
2021-01-27 01:08:16 +00:00
_client.Timeout = System.TimeSpan.FromSeconds(7);
2021-01-12 22:11:10 +00:00
2021-10-21 08:50:23 +00:00
_masterServerUrl = Config.GetString("master_server_url");
2021-01-12 22:12:23 +00:00
if (string.IsNullOrWhiteSpace(_masterServerUrl))
Debug.LogError("Url of master server not defined in config");
2021-01-12 22:12:23 +00:00
NetManager.Instance.onServerStatusChanged += OnServerStatusChange;
}
2021-01-12 22:11:10 +00:00
2021-01-12 22:12:23 +00:00
private async void OnServerStatusChange()
{
2021-01-12 22:12:23 +00:00
if (!NetStatus.IsServer)
return;
2021-01-12 22:11:10 +00:00
2021-01-12 22:12:23 +00:00
if (string.IsNullOrWhiteSpace(_masterServerUrl))
return;
if (!IsServerRegistrationEnabled)
return;
_serverInfo = GetCurrentServerInfo();
await RegisterServer();
}
private static ServerInfo GetCurrentServerInfo()
{
return new ServerInfo
2021-01-12 22:12:23 +00:00
{
2021-10-21 08:50:23 +00:00
Name = Config.GetString("server_name"),
2021-01-12 22:12:23 +00:00
Port = NetManager.listenPortNumber,
NumPlayersOnline = NetManager.numConnections,
MaxPlayers = NetManager.maxNumPlayers,
Map = UnityEngine.SceneManagement.SceneManager.GetActiveScene().name,
Version = Application.version,
2021-01-12 22:12:23 +00:00
};
}
2021-01-12 22:12:23 +00:00
private async Task RegisterServer()
{
2021-01-12 22:11:10 +00:00
await SendRequestToRegister();
2021-01-12 22:12:23 +00:00
_updating = true;
2021-01-27 00:25:43 +00:00
Invoke(nameof(UpdateServer), RegisterInterval);
2021-01-12 22:11:10 +00:00
}
2021-01-12 22:12:23 +00:00
private async Task UpdateServer()
{
while (_updating)
{
_serverInfo = GetCurrentServerInfo();
2021-01-12 22:12:23 +00:00
await SendRequestToRegister();
2021-01-12 22:11:10 +00:00
2021-01-27 00:25:43 +00:00
await Task.Delay(RegisterInterval * 1000);
2021-01-12 22:12:23 +00:00
}
}
private async Task SendRequestToRegister()
2021-01-12 22:11:10 +00:00
{
2021-01-12 22:12:23 +00:00
var response = await _client.PostAsync(_masterServerUrl + "/register", new StringContent(JsonConvert.SerializeObject(_serverInfo), Encoding.UTF8, "application/json"));
if (!response.IsSuccessStatusCode)
{
string str = await response.Content.ReadAsStringAsync();
Debug.LogError($"Master server returned error while trying to register: {str}");
}
}
2021-01-12 22:12:23 +00:00
private async Task UnregisterServer()
{
_updating = false;
2021-01-12 22:12:23 +00:00
if (null == _serverInfo)
return;
2021-01-12 22:11:10 +00:00
2021-01-12 22:12:23 +00:00
if (string.IsNullOrWhiteSpace(_masterServerUrl))
return;
2021-01-12 22:11:10 +00:00
2021-01-12 22:12:23 +00:00
await _client.PostAsync(_masterServerUrl + "/unregister", new StringContent(JsonConvert.SerializeObject(_serverInfo), Encoding.UTF8, "application/json"));
}
2021-01-12 22:12:23 +00:00
public async Task<List<ServerInfo>> GetAllServers()
{
if (string.IsNullOrWhiteSpace(_masterServerUrl))
return new List<ServerInfo>();
return JsonConvert.DeserializeObject<List<ServerInfo>>(await _client.GetStringAsync(_masterServerUrl));
}
2021-01-12 22:11:10 +00:00
2021-01-12 22:12:23 +00:00
public async void OnDestroy()
{
await UnregisterServer();
}
}
2021-01-12 22:12:23 +00:00
public class ServerInfo
{
public string Version { get; set; }
2021-01-12 22:12:23 +00:00
public string Name { get; set; }
public int NumPlayersOnline { get; set; }
public string IP { get; set; }
public int Port { get; set; }
public int MaxPlayers { get; set; }
public string Map { get; set; }
}
}