ping pong bugs
This commit is contained in:
@@ -2,10 +2,12 @@ using System.Collections.Concurrent;
|
|||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Sockets;
|
using System.Net.Sockets;
|
||||||
using System.Net.WebSockets;
|
using System.Net.WebSockets;
|
||||||
|
using System.Reflection.Emit;
|
||||||
|
using GameServices.Auth;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Server.Networking.Messages;
|
||||||
using Server.Redis;
|
using Server.Redis;
|
||||||
using GameServices.Auth;
|
|
||||||
using StackExchange.Redis;
|
using StackExchange.Redis;
|
||||||
|
|
||||||
namespace Server.Networking;
|
namespace Server.Networking;
|
||||||
@@ -22,6 +24,7 @@ public sealed class NetworkManager
|
|||||||
private readonly ISessionRegistry _sessions;
|
private readonly ISessionRegistry _sessions;
|
||||||
private readonly IRedisManager _redis;
|
private readonly IRedisManager _redis;
|
||||||
private readonly ISessionAudit _sessionAudit;
|
private readonly ISessionAudit _sessionAudit;
|
||||||
|
private readonly IMessageSerializer _serializer;
|
||||||
|
|
||||||
private readonly TimeSpan _tickInterval;
|
private readonly TimeSpan _tickInterval;
|
||||||
private readonly TimeSpan _idleTimeout;
|
private readonly TimeSpan _idleTimeout;
|
||||||
@@ -30,13 +33,15 @@ public sealed class NetworkManager
|
|||||||
|
|
||||||
public NetworkManager(
|
public NetworkManager(
|
||||||
ILogger<NetworkManager> log,
|
ILogger<NetworkManager> log,
|
||||||
MessageRouter router,
|
MessageRouter router,
|
||||||
|
IMessageSerializer serializer,
|
||||||
ISessionRegistry sessions,
|
ISessionRegistry sessions,
|
||||||
IRedisManager redis,
|
IRedisManager redis,
|
||||||
ISessionAudit sessionAudit)
|
ISessionAudit sessionAudit)
|
||||||
{
|
{
|
||||||
_log = log;
|
_log = log;
|
||||||
_router = router;
|
_router = router;
|
||||||
|
_serializer = serializer;
|
||||||
_sessions = sessions;
|
_sessions = sessions;
|
||||||
_redis = redis;
|
_redis = redis;
|
||||||
_sessionAudit = sessionAudit;
|
_sessionAudit = sessionAudit;
|
||||||
@@ -159,11 +164,11 @@ public sealed class NetworkManager
|
|||||||
// Send PING message (OpCode 2)
|
// Send PING message (OpCode 2)
|
||||||
// WebSocketConnection.SendAsync will add the 4-byte length prefix automatically
|
// WebSocketConnection.SendAsync will add the 4-byte length prefix automatically
|
||||||
// So we just send the 1-byte opcode
|
// So we just send the 1-byte opcode
|
||||||
var pingMessage = new byte[1];
|
var emptyPayload = _serializer.Serialize(new { });
|
||||||
pingMessage[0] = 2; // OpCode 2 = PING
|
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);
|
_log.LogTrace("Sent PING to ConnectionId={Id}", connection.Id);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
@@ -311,13 +316,60 @@ public sealed class NetworkManager
|
|||||||
{
|
{
|
||||||
if (_connections.TryRemove(conn.Id, out _))
|
if (_connections.TryRemove(conn.Id, out _))
|
||||||
{
|
{
|
||||||
_log.LogInformation("Connection cleaned up: ConnectionId={Id}, Reason={Reason}", conn.Id, reason);
|
if (_sessions.TryGet(conn.Id, out var accountId, out var token)
|
||||||
|
&& accountId != 0 && !string.IsNullOrEmpty(token))
|
||||||
// Notify router about disconnection for session cleanup
|
{
|
||||||
_router.OnDisconnect(conn.Id);
|
try
|
||||||
|
{
|
||||||
// Close the connection gracefully
|
var onlineKey = $"online:acct:{accountId}";
|
||||||
await conn.CloseAsync(reason, CancellationToken.None);
|
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;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user