SanAndreasUnity/Assets/Scripts/Networking/MasterServerClient.cs

126 lines
3.6 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;
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-01-27 00:19:28 +00:00
_masterServerUrl = Config.Get<string>("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;
2021-01-12 22:12:23 +00:00
_serverInfo = new ServerInfo
{
Name = Config.Get<string>("server_name"),
Port = NetManager.listenPortNumber,
NumPlayersOnline = NetManager.numConnections,
MaxPlayers = NetManager.maxNumPlayers,
};
2021-01-12 22:12:23 +00:00
await RegisterServer();
}
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.NumPlayersOnline = NetManager.numConnections;
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
{
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; }
}
}