Files
brass-and-sigil/server/Services/RconManager.cs
T
Matt Sijbers a1331212cb Initial commit: Brass & Sigil monorepo
Self-hosted Minecraft modpack distribution + administration system.

- launcher/  Avalonia 12 desktop client; single-file win-x64 publish.
             Microsoft auth via XboxAuthNet, manifest+SHA-1 mod sync,
             portable install path, sidecar settings.
- server/    brass-sigil-server daemon (.NET 8, linux-x64). Wraps the
             MC subprocess, embedded Kestrel admin panel with cookie
             auth + rate limiting, RCON bridge, scheduled backups,
             BlueMap CLI integration with player markers + skin proxy,
             friend-side whitelist request flow, world wipe with seed
             selection (keep current / random / custom).
- pack/      pack.lock.json (Modrinth + manual CurseForge entries),
             data-only tweak source under tweaks/, build outputs in
             overrides/ (gitignored).
- scripts/   Build-Pack / Build-Tweaks / Update-Pack / Check-Updates
             plus Deploy-Brass.ps1 unified one-shot deploy with
             version-bump pre-flight and daemon-state detection.
2026-05-05 00:19:05 +01:00

69 lines
1.9 KiB
C#

namespace BrassAndSigil.Server.Services;
/// <summary>
/// Thin reconnecting wrapper around <see cref="RconClient"/>. The original
/// single-connection-with-no-retry pattern caches a dead client whenever the
/// initial connect happens before MC has opened the RCON port (which is normal --
/// boot takes ~30 s). This manager lazily connects on first use, retries on
/// failure, and drops the client when a send throws so the next call reconnects.
/// </summary>
public sealed class RconManager : IDisposable
{
private readonly string _host;
private readonly int _port;
private readonly string _password;
private readonly SemaphoreSlim _lock = new(1, 1);
private RconClient? _client;
public RconManager(string host, int port, string password)
{
_host = host;
_port = port;
_password = password;
}
public async Task<string> SendCommandAsync(string command, CancellationToken ct = default)
{
var client = await EnsureConnectedAsync(ct);
try
{
return await client.SendCommandAsync(command, ct);
}
catch
{
await DropAsync();
throw;
}
}
private async Task<RconClient> EnsureConnectedAsync(CancellationToken ct)
{
await _lock.WaitAsync(ct);
try
{
if (_client is { Connected: true }) return _client;
_client?.Dispose();
_client = new RconClient();
await _client.ConnectAsync(_host, _port, _password, ct);
return _client;
}
finally
{
_lock.Release();
}
}
private async Task DropAsync()
{
await _lock.WaitAsync();
try { _client?.Dispose(); _client = null; }
finally { _lock.Release(); }
}
public void Dispose()
{
_client?.Dispose();
_lock.Dispose();
}
}