Table of Contents

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

TConnection

The 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

object

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

connection TConnection

GetAccountNameByConnection(TConnection, out string)

Gets the account name for a connection.

public bool GetAccountNameByConnection(TConnection connection, out string accountName)

Parameters

connection TConnection

The network connection.

accountName string

The account name if found.

Returns

bool

true if found; otherwise, false.

GetConnectionAccountData(TConnection, out AccountData)

Gets the account data for a connection.

public bool GetConnectionAccountData(TConnection connection, out AccountData accountData)

Parameters

connection TConnection

The network connection.

accountData AccountData

The account data if found.

Returns

bool

true if found; otherwise, false.

GetConnectionByAccountName(string, out TConnection)

Gets the network connection for an account name.

public bool GetConnectionByAccountName(string accountName, out TConnection connection)

Parameters

accountName string

The account name.

connection TConnection

The network connection if found.

Returns

bool

true if found; otherwise, false.

GetConnectionEncryptionData(TConnection, out ConnectionEncryptionData)

Gets the encryption data for a connection.

public bool GetConnectionEncryptionData(TConnection connection, out ConnectionEncryptionData encryptionData)

Parameters

connection TConnection

The network connection.

encryptionData ConnectionEncryptionData

The encryption data if found.

Returns

bool

true if found; otherwise, false.

HasAuthState(TConnection, AuthState)

Checks whether a connection has the specified authentication state.

public bool HasAuthState(TConnection connection, AuthState state)

Parameters

connection TConnection

The network connection.

state AuthState

The auth state to check for.

Returns

bool

true if 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

connection TConnection

The network connection.

Returns

bool

true if auth is in progress (state > Handshake); otherwise, false.

RemoveConnectionAccount(TConnection)

Removes all account mappings for a connection.

public void RemoveConnectionAccount(TConnection connection)

Parameters

connection TConnection

The 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

connection TConnection

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

connection TConnection

The network connection.

publicKey byte[]

The client's X25519 public key (32 bytes).

Returns

bool

true if the entry was added; false if 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

connection TConnection

The network connection.

required AuthState

The expected current auth state.

next AuthState

The new auth state to set.

Returns

bool

true if 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

connection TConnection

The network connection.

required AuthState

The expected current auth state.

next AuthState

The new auth state to set.

onSuccess Func<AccountData, bool>

Optional callback invoked inside the lock. Must return true to confirm.

Returns

bool

true if 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

connection TConnection