Class AccountManager<TConnection>
- Namespace
- FishMMO.Auth.Implementation
- Assembly
- FishMMO-ServerAuth.dll
Thread-safe base manager for account and connection data, including encryption state and the unified AuthState machine. All public methods are synchronized via a shared lock to support concurrent access from network broadcast handlers and async worker threads. Subclass SrpAccountManager<TConnection> or TokenAccountManager<TConnection> for authentication-method-specific behaviour.
public class AccountManager<TConnection> : IAccountManager<TConnection>
Type Parameters
TConnectionThe type representing a network connection.
- Inheritance
-
AccountManager<TConnection>
- Implements
-
IAccountManager<TConnection>
- Derived
-
SrpAccountManager<TConnection>TokenAccountManager<TConnection>
- Inherited Members
Fields
SyncRoot
Synchronization object for all dictionary access. Subclasses must acquire this lock before accessing any protected field.
protected readonly object SyncRoot
Field Value
accountConnections
Reverse lookup: maps each account name back to its connection.
protected readonly Dictionary<string, TConnection> accountConnections
Field Value
- Dictionary<string, TConnection>
connectionAccountData
Maps each connection to its full account data (auth state, access level, and SRP state). AccountData is created at handshake time with Handshake.
protected readonly Dictionary<TConnection, AccountData> connectionAccountData
Field Value
- Dictionary<TConnection, AccountData>
connectionAccounts
Maps each connection to its associated account name.
protected readonly Dictionary<TConnection, string> connectionAccounts
Field Value
- Dictionary<TConnection, string>
connectionEncryptionEntries
Maps each connection to its encryption data (public key, symmetric key, session prefix, and counters).
protected readonly Dictionary<TConnection, ConnectionEncryptionData> connectionEncryptionEntries
Field Value
- Dictionary<TConnection, ConnectionEncryptionData>
unauthenticatedTracker
Tracks unauthenticated connections in arrival order for efficient TTL sweeps. Oldest entries are at the head.
protected readonly ArrivalOrderTracker<TConnection> unauthenticatedTracker
Field Value
- ArrivalOrderTracker<TConnection>
Methods
Clear()
Zeroes all sensitive key material and clears all stored account and connection data.
public void Clear()
ClearAndRemoveEncryptionData_NoLock(TConnection)
Calls Clear() on the connection's encryption data, zeroing AES session keys, then removes the entry from the dictionary. Use when tearing down connection state (disconnect, sweep, failed auth). Must be called inside SyncRoot lock.
protected void ClearAndRemoveEncryptionData_NoLock(TConnection connection)
Parameters
connectionTConnection
GetAccountNameByConnection(TConnection, out string)
Gets the account name for a connection.
public bool GetAccountNameByConnection(TConnection connection, out string accountName)
Parameters
connectionTConnectionThe network connection.
accountNamestringThe account name if found.
Returns
- bool
trueif found; otherwise,false.
GetConnectionAccountData(TConnection, out AccountData)
Gets the account data for a connection.
public bool GetConnectionAccountData(TConnection connection, out AccountData accountData)
Parameters
connectionTConnectionThe network connection.
accountDataAccountDataThe account data if found.
Returns
- bool
trueif found; otherwise,false.
GetConnectionByAccountName(string, out TConnection)
Gets the network connection for an account name.
public bool GetConnectionByAccountName(string accountName, out TConnection connection)
Parameters
accountNamestringThe account name.
connectionTConnectionThe network connection if found.
Returns
- bool
trueif found; otherwise,false.
GetConnectionEncryptionData(TConnection, out ConnectionEncryptionData)
Gets the encryption data for a connection.
public bool GetConnectionEncryptionData(TConnection connection, out ConnectionEncryptionData encryptionData)
Parameters
connectionTConnectionThe network connection.
encryptionDataConnectionEncryptionDataThe encryption data if found.
Returns
- bool
trueif found; otherwise,false.
HasAuthState(TConnection, AuthState)
Checks whether a connection has the specified authentication state.
public bool HasAuthState(TConnection connection, AuthState state)
Parameters
connectionTConnectionThe network connection.
stateAuthStateThe auth state to check for.
Returns
- bool
trueif the connection has exactly the given state; otherwise,false.
IsAuthInProgress(TConnection)
Checks whether a connection has progressed beyond Handshake. Used by handshake handlers to reject repeated handshakes while auth is in progress.
public bool IsAuthInProgress(TConnection connection)
Parameters
connectionTConnectionThe network connection.
Returns
- bool
trueif auth is in progress (state > Handshake); otherwise,false.
RemoveConnectionAccount(TConnection)
Removes all account mappings for a connection.
public void RemoveConnectionAccount(TConnection connection)
Parameters
connectionTConnectionThe network connection.
TrackUnauthenticatedConnection_NoLock(TConnection)
Adds connection to the unauthenticated-state arrival-order tracker
so that stale handshakes can be expired by SweepUnauthenticatedConnections.
Must be called inside SyncRoot lock — callers must ensure they hold the lock.
protected void TrackUnauthenticatedConnection_NoLock(TConnection connection)
Parameters
connectionTConnection
TryAddConnectionEncryptionData(TConnection, byte[])
Registers a connection's X25519 public key and creates initial AccountData with Handshake state.
Directional encryption keys are established later by the handshake handler after
X25519 ECDH key agreement completes.
Uses try-add semantics: if the connection already has encryption data the call
returns false and the existing entry is not modified. This closes the
TOCTOU race where two concurrent handshake packets could both pass the
GetConnectionEncryptionData(TConnection, out ConnectionEncryptionData) idempotency guard before either writes.
public bool TryAddConnectionEncryptionData(TConnection connection, byte[] publicKey)
Parameters
connectionTConnectionThe network connection.
publicKeybyte[]The client's X25519 public key (32 bytes).
Returns
- bool
trueif the entry was added;falseif one already existed.
TryAdvanceAuthState(TConnection, AuthState, AuthState)
Atomically advances the authentication state for a connection (compare-and-swap).
public bool TryAdvanceAuthState(TConnection connection, AuthState required, AuthState next)
Parameters
connectionTConnectionThe network connection.
requiredAuthStateThe expected current auth state.
nextAuthStateThe new auth state to set.
Returns
- bool
trueif the state was advanced; otherwise,false.
TryAdvanceAuthState(TConnection, AuthState, AuthState, Func<AccountData, bool>?)
Atomically advances the authentication state for a connection and invokes a callback. The callback runs inside the lock and must not block or re-enter the AccountManager.
public bool TryAdvanceAuthState(TConnection connection, AuthState required, AuthState next, Func<AccountData, bool>? onSuccess)
Parameters
connectionTConnectionThe network connection.
requiredAuthStateThe expected current auth state.
nextAuthStateThe new auth state to set.
onSuccessFunc<AccountData, bool>Optional callback invoked inside the lock. Must return true to confirm.
Returns
- bool
trueif the state was advanced and the callback (if any) succeeded.
UntrackUnauthenticatedConnection_NoLock(TConnection)
Removes connection from the unauthenticated-state tracker.
Call when the connection advances to an authenticated state (SrpSuccess/Authenticated)
or when it is disconnected, to keep the sweep's scan bound tight.
Must be called inside SyncRoot lock.
protected void UntrackUnauthenticatedConnection_NoLock(TConnection connection)
Parameters
connectionTConnection