ping pong bugs

This commit is contained in:
dingo
2026-01-17 14:19:24 -05:00
parent d09355a33e
commit 7bba9adb4c
+65 -13
View File
@@ -2,10 +2,12 @@ using System.Collections.Concurrent;
using System.Net;
using System.Net.Sockets;
using System.Net.WebSockets;
using System.Reflection.Emit;
using GameServices.Auth;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Server.Networking.Messages;
using Server.Redis;
using GameServices.Auth;
using StackExchange.Redis;
namespace Server.Networking;
@@ -22,6 +24,7 @@ public sealed class NetworkManager
private readonly ISessionRegistry _sessions;
private readonly IRedisManager _redis;
private readonly ISessionAudit _sessionAudit;
private readonly IMessageSerializer _serializer;
private readonly TimeSpan _tickInterval;
private readonly TimeSpan _idleTimeout;
@@ -30,13 +33,15 @@ public sealed class NetworkManager
public NetworkManager(
ILogger<NetworkManager> log,
MessageRouter router,
MessageRouter router,
IMessageSerializer serializer,
ISessionRegistry sessions,
IRedisManager redis,
ISessionAudit sessionAudit)
{
_log = log;
_router = router;
_serializer = serializer;
_sessions = sessions;
_redis = redis;
_sessionAudit = sessionAudit;
@@ -159,11 +164,11 @@ public sealed class NetworkManager
// Send PING message (OpCode 2)
// WebSocketConnection.SendAsync will add the 4-byte length prefix automatically
// So we just send the 1-byte opcode
var pingMessage = new byte[1];
pingMessage[0] = 2; // OpCode 2 = PING
var emptyPayload = _serializer.Serialize(new { });
var pingMessage = new ServerMessage(2, emptyPayload);
var wireBytes = _serializer.Serialize(pingMessage);
await connection.SendAsync(wireBytes, cancellationToken);
await connection.SendAsync(pingMessage, cancellationToken);
_log.LogTrace("Sent PING to ConnectionId={Id}", connection.Id);
}
catch (Exception ex)
@@ -311,13 +316,60 @@ public sealed class NetworkManager
{
if (_connections.TryRemove(conn.Id, out _))
{
_log.LogInformation("Connection cleaned up: ConnectionId={Id}, Reason={Reason}", conn.Id, reason);
// Notify router about disconnection for session cleanup
_router.OnDisconnect(conn.Id);
// Close the connection gracefully
await conn.CloseAsync(reason, CancellationToken.None);
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 { }
}
}
//private async Task CleanupAsync(IConnection conn, string reason)
//{
// if (_connections.TryRemove(conn.Id, out _))
// {
// _log.LogInformation("Connection cleaned up: ConnectionId={Id}, Reason={Reason}", conn.Id, reason);
// // Notify router about disconnection for session cleanup
// _router.OnDisconnect(conn.Id);
// // Close the connection gracefully
// await conn.CloseAsync(reason, CancellationToken.None);
// }
//}
}
public class PingRequest
{
public int ping { get; set; } = 1;
}