1
0
mirror of https://github.com/chylex/Minecraft-Phantom-Panel.git synced 2026-04-14 22:44:08 +02:00
Files
Minecraft-Phantom-Panel/Agent/Phantom.Agent.Services/Games/MinecraftServerPlayerCountDetector.cs
2026-04-06 09:33:01 +02:00

33 lines
1.3 KiB
C#

using System.Net.Sockets;
using Phantom.Agent.Services.Instances;
using Phantom.Common.Data.Agent.Instance.Backups;
using Phantom.Common.Data.Instance;
using Phantom.Utils.Logging;
using Serilog;
namespace Phantom.Agent.Services.Games;
sealed class MinecraftServerPlayerCountDetector(InstanceContext instanceContext, ushort serverPort) : IInstancePlayerCountDetector {
private readonly ILogger logger = PhantomLogger.Create<MinecraftServerPlayerCountDetector>(instanceContext.ShortName);
private bool waitingForFirstDetection = true;
public async Task<InstancePlayerCounts?> TryGetPlayerCounts(CancellationToken cancellationToken) {
try {
var playerCounts = await MinecraftServerStatusProtocol.GetPlayerCounts(serverPort, cancellationToken);
waitingForFirstDetection = false;
return playerCounts;
} catch (MinecraftServerStatusProtocol.ProtocolException e) {
logger.Error("{Message}", e.Message);
return null;
} catch (SocketException e) {
bool waitingForServerStart = e.SocketErrorCode == SocketError.ConnectionRefused && waitingForFirstDetection;
if (!waitingForServerStart) {
logger.Warning("Could not check online player count. Socket error {ErrorCode} ({ErrorCodeName}), reason: {ErrorMessage}", e.ErrorCode, e.SocketErrorCode, e.Message);
}
return null;
}
}
}