using System.Collections.Concurrent; using System.Net; using System.Net.Sockets; using System.Net.WebSockets; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Logging; using Server.Redis; using GameServices.Auth; using StackExchange.Redis; namespace Server.Networking; /// /// Manages all network connections (WebSocket and TCP), tick loop, and keepalive/ping system. /// Now includes automatic ping mechanism to prevent idle timeout disconnections. /// public sealed class NetworkManager { private readonly ILogger _log; private readonly MessageRouter _router; private readonly ConcurrentDictionary _connections = new(); private readonly ISessionRegistry _sessions; private readonly IRedisManager _redis; private readonly ISessionAudit _sessionAudit; private readonly TimeSpan _tickInterval; private readonly TimeSpan _idleTimeout; private readonly TimeSpan _pingInterval; private readonly int _maxConnections; public NetworkManager( ILogger log, MessageRouter router, ISessionRegistry sessions, IRedisManager redis, ISessionAudit sessionAudit) { _log = log; _router = router; _sessions = sessions; _redis = redis; _sessionAudit = sessionAudit; // Parse tick rate from environment variable (default: 20 Hz) var tickHz = int.TryParse(Environment.GetEnvironmentVariable("TICK_RATE_HZ"), out var hz) ? Math.Clamp(hz, 1, 120) : 20; _tickInterval = TimeSpan.FromMilliseconds(1000.0 / tickHz); // Parse idle timeout from environment variable (default: 90 seconds to allow for ping delays) _idleTimeout = TimeSpan.FromSeconds( int.TryParse(Environment.GetEnvironmentVariable("IDLE_TIMEOUT_S"), out var idle) ? Math.Clamp(idle, 10, 300) : 90 ); // Ping interval should be less than half of idle timeout to ensure keepalive // Default: 30 seconds (gives 3 attempts before 90 second timeout) _pingInterval = TimeSpan.FromSeconds( int.TryParse(Environment.GetEnvironmentVariable("PING_INTERVAL_S"), out var ping) ? Math.Clamp(ping, 5, 60) : 30 ); // Parse max connections from environment variable (default: 10,000) _maxConnections = int.TryParse(Environment.GetEnvironmentVariable("MAX_CONN"), out var mc) ? Math.Max(mc, 100) : 10_000; _log.LogInformation( "NetworkManager initialized: Tick={Hz}Hz, IdleTimeout={Idle}s, PingInterval={Ping}s, MaxConn={Max}", tickHz, _idleTimeout.TotalSeconds, _pingInterval.TotalSeconds, _maxConnections ); } /// /// Accept and handle a WebSocket connection from a client. /// Implements ping/pong keepalive mechanism to prevent idle timeouts. /// public async Task AcceptWebSocketAsync(HttpContext ctx, WebSocket ws) { // Check if server is at capacity if (_connections.Count >= _maxConnections) { await ws.CloseAsync(WebSocketCloseStatus.PolicyViolation, "server full", CancellationToken.None); return; } // Create connection wrapper var conn = new WebSocketConnection( ws, ctx.Connection.RemoteIpAddress == null ? null : new IPEndPoint(ctx.Connection.RemoteIpAddress, ctx.Connection.RemotePort) ); _connections[conn.Id] = conn; _log.LogInformation("WebSocket connected: ConnectionId={Id}, RemoteEndPoint={Remote}", conn.Id, conn.RemoteEndPoint); // Start ping sender task using var pingCancellationTokenSource = new CancellationTokenSource(); var pingTask = SendPeriodicPingsAsync(conn, pingCancellationTokenSource.Token); try { // Main message receive loop while (conn.IsAlive) { using var idleTimeoutCancellationTokenSource = new CancellationTokenSource(_idleTimeout); var data = await conn.ReceiveAsync(idleTimeoutCancellationTokenSource.Token); if (data is null) { _log.LogDebug("Connection closed by client: ConnectionId={Id}", conn.Id); break; } // Route message to appropriate handler await _router.OnMessageAsync(conn, data.Value, CancellationToken.None); } } catch (OperationCanceledException) { _log.LogWarning("Connection idle timeout: ConnectionId={Id}", conn.Id); } catch (Exception ex) { _log.LogWarning(ex, "WebSocket loop error: ConnectionId={Id}", conn.Id); } finally { // Stop ping task pingCancellationTokenSource.Cancel(); try { await pingTask; } catch { /* ignore cancellation */ } // Clean up connection await CleanupAsync(conn, "websocket closed"); } } /// /// Sends periodic PING messages to keep connection alive and detect disconnections. /// Client should respond with PONG to reset idle timeout. /// private async Task SendPeriodicPingsAsync(IConnection connection, CancellationToken cancellationToken) { try { // Wait before sending first ping to avoid immediate ping on connection await Task.Delay(_pingInterval, cancellationToken); while (!cancellationToken.IsCancellationRequested && connection.IsAlive) { try { // Send PING message (OpCode 2, empty payload) var pingMessage = new byte[5]; pingMessage[0] = 0; // Length high byte pingMessage[1] = 0; pingMessage[2] = 0; pingMessage[3] = 1; // Length low byte (1 byte payload) pingMessage[4] = 2; // OpCode 2 = PING await connection.SendAsync(pingMessage, cancellationToken); _log.LogTrace("Sent PING to ConnectionId={Id}", connection.Id); } catch (Exception ex) { _log.LogDebug(ex, "Failed to send PING to ConnectionId={Id}", connection.Id); break; } // Wait for next ping interval await Task.Delay(_pingInterval, cancellationToken); } } catch (OperationCanceledException) { // Normal cancellation, connection is closing } catch (Exception ex) { _log.LogError(ex, "Unexpected error in ping sender for ConnectionId={Id}", connection.Id); } } /// /// Accept TCP connections in a loop (for legacy TCP client support if needed) /// 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(); } } } /// /// Handle an individual TCP client connection with ping/pong support /// private async Task HandleTcpClientAsync(TcpClient client, CancellationToken ct) { var conn = new TcpConnection(client); _connections[conn.Id] = conn; _log.LogInformation("TCP connected: ConnectionId={Id}, RemoteEndPoint={Remote}", conn.Id, conn.RemoteEndPoint); // Start ping sender task using var pingCancellationTokenSource = new CancellationTokenSource(); var pingTask = SendPeriodicPingsAsync(conn, pingCancellationTokenSource.Token); try { while (conn.IsAlive && !ct.IsCancellationRequested) { using var idleTimeoutCancellationTokenSource = new CancellationTokenSource(_idleTimeout); using var linkedCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource( idleTimeoutCancellationTokenSource.Token, ct ); var data = await conn.ReceiveAsync(linkedCancellationTokenSource.Token); if (data is null) break; await _router.OnMessageAsync(conn, data.Value, ct); } } catch (OperationCanceledException) { _log.LogWarning("TCP connection idle timeout or shutdown: ConnectionId={Id}", conn.Id); } catch (Exception ex) { _log.LogWarning(ex, "TCP loop error: ConnectionId={Id}", conn.Id); } finally { // Stop ping task pingCancellationTokenSource.Cancel(); try { await pingTask; } catch { /* ignore cancellation */ } await CleanupAsync(conn, "tcp closed"); } } /// /// Main server tick loop - runs game simulation, broadcasts state, etc. /// 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(); } } /// /// Called every tick interval to update game state, prune idle connections, etc. /// private Task TickAsync(CancellationToken ct) { var now = DateTime.UtcNow; foreach (var (id, conn) in _connections) { if (now - conn.LastSeenUtc > _idleTimeout + TimeSpan.FromSeconds(5)) { _ = CleanupAsync(conn, "idle prune"); } } return Task.CompletedTask; } /// /// Clean up a connection and remove it from tracking /// private async Task CleanupAsync(IConnection conn, string reason) { if (_connections.TryRemove(conn.Id, out _)) { if (_sessions.TryGet(conn.Id, out var accountId, out var token) && accountId != 0 && !string.IsNullOrEmpty(token)) { try { var onlineKey = $"online:acct:{accountId}"; var sessKey = $"sess:{token}"; const string lua = @" if redis.call('GET', KEYS[1]) == ARGV[1] then redis.call('DEL', KEYS[1]) end redis.call('DEL', KEYS[2]) return 1"; await _redis.Db.ScriptEvaluateAsync(lua, new StackExchange.Redis.RedisKey[] { onlineKey, sessKey }, new StackExchange.Redis.RedisValue[] { token }); // NEW: close the SQL session audit row await _sessionAudit.EndAsync(accountId, token, reason); _sessions.Unbind(conn.Id); _log.LogInformation("Logged out account {AccountId} on disconnect", accountId); } catch (Exception ex) { _log.LogWarning(ex, "Failed logout cleanup for account {AccountId}", accountId); try { await _sessionAudit.EndAsync(accountId, token, reason); } catch { } _sessions.Unbind(conn.Id); } } _log.LogInformation("Disconnected {Id} ({Reason})", conn.Id, reason); try { await conn.CloseAsync(reason); } catch { } try { await conn.DisposeAsync(); } catch { } } } }