Table of Contents

Class PendingAuthTracker<TConnection>

Namespace
FishMMO.Auth.Core.Collections
Assembly
FishMMO-ServerAuth.dll

Connections that have completed their handshake and not yet authenticated, each in a PendingAuthPhase, with expiry swept head-first in the order they fall due.

public sealed class PendingAuthTracker<TConnection>

Type Parameters

TConnection

The connection type.

Inheritance
PendingAuthTracker<TConnection>
Inherited Members

Remarks

Two lists, each in deadline order. An authenticating entry falls due ProgressTtlSeconds after its last progress, the same TTL for every entry, so the order of last progress is the order of expiry; a progress report moves its entry to the tail. An entry awaiting its second factor falls due one window after its prompt, again the same window for every entry, so the order of prompts is the order of expiry; a prompt moves its entry to the tail. The two durations differ, so the two kinds cannot share one list and stay ordered — they did share one, as a single TTL, which is why the two-factor prompt had fifteen seconds. SweepOverdue(double, int, List<(TConnection Connection, PendingAuthPhase Phase)>) reads only the two heads and stops at the first entry of each that is still in time: O(removed + 1) whatever the number pending.

The window is applied when the sweep reads it, not stamped on each entry, so changing TwoFactorWindowSeconds moves every prompt's deadline together and the list stays in order.

Time is monotonic seconds, passed in by the caller: on the wall clock a step backwards stopped every entry from falling due until the clock caught up and the pending cap filled with connections nobody would purge. Stamps never go backwards either — a caller that read the clock just before another thread did can reach the lock second, and its stamp is raised to the latest one so the lists cannot fall out of order.

Thread-safe. Every operation takes one short lock and calls nothing outside this class, so the lock can be taken under no other and never waits on disconnect or database work. Entries are keyed by client ID; operations that act for a specific connection check it is the same connection, so a late call for a connection whose ID was recycled cannot touch its successor.

Constructors

PendingAuthTracker(double, double, double)

Initializes an empty tracker.

public PendingAuthTracker(double progressTtlSeconds, double authenticatingCapSeconds, double twoFactorWindowSeconds)

Parameters

progressTtlSeconds double

See ProgressTtlSeconds. Must be positive.

authenticatingCapSeconds double

See AuthenticatingCapSeconds. Must be positive.

twoFactorWindowSeconds double

Seconds a player has to answer one two-factor prompt. Must be positive.

Properties

Count

Connections currently pending, in either phase.

public int Count { get; }

Property Value

int

TwoFactorWindowSeconds

Seconds a player has to answer one two-factor prompt. Takes effect for every prompt at once, including those already on screen.

public double TwoFactorWindowSeconds { get; set; }

Property Value

double

Methods

BeginAwaitingTwoFactor(int, TConnection, double)

Puts a connection on a two-factor prompt: AwaitingTwoFactor with a full window starting now.

public bool BeginAwaitingTwoFactor(int clientId, TConnection connection, double nowSeconds)

Parameters

clientId int

The connection's client ID.

connection TConnection

The connection being prompted.

nowSeconds double

Current monotonic time in seconds.

Returns

bool

false when the connection is not tracked (it is being dropped).

Remarks

Called for every prompt, the first and each re-prompt after a counted wrong code, so each one gets the whole window.

Clear()

Stops tracking everything.

public void Clear()

Remove(int)

Stops tracking a client ID, whichever connection holds it.

public bool Remove(int clientId)

Parameters

clientId int

Returns

bool

true when it was tracked.

Remove(int, TConnection)

Stops tracking connection, leaving a successor on a recycled ID alone.

public bool Remove(int clientId, TConnection connection)

Parameters

clientId int
connection TConnection

Returns

bool

true when it was tracked.

ReportProgress(int, double)

Records progress on an authenticating connection, restarting its progress TTL.

public bool ReportProgress(int clientId, double nowSeconds)

Parameters

clientId int

The connection's client ID.

nowSeconds double

Current monotonic time in seconds.

Returns

bool

true when the TTL was restarted.

Remarks

Ignored for a connection that is not tracked, that has been authenticating for AuthenticatingCapSeconds, or that is awaiting its second factor (see MayExtend(PendingAuthPhase, double, double, double)).

ResumeAuthenticating(int, TConnection, double)

Starts a fresh Authenticating phase for a connection, as when the player's two-factor code arrives and the servers begin checking it.

public bool ResumeAuthenticating(int clientId, TConnection connection, double nowSeconds)

Parameters

clientId int

The connection's client ID.

connection TConnection

The connection.

nowSeconds double

Current monotonic time in seconds.

Returns

bool

false when the connection is not tracked.

SweepOverdue(double, int, List<(TConnection Connection, PendingAuthPhase Phase)>)

Takes overdue connections out of tracking, oldest first, stopping at the first entry of each phase that is still in time.

public int SweepOverdue(double nowSeconds, int maxRemovals, List<(TConnection Connection, PendingAuthPhase Phase)> removed)

Parameters

nowSeconds double

Current monotonic time in seconds.

maxRemovals int

Most connections to take in this call; the rest stay at the heads.

removed List<(TConnection Connection, PendingAuthPhase Phase)>

Receives the connections taken and the phase each was in, for the caller to answer, purge and disconnect outside the lock.

Returns

int

Connections taken.

Remarks

Each connection comes back with the phase it ran out of time in, because the two mean different things to the player. A stalled exchange is dropped without a word, as any other stall is; an unanswered two-factor prompt is a person who was asked for something and is owed an answer saying the question has closed (TwoFactorExpired).

TryGetPhase(int, TConnection, out PendingAuthPhase)

The phase a connection is in, when it is pending.

public bool TryGetPhase(int clientId, TConnection connection, out PendingAuthPhase phase)

Parameters

clientId int

The connection's client ID.

connection TConnection

The connection; an entry for a different one under the same ID does not count.

phase PendingAuthPhase

Its phase, when tracked.

Returns

bool

true when connection is pending.

TryStart(int, TConnection, double, int)

Starts tracking a connection whose handshake has just completed, in Authenticating.

public bool TryStart(int clientId, TConnection connection, double nowSeconds, int capacity)

Parameters

clientId int

The connection's client ID.

connection TConnection

The connection.

nowSeconds double

Current monotonic time in seconds.

capacity int

Most connections that may be authenticating at once (see AdmitsNewPending(int, int, int)).

Returns

bool

false when the cap or the ceiling is reached.

Remarks

A client ID already tracked is restarted from scratch with the new connection: a fresh handshake on it already holds a slot, so the capacity does not apply.