Networking Baseline

This commit is contained in:
dingo
2025-11-04 14:12:47 -05:00
parent a3a9dff963
commit 5b66567726
9 changed files with 466 additions and 2 deletions
+22
View File
@@ -0,0 +1,22 @@
using System.Net;
namespace Server.Networking;
public interface IConnection : IAsyncDisposable
{
Guid Id { get; }
EndPoint? RemoteEndPoint { get; }
DateTime LastSeenUtc { get; }
// Send a raw payload (already framed/serialized by caller)
ValueTask SendAsync(ReadOnlyMemory<byte> payload, CancellationToken ct = default);
// Receive next **complete** message (length-prefixed by transport adapter)
ValueTask<ReadOnlyMemory<byte>?> ReceiveAsync(CancellationToken ct = default);
// For heartbeats/metrics
bool IsAlive { get; }
// Close with reason
ValueTask CloseAsync(string reason, CancellationToken ct = default);
}
+38
View File
@@ -0,0 +1,38 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Server.Networking.Messages;
namespace Server.Networking;
public sealed class MessageRouter
{
private readonly ILogger<MessageRouter> _logger;
private readonly IMessageSerializer _serializer = new JsonSerializerAdapter();
public MessageRouter(ILogger<MessageRouter> logger) => _logger = logger;
public async Task OnMessageAsync(IConnection connection, ReadOnlyMemory<byte> data, CancellationToken cancellationToken)
{
var clientMessage = _serializer.Deserialize<ClientMessage>(data);
switch (clientMessage.OpCode)
{
case 1: // Hello / Echo
_logger.LogDebug("HELLO from {ConnectionId}", connection.Id);
await SendAsync(connection, new ServerMessage(1, clientMessage.Payload), cancellationToken);
break;
default:
_logger.LogWarning("Unknown opcode {OpCode} from {ConnectionId}", clientMessage.OpCode, connection.Id);
break;
}
}
public Task SendAsync(IConnection connection, ServerMessage message, CancellationToken cancellationToken)
{
var bytes = _serializer.Serialize(message);
return connection.SendAsync(bytes, cancellationToken).AsTask();
}
}
+5
View File
@@ -0,0 +1,5 @@
namespace Server.Networking.Messages;
// Unified, descriptive contracts
public sealed record ClientMessage(int OpCode, byte[] Payload);
public sealed record ServerMessage(int OpCode, byte[] Payload);
@@ -0,0 +1,9 @@
namespace Server.Networking.Messages;
public interface IMessageSerializer
{
byte[] Serialize<T>(T message);
T Deserialize<T>(ReadOnlyMemory<byte> bytes);
}
@@ -0,0 +1,20 @@
using System;
using System.Text.Json;
namespace Server.Networking.Messages;
public sealed class JsonSerializerAdapter : IMessageSerializer
{
private static readonly JsonSerializerOptions Options = new()
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
WriteIndented = false,
IncludeFields = true
};
public byte[] Serialize<T>(T message) =>
JsonSerializer.SerializeToUtf8Bytes(message, Options);
public T Deserialize<T>(ReadOnlyMemory<byte> bytes) =>
JsonSerializer.Deserialize<T>(bytes.Span, Options)!;
}
+144
View File
@@ -0,0 +1,144 @@
using System.Collections.Concurrent;
using System.Net;
using System.Net.Sockets;
using System.Net.WebSockets;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
namespace Server.Networking;
public sealed class NetworkManager
{
private readonly ILogger<NetworkManager> _log;
private readonly MessageRouter _router;
private readonly ConcurrentDictionary<Guid, IConnection> _connections = new();
private readonly TimeSpan _tickInterval;
private readonly TimeSpan _idleTimeout;
private readonly int _maxConnections;
public NetworkManager(ILogger<NetworkManager> log, MessageRouter router)
{
_log = log;
_router = router;
var tickHz = int.TryParse(Environment.GetEnvironmentVariable("TICK_RATE_HZ"), out var hz) ? Math.Clamp(hz, 1, 120) : 20;
_tickInterval = TimeSpan.FromMilliseconds(1000.0 / tickHz);
_idleTimeout = TimeSpan.FromSeconds(int.TryParse(Environment.GetEnvironmentVariable("IDLE_TIMEOUT_S"), out var idle) ? Math.Clamp(idle, 10, 300) : 45);
_maxConnections = int.TryParse(Environment.GetEnvironmentVariable("MAX_CONN"), out var mc) ? Math.Max(mc, 100) : 10_000;
_log.LogInformation("Tick={Hz}Hz IdleTimeout={Idle}s MaxConn={Max}", tickHz, _idleTimeout.TotalSeconds, _maxConnections);
}
public async Task AcceptWebSocketAsync(HttpContext ctx, WebSocket ws)
{
if (_connections.Count >= _maxConnections)
{
await ws.CloseAsync(WebSocketCloseStatus.PolicyViolation, "server full", CancellationToken.None);
return;
}
var conn = new WebSocketConnection(ws, ctx.Connection.RemoteIpAddress == null ? null : new IPEndPoint(ctx.Connection.RemoteIpAddress, ctx.Connection.RemotePort));
_connections[conn.Id] = conn;
_log.LogInformation("WS connected {Id} {Remote}", conn.Id, conn.RemoteEndPoint);
try
{
while (conn.IsAlive)
{
using var cts = new CancellationTokenSource(_idleTimeout);
var data = await conn.ReceiveAsync(cts.Token);
if (data is null) break;
await _router.OnMessageAsync(conn, data.Value, CancellationToken.None);
}
}
catch (OperationCanceledException) { /* idle timeout */ }
catch (Exception ex) { _log.LogWarning(ex, "WS loop error {Id}", conn.Id); }
finally
{
await CleanupAsync(conn, "ws closed");
}
}
public async Task AcceptTcpLoopAsync(TcpListener listener, CancellationToken stopping)
{
while (!stopping.IsCancellationRequested)
{
TcpClient? client = null;
try
{
client = await listener.AcceptTcpClientAsync(stopping);
if (_connections.Count >= _maxConnections) { client.Close(); continue; }
_ = HandleTcpClientAsync(client, stopping);
}
catch (OperationCanceledException) { break; }
catch (Exception ex) { _log.LogError(ex, "TCP accept failed"); client?.Close(); }
}
}
private async Task HandleTcpClientAsync(TcpClient client, CancellationToken ct)
{
var conn = new TcpConnection(client);
_connections[conn.Id] = conn;
_log.LogInformation("TCP connected {Id} {Remote}", conn.Id, conn.RemoteEndPoint);
try
{
while (conn.IsAlive && !ct.IsCancellationRequested)
{
using var idleCts = new CancellationTokenSource(_idleTimeout);
var linked = CancellationTokenSource.CreateLinkedTokenSource(idleCts.Token, ct);
var data = await conn.ReceiveAsync(linked.Token);
if (data is null) break;
await _router.OnMessageAsync(conn, data.Value, ct);
}
}
catch (OperationCanceledException) { /* idle timeout or shutdown */ }
catch (Exception ex) { _log.LogWarning(ex, "TCP loop error {Id}", conn.Id); }
finally
{
await CleanupAsync(conn, "tcp closed");
}
}
public async Task RunTickLoopAsync(CancellationToken stopping)
{
var timer = new PeriodicTimer(_tickInterval);
try
{
while (!stopping.IsCancellationRequested && await timer.WaitForNextTickAsync(stopping))
{
await TickAsync(stopping);
}
}
catch (OperationCanceledException) { /* shutting down */ }
finally { timer.Dispose(); }
}
private Task TickAsync(CancellationToken ct)
{
// Example: prune idle, broadcast heartbeat, step simulation, etc.
var now = DateTime.UtcNow;
foreach (var (id, conn) in _connections)
{
if (now - conn.LastSeenUtc > _idleTimeout + TimeSpan.FromSeconds(5))
{
_ = CleanupAsync(conn, "idle prune");
}
}
// TODO: call your simulation step here
return Task.CompletedTask;
}
private async Task CleanupAsync(IConnection conn, string reason)
{
if (_connections.TryRemove(conn.Id, out _))
{
_log.LogInformation("Disconnected {Id} ({Reason})", conn.Id, reason);
await conn.CloseAsync(reason);
await conn.DisposeAsync();
}
}
}
+80
View File
@@ -0,0 +1,80 @@
using System;
using System.Buffers;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
namespace Server.Networking;
public sealed class TcpConnection : IConnection
{
private readonly TcpClient _tcpClient;
private readonly NetworkStream _networkStream;
private volatile bool _isClosed;
public Guid Id { get; } = Guid.NewGuid();
public EndPoint? RemoteEndPoint => _tcpClient.Client.RemoteEndPoint;
public DateTime LastSeenUtc { get; private set; } = DateTime.UtcNow;
public bool IsAlive => !_isClosed && _tcpClient.Connected;
public TcpConnection(TcpClient tcpClient)
{
_tcpClient = tcpClient;
_tcpClient.NoDelay = true;
_networkStream = tcpClient.GetStream();
}
public async ValueTask<ReadOnlyMemory<byte>?> ReceiveAsync(CancellationToken cancellationToken = default)
{
var headerBuffer = new byte[4];
// Await the framework ReadExactlyAsync (no return value)
await _networkStream.ReadExactlyAsync(headerBuffer.AsMemory(0, 4), cancellationToken);
int payloadLength =
(headerBuffer[0] << 24) |
(headerBuffer[1] << 16) |
(headerBuffer[2] << 8) |
headerBuffer[3];
if (payloadLength <= 0 || payloadLength > 4 * 1024 * 1024)
throw new InvalidOperationException("Invalid frame length.");
byte[] payloadBuffer = ArrayPool<byte>.Shared.Rent(payloadLength);
await _networkStream.ReadExactlyAsync(payloadBuffer.AsMemory(0, payloadLength), cancellationToken);
LastSeenUtc = DateTime.UtcNow;
return new ReadOnlyMemory<byte>(payloadBuffer, 0, payloadLength);
}
public async ValueTask SendAsync(ReadOnlyMemory<byte> payload, CancellationToken cancellationToken = default)
{
int length = payload.Length;
var header = new byte[4];
header[0] = (byte)((length >> 24) & 0xFF);
header[1] = (byte)((length >> 16) & 0xFF);
header[2] = (byte)((length >> 8) & 0xFF);
header[3] = (byte)(length & 0xFF);
await _networkStream.WriteAsync(header, cancellationToken);
await _networkStream.WriteAsync(payload, cancellationToken);
await _networkStream.FlushAsync(cancellationToken);
}
public ValueTask CloseAsync(string reason, CancellationToken cancellationToken = default)
{
_isClosed = true;
try { _tcpClient.Close(); } catch { }
return ValueTask.CompletedTask;
}
public ValueTask DisposeAsync()
{
_tcpClient.Dispose();
return ValueTask.CompletedTask;
}
}
+113
View File
@@ -0,0 +1,113 @@
using System.Buffers;
using System.Net;
using System.Net.WebSockets;
using System.Threading.Channels;
namespace Server.Networking;
public sealed class WebSocketConnection : IConnection
{
private readonly WebSocket _webSocket;
private readonly EndPoint? _remoteEndPoint;
private readonly Channel<byte[]> _outboundQueue = Channel.CreateBounded<byte[]>(new BoundedChannelOptions(256)
{
SingleReader = true,
SingleWriter = false,
FullMode = BoundedChannelFullMode.DropOldest
});
private readonly Task _senderLoopTask;
private volatile bool _isClosed;
public Guid Id { get; } = Guid.NewGuid();
public EndPoint? RemoteEndPoint => _remoteEndPoint;
public DateTime LastSeenUtc { get; private set; } = DateTime.UtcNow;
public bool IsAlive => !_isClosed && _webSocket.State == WebSocketState.Open;
public WebSocketConnection(WebSocket webSocket, EndPoint? remoteEndPoint)
{
_webSocket = webSocket;
_remoteEndPoint = remoteEndPoint;
_senderLoopTask = Task.Run(SenderLoopAsync);
}
public async ValueTask<ReadOnlyMemory<byte>?> ReceiveAsync(CancellationToken cancellationToken = default)
{
// Read exactly 4 bytes for the big-endian length header (could be fragmented)
var header = new byte[4];
int headerFilled = 0;
while (headerFilled < 4)
{
var result = await _webSocket.ReceiveAsync(new ArraySegment<byte>(header, headerFilled, 4 - headerFilled), cancellationToken);
if (result.MessageType == WebSocketMessageType.Close || result.Count == 0) return null;
headerFilled += result.Count;
}
int payloadLength =
(header[0] << 24) |
(header[1] << 16) |
(header[2] << 8) |
header[3];
if (payloadLength <= 0 || payloadLength > 4 * 1024 * 1024)
throw new InvalidOperationException("Invalid frame length.");
byte[] payloadBuffer = ArrayPool<byte>.Shared.Rent(payloadLength);
int filled = 0;
while (filled < payloadLength)
{
var segment = new ArraySegment<byte>(payloadBuffer, filled, payloadLength - filled);
var result = await _webSocket.ReceiveAsync(segment, cancellationToken);
if (result.MessageType == WebSocketMessageType.Close) { ArrayPool<byte>.Shared.Return(payloadBuffer); return null; }
if (result.Count == 0) continue;
filled += result.Count;
}
LastSeenUtc = DateTime.UtcNow;
return new ReadOnlyMemory<byte>(payloadBuffer, 0, payloadLength);
}
public async ValueTask SendAsync(ReadOnlyMemory<byte> payload, CancellationToken cancellationToken = default)
{
int length = payload.Length;
var framed = new byte[length + 4];
framed[0] = (byte)((length >> 24) & 0xFF);
framed[1] = (byte)((length >> 16) & 0xFF);
framed[2] = (byte)((length >> 8) & 0xFF);
framed[3] = (byte)(length & 0xFF);
payload.CopyTo(framed.AsMemory(4));
await _outboundQueue.Writer.WriteAsync(framed, cancellationToken);
}
private async Task SenderLoopAsync()
{
try
{
while (await _outboundQueue.Reader.WaitToReadAsync())
{
while (_outboundQueue.Reader.TryRead(out var framed))
{
await _webSocket.SendAsync(framed, WebSocketMessageType.Binary, endOfMessage: true, CancellationToken.None);
}
}
}
catch { /* optional logging */ }
}
public async ValueTask CloseAsync(string reason, CancellationToken cancellationToken = default)
{
if (_isClosed) return;
_isClosed = true;
try { await _webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, reason, cancellationToken); }
catch { /* ignore */ }
}
public async ValueTask DisposeAsync()
{
await CloseAsync("dispose");
_webSocket.Dispose();
}
}
+35 -2
View File
@@ -1,6 +1,39 @@
using System.Net;
using System.Net.Sockets;
using Server.Networking;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSingleton<MessageRouter>();
builder.Services.AddSingleton<NetworkManager>();
var app = builder.Build();
app.UseWebSockets(new WebSocketOptions
{
KeepAliveInterval = TimeSpan.FromSeconds(20),
ReceiveBufferSize = 64 * 1024
});
app.MapGet("/", () => "SystemRevival Server is up!");
app.Map("/ws", async (HttpContext httpContext, NetworkManager networkManager) =>
{
if (!httpContext.WebSockets.IsWebSocketRequest)
{
httpContext.Response.StatusCode = StatusCodes.Status400BadRequest;
return;
}
app.Run();
var acceptedWebSocket = await httpContext.WebSockets.AcceptWebSocketAsync();
await networkManager.AcceptWebSocketAsync(httpContext, acceptedWebSocket);
});
// Start TCP listener
int tcpPort = int.TryParse(Environment.GetEnvironmentVariable("TCP_PORT"), out var parsed) ? parsed : 40000;
var tcpListener = new TcpListener(IPAddress.Any, tcpPort);
tcpListener.Start();
app.Logger.LogInformation("TCP listening on {Port}", tcpPort);
var networkManagerInstance = app.Services.GetRequiredService<NetworkManager>();
_ = networkManagerInstance.AcceptTcpLoopAsync(tcpListener, app.Lifetime.ApplicationStopping);
// Tick loop
_ = networkManagerInstance.RunTickLoopAsync(app.Lifetime.ApplicationStopping);
await app.RunAsync();