## Live WebSocket connection registry (DESIGN 4.3, 6.5; issue #17). ## Caps FIXED (owner-delegated, #15): 4 connections per player (a 5th evicts that player's oldest), ## 16 total (the 17th is refused before the upgrade). Connections are kept in activation order, which ## is the eviction order. Keyed by an app connId, not by WebSocket fields (H19). ## One lock, held only for these O(16) list operations. The caller closes the tickets returned. import std/[locks, options] import mummy import auth type Conn* = object id*: int playerId*: int role*: PlayerRole ws*: WebSocket Conns* = ref object lock: Lock nextId: int pending: int ## upgrades in flight, reserved against the total cap maxPerPlayer: int maxTotal: int active: seq[Conn] ## activation order; active[0] is the oldest of the whole server Activation* = object id*: int evictedId*: Option[int] evicted*: Option[WebSocket] const MaxConnsPerPlayer* = 4 MaxConnsTotal* = 16 proc newConns*(maxPerPlayer = MaxConnsPerPlayer, maxTotal = MaxConnsTotal): Conns = result = Conns(maxPerPlayer: maxPerPlayer, maxTotal: maxTotal) initLock(result.lock) proc reserve*(c: Conns): bool = ## Before the upgrade: false means the 17th connection - answer HTTP 503, no upgrade. withLock c.lock: result = c.active.len + c.pending < c.maxTotal if result: inc c.pending proc cancelReserve*(c: Conns) = withLock c.lock: if c.pending > 0: dec c.pending proc playerConnIndices(c: Conns, playerId: int): seq[int] = for i, conn in c.active: if conn.playerId == playerId: result.add i proc activate*(c: Conns, playerId: int, role: PlayerRole, ws: WebSocket): Activation = ## After a successful upgrade. A 5th connection of one player evicts that player's oldest. withLock c.lock: if c.pending > 0: dec c.pending let mine = c.playerConnIndices(playerId) if mine.len >= c.maxPerPlayer: let oldest = mine[0] result.evictedId = some(c.active[oldest].id) result.evicted = some(c.active[oldest].ws) c.active.delete(oldest) inc c.nextId result.id = c.nextId c.active.add Conn(id: c.nextId, playerId: playerId, role: role, ws: ws) proc remove*(c: Conns, connId: int) = withLock c.lock: for i, conn in c.active: if conn.id == connId: c.active.delete(i) return proc connById*(c: Conns, connId: int): Option[Conn] = withLock c.lock: for conn in c.active: if conn.id == connId: return some(conn) proc connForWs*(c: Conns, ws: WebSocket): Option[Conn] = ## Mummy hands the same WebSocket value back on MessageEvent/CloseEvent (H19 layout check). withLock c.lock: for conn in c.active: if conn.ws == ws: return some(conn) proc revokePlayer*(c: Conns, playerId: int): seq[WebSocket] = ## key.replace / player.remove / GM key rotation: every live connection of that player. withLock c.lock: var i = 0 while i < c.active.len: if c.active[i].playerId == playerId: result.add c.active[i].ws c.active.delete(i) else: inc i proc activeCount*(c: Conns): int = withLock c.lock: result = c.active.len proc activeCountFor*(c: Conns, playerId: int): int = withLock c.lock: result = c.playerConnIndices(playerId).len proc pendingCount*(c: Conns): int = withLock c.lock: result = c.pending