From cff5989a8d1aa5979bffee0a196a7af5abac4d69 Mon Sep 17 00:00:00 2001 From: ppc Date: Wed, 4 Sep 2024 10:17:14 +0100 Subject: [PATCH] add instance metrics to server --- AMNet.Server/CardPresenter.cs | 2 +- AMNet.Server/DllMain.cs | 8 ++++++++ AMNet.Server/SerializerContext.cs | 4 +++- AMNet.Server/WebServer.cs | 21 +++++++++++++++++++-- 4 files changed, 31 insertions(+), 4 deletions(-) diff --git a/AMNet.Server/CardPresenter.cs b/AMNet.Server/CardPresenter.cs index 4e1fc41..5221c55 100644 --- a/AMNet.Server/CardPresenter.cs +++ b/AMNet.Server/CardPresenter.cs @@ -7,7 +7,7 @@ namespace AMNet.Server; /// /// Represents a storage location to load/read cards from /// -public class CardPresenter +internal class CardPresenter { private readonly object _cardLock = new(); private StoredCard _currentCard; diff --git a/AMNet.Server/DllMain.cs b/AMNet.Server/DllMain.cs index f3ef0a0..54ae69d 100644 --- a/AMNet.Server/DllMain.cs +++ b/AMNet.Server/DllMain.cs @@ -1,3 +1,4 @@ +using System; using System.IO; using System.Runtime.InteropServices; using System.Text; @@ -13,6 +14,10 @@ public static class DllMain { private const string WebAddress = "http://card.ppc.moe"; + // instance metrics + internal static long LastPollTime = -1; + internal static long ServerStartedAt; + static DllMain() { PInvoke.AllocConsole(); @@ -38,6 +43,7 @@ public static class DllMain cancellationRegistration?.Dispose(); }); + Volatile.Write(ref ServerStartedAt, Environment.TickCount64); App.RunAsync(); return 0; } @@ -45,6 +51,8 @@ public static class DllMain [UnmanagedCallersOnly(EntryPoint = "aime_io_nfc_poll")] public static int NfcPoll(byte unitNo) { + Volatile.Write(ref LastPollTime, Environment.TickCount64); + if (!Config.EnableAimeTxt) { return 1; diff --git a/AMNet.Server/SerializerContext.cs b/AMNet.Server/SerializerContext.cs index e3f64d9..ce14208 100644 --- a/AMNet.Server/SerializerContext.cs +++ b/AMNet.Server/SerializerContext.cs @@ -5,7 +5,9 @@ namespace AMNet.Server; public record SystemState( [property: JsonPropertyName("apiVersion")] int ApiVersion, [property: JsonPropertyName("gameId")] string GameId, - [property: JsonPropertyName("serverName")] string ServerName); + [property: JsonPropertyName("serverName")] string ServerName, + [property: JsonPropertyName("sessionUptime")] long SessionUptime, + [property: JsonPropertyName("timeSinceLastPoll")] long? TimeSinceLastPoll); public record CardReadRequest( [property: JsonPropertyName("cardId")] string MatrixCode); diff --git a/AMNet.Server/WebServer.cs b/AMNet.Server/WebServer.cs index b0d5427..c4dbe85 100644 --- a/AMNet.Server/WebServer.cs +++ b/AMNet.Server/WebServer.cs @@ -1,6 +1,7 @@ using System; using System.Globalization; using System.Text.Json; +using System.Threading; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; @@ -53,12 +54,28 @@ internal static class WebServer var app = builder.Build(); app.UseCors(); - app.MapGet("/amnet/info", () => Results.Ok(new SystemState(ApiVersion, Config.GameId, Config.ServerName))); + + app.MapGet("/amnet/info", ServerInfo); app.MapPost("/amnet/signin", ProcessCard); return app; } - + + private static IResult ServerInfo() + { + var startedAt = Volatile.Read(ref DllMain.ServerStartedAt); + var lastPollAt = Volatile.Read(ref DllMain.LastPollTime); + + var state = new SystemState( + ApiVersion, + Config.GameId, + Config.ServerName, + Environment.TickCount64 - startedAt, + lastPollAt < 0 ? null : Environment.TickCount64 - lastPollAt); + + return Results.Ok(state); + } + private static async Task ProcessCard(HttpContext ctx) { CardReadRequest request;