add instance metrics to server

This commit is contained in:
ppc
2024-09-04 10:17:14 +01:00
parent 20577f76f0
commit cff5989a8d
4 changed files with 31 additions and 4 deletions
+1 -1
View File
@@ -7,7 +7,7 @@ namespace AMNet.Server;
/// <summary>
/// Represents a storage location to load/read cards from
/// </summary>
public class CardPresenter
internal class CardPresenter
{
private readonly object _cardLock = new();
private StoredCard _currentCard;
+8
View File
@@ -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;
+3 -1
View File
@@ -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);
+19 -2
View File
@@ -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;