mirror of
https://github.com/chylex/Minecraft-Phantom-Panel.git
synced 2024-11-25 07:42:58 +01:00
Compare commits
3 Commits
25d0e2b0d1
...
398bb14742
Author | SHA1 | Date | |
---|---|---|---|
398bb14742 | |||
4e89d7b12f | |||
3b313bf0a8 |
@ -3,28 +3,11 @@ using System.Buffers.Binary;
|
|||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Sockets;
|
using System.Net.Sockets;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using Phantom.Utils.Logging;
|
|
||||||
using Serilog;
|
|
||||||
|
|
||||||
namespace Phantom.Agent.Minecraft.Server;
|
namespace Phantom.Agent.Minecraft.Server;
|
||||||
|
|
||||||
public sealed class ServerStatusProtocol {
|
public static class ServerStatusProtocol {
|
||||||
private readonly ILogger logger;
|
public static async Task<int> GetOnlinePlayerCount(ushort serverPort, CancellationToken cancellationToken) {
|
||||||
|
|
||||||
public ServerStatusProtocol(string loggerName) {
|
|
||||||
this.logger = PhantomLogger.Create<ServerStatusProtocol>(loggerName);
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<int?> GetOnlinePlayerCount(int serverPort, CancellationToken cancellationToken) {
|
|
||||||
try {
|
|
||||||
return await GetOnlinePlayerCountOrThrow(serverPort, cancellationToken);
|
|
||||||
} catch (Exception e) {
|
|
||||||
logger.Error(e, "Caught exception checking online player count.");
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private async Task<int?> GetOnlinePlayerCountOrThrow(int serverPort, CancellationToken cancellationToken) {
|
|
||||||
using var tcpClient = new TcpClient();
|
using var tcpClient = new TcpClient();
|
||||||
await tcpClient.ConnectAsync(IPAddress.Loopback, serverPort, cancellationToken);
|
await tcpClient.ConnectAsync(IPAddress.Loopback, serverPort, cancellationToken);
|
||||||
var tcpStream = tcpClient.GetStream();
|
var tcpStream = tcpClient.GetStream();
|
||||||
@ -33,24 +16,22 @@ public sealed class ServerStatusProtocol {
|
|||||||
tcpStream.WriteByte(0xFE);
|
tcpStream.WriteByte(0xFE);
|
||||||
await tcpStream.FlushAsync(cancellationToken);
|
await tcpStream.FlushAsync(cancellationToken);
|
||||||
|
|
||||||
short? messageLength = await ReadStreamHeader(tcpStream, cancellationToken);
|
short messageLength = await ReadStreamHeader(tcpStream, cancellationToken);
|
||||||
return messageLength == null ? null : await ReadOnlinePlayerCount(tcpStream, messageLength.Value * 2, cancellationToken);
|
return await ReadOnlinePlayerCount(tcpStream, messageLength * 2, cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task<short?> ReadStreamHeader(NetworkStream tcpStream, CancellationToken cancellationToken) {
|
private static async Task<short> ReadStreamHeader(NetworkStream tcpStream, CancellationToken cancellationToken) {
|
||||||
var headerBuffer = ArrayPool<byte>.Shared.Rent(3);
|
var headerBuffer = ArrayPool<byte>.Shared.Rent(3);
|
||||||
try {
|
try {
|
||||||
await tcpStream.ReadExactlyAsync(headerBuffer, 0, 3, cancellationToken);
|
await tcpStream.ReadExactlyAsync(headerBuffer, 0, 3, cancellationToken);
|
||||||
|
|
||||||
if (headerBuffer[0] != 0xFF) {
|
if (headerBuffer[0] != 0xFF) {
|
||||||
logger.Error("Unexpected first byte in response from server: {FirstByte}.", headerBuffer[0]);
|
throw new ProtocolException("Unexpected first byte in response from server: " + headerBuffer[0]);
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
short messageLength = BinaryPrimitives.ReadInt16BigEndian(headerBuffer.AsSpan(1));
|
short messageLength = BinaryPrimitives.ReadInt16BigEndian(headerBuffer.AsSpan(1));
|
||||||
if (messageLength <= 0) {
|
if (messageLength <= 0) {
|
||||||
logger.Error("Unexpected message length in response from server: {MessageLength}.", messageLength);
|
throw new ProtocolException("Unexpected message length in response from server: " + messageLength);
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return messageLength;
|
return messageLength;
|
||||||
@ -59,7 +40,7 @@ public sealed class ServerStatusProtocol {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task<int?> ReadOnlinePlayerCount(NetworkStream tcpStream, int messageLength, CancellationToken cancellationToken) {
|
private static async Task<int> ReadOnlinePlayerCount(NetworkStream tcpStream, int messageLength, CancellationToken cancellationToken) {
|
||||||
var messageBuffer = ArrayPool<byte>.Shared.Rent(messageLength);
|
var messageBuffer = ArrayPool<byte>.Shared.Rent(messageLength);
|
||||||
try {
|
try {
|
||||||
await tcpStream.ReadExactlyAsync(messageBuffer, 0, messageLength, cancellationToken);
|
await tcpStream.ReadExactlyAsync(messageBuffer, 0, messageLength, cancellationToken);
|
||||||
@ -74,20 +55,21 @@ public sealed class ServerStatusProtocol {
|
|||||||
int separator2 = Array.LastIndexOf(messageBuffer, SeparatorSecondByte);
|
int separator2 = Array.LastIndexOf(messageBuffer, SeparatorSecondByte);
|
||||||
int separator1 = separator2 == -1 ? -1 : Array.LastIndexOf(messageBuffer, SeparatorSecondByte, separator2 - 1);
|
int separator1 = separator2 == -1 ? -1 : Array.LastIndexOf(messageBuffer, SeparatorSecondByte, separator2 - 1);
|
||||||
if (!IsValidSeparator(messageBuffer, separator1) || !IsValidSeparator(messageBuffer, separator2)) {
|
if (!IsValidSeparator(messageBuffer, separator1) || !IsValidSeparator(messageBuffer, separator2)) {
|
||||||
logger.Error("Could not find message separators in response from server.");
|
throw new ProtocolException("Could not find message separators in response from server.");
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
string onlinePlayerCountStr = Encoding.BigEndianUnicode.GetString(messageBuffer.AsSpan((separator1 + 1)..(separator2 - 1)));
|
string onlinePlayerCountStr = Encoding.BigEndianUnicode.GetString(messageBuffer.AsSpan((separator1 + 1)..(separator2 - 1)));
|
||||||
if (!int.TryParse(onlinePlayerCountStr, out int onlinePlayerCount)) {
|
if (!int.TryParse(onlinePlayerCountStr, out int onlinePlayerCount)) {
|
||||||
logger.Error("Could not parse online player count in response from server: {OnlinePlayerCount}.", onlinePlayerCountStr);
|
throw new ProtocolException("Could not parse online player count in response from server: " + onlinePlayerCountStr);
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Debug("Detected {OnlinePlayerCount} online player(s).", onlinePlayerCount);
|
|
||||||
return onlinePlayerCount;
|
return onlinePlayerCount;
|
||||||
} finally {
|
} finally {
|
||||||
ArrayPool<byte>.Shared.Return(messageBuffer);
|
ArrayPool<byte>.Shared.Return(messageBuffer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public sealed class ProtocolException : Exception {
|
||||||
|
internal ProtocolException(string message) : base(message) {}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,8 +10,6 @@ sealed class InstancePlayerCountTracker : CancellableBackgroundTask {
|
|||||||
private readonly InstanceProcess process;
|
private readonly InstanceProcess process;
|
||||||
private readonly ushort serverPort;
|
private readonly ushort serverPort;
|
||||||
|
|
||||||
private readonly ServerStatusProtocol serverStatusProtocol;
|
|
||||||
|
|
||||||
private readonly TaskCompletionSource firstDetection = AsyncTasks.CreateCompletionSource();
|
private readonly TaskCompletionSource firstDetection = AsyncTasks.CreateCompletionSource();
|
||||||
private readonly ManualResetEventSlim serverOutputEvent = new ();
|
private readonly ManualResetEventSlim serverOutputEvent = new ();
|
||||||
|
|
||||||
@ -45,7 +43,6 @@ sealed class InstancePlayerCountTracker : CancellableBackgroundTask {
|
|||||||
public InstancePlayerCountTracker(InstanceContext context, InstanceProcess process, ushort serverPort) : base(PhantomLogger.Create<InstancePlayerCountTracker>(context.ShortName)) {
|
public InstancePlayerCountTracker(InstanceContext context, InstanceProcess process, ushort serverPort) : base(PhantomLogger.Create<InstancePlayerCountTracker>(context.ShortName)) {
|
||||||
this.process = process;
|
this.process = process;
|
||||||
this.serverPort = serverPort;
|
this.serverPort = serverPort;
|
||||||
this.serverStatusProtocol = new ServerStatusProtocol(context.ShortName);
|
|
||||||
Start();
|
Start();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -59,7 +56,7 @@ sealed class InstancePlayerCountTracker : CancellableBackgroundTask {
|
|||||||
while (!CancellationToken.IsCancellationRequested) {
|
while (!CancellationToken.IsCancellationRequested) {
|
||||||
serverOutputEvent.Reset();
|
serverOutputEvent.Reset();
|
||||||
|
|
||||||
OnlinePlayerCount = await serverStatusProtocol.GetOnlinePlayerCount(serverPort, CancellationToken);
|
OnlinePlayerCount = await TryGetOnlinePlayerCount();
|
||||||
|
|
||||||
if (!firstDetection.Task.IsCompleted) {
|
if (!firstDetection.Task.IsCompleted) {
|
||||||
firstDetection.SetResult();
|
firstDetection.SetResult();
|
||||||
@ -67,7 +64,21 @@ sealed class InstancePlayerCountTracker : CancellableBackgroundTask {
|
|||||||
|
|
||||||
await Task.Delay(TimeSpan.FromSeconds(10), CancellationToken);
|
await Task.Delay(TimeSpan.FromSeconds(10), CancellationToken);
|
||||||
await serverOutputEvent.WaitHandle.WaitOneAsync(CancellationToken);
|
await serverOutputEvent.WaitHandle.WaitOneAsync(CancellationToken);
|
||||||
await Task.Delay(TimeSpan.FromSeconds(2), CancellationToken);
|
await Task.Delay(TimeSpan.FromSeconds(1), CancellationToken);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task<int?> TryGetOnlinePlayerCount() {
|
||||||
|
try {
|
||||||
|
int newOnlinePlayerCount = await ServerStatusProtocol.GetOnlinePlayerCount(serverPort, CancellationToken);
|
||||||
|
Logger.Debug("Detected {OnlinePlayerCount} online player(s).", newOnlinePlayerCount);
|
||||||
|
return newOnlinePlayerCount;
|
||||||
|
} catch (ServerStatusProtocol.ProtocolException e) {
|
||||||
|
Logger.Error(e.Message);
|
||||||
|
return null;
|
||||||
|
} catch (Exception e) {
|
||||||
|
Logger.Error(e, "Caught exception while checking online player count.");
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user