44 lines
1.5 KiB
C#
44 lines
1.5 KiB
C#
using System;
|
|
using System.Collections.Concurrent;
|
|
|
|
namespace GameServices.Auth;
|
|
|
|
public sealed class SessionRegistry : ISessionRegistry
|
|
{
|
|
private readonly ConcurrentDictionary<Guid, (ulong accountId, string token)> _byConn = new();
|
|
private readonly ConcurrentDictionary<Guid, ulong> _charByConn = new();
|
|
|
|
public void Bind(Guid connectionId, ulong accountId, string token)
|
|
=> _byConn[connectionId] = (accountId, token);
|
|
|
|
public void Unbind(Guid connectionId)
|
|
{
|
|
_byConn.TryRemove(connectionId, out _);
|
|
_charByConn.TryRemove(connectionId, out _);
|
|
}
|
|
|
|
public bool IsAuthenticated(Guid connectionId)
|
|
=> _byConn.ContainsKey(connectionId);
|
|
|
|
public bool TryGetAccountId(Guid connectionId, out ulong accountId)
|
|
{
|
|
if (_byConn.TryGetValue(connectionId, out var v)) { accountId = v.accountId; return true; }
|
|
accountId = 0; return false;
|
|
}
|
|
|
|
public bool TryGet(Guid connectionId, out ulong accountId, out string token)
|
|
{
|
|
if (_byConn.TryGetValue(connectionId, out var v))
|
|
{ accountId = v.accountId; token = v.token; return true; }
|
|
accountId = 0; token = string.Empty; return false;
|
|
}
|
|
public void BindCharacter(Guid connectionId, ulong characterId)
|
|
=> _charByConn[connectionId] = characterId;
|
|
|
|
public void UnbindCharacter(Guid connectionId)
|
|
=> _charByConn.TryRemove(connectionId, out _);
|
|
|
|
public bool TryGetCharacter(Guid connectionId, out ulong characterId)
|
|
=> _charByConn.TryGetValue(connectionId, out characterId);
|
|
}
|