import { canAcceptSession, MAX_ACTIVE_SESSIONS } from "../constants.js"; export type ConnectionState = | "idle" | "connecting" | "authenticating" | "syncing" | "connected" | "interrupted" | "reconnecting" | "disconnecting" | "failed"; export type SessionTeardownReason = | "client-disconnect" | "websocket-close" | "websocket-error" | "protocol-error" | "teamSpeak-disconnect" | "teamSpeak-kicked" // 会话被 TeamSpeak 服务器踢出/封禁时拆除 | "teamSpeak-connect-failed" | "heartbeat-timeout" | "admin-terminated" | "gateway-shutdown"; const ALLOWED_TRANSITIONS: Record = { idle: ["connecting", "disconnecting"], connecting: ["authenticating", "reconnecting", "failed", "disconnecting"], authenticating: ["syncing", "reconnecting", "failed", "disconnecting"], syncing: ["connected", "reconnecting", "failed", "disconnecting"], connected: ["interrupted", "failed", "disconnecting"], interrupted: ["reconnecting", "failed", "disconnecting"], reconnecting: ["connecting", "failed", "disconnecting"], disconnecting: ["idle"], failed: ["disconnecting", "idle"], }; export class ConnectionStateMachine { private current: ConnectionState = "idle"; get state(): ConnectionState { return this.current; } transition(next: ConnectionState): void { if (next === this.current) return; if (!ALLOWED_TRANSITIONS[this.current].includes(next)) { throw new Error(`Illegal connection state transition: ${this.current} -> ${next}`); } this.current = next; } } export type SessionCleanup = (reason: SessionTeardownReason) => Promise; export class ManagedSession { readonly createdAt = Date.now(); private readonly stateMachine = new ConnectionStateMachine(); private teardownPromise: Promise | null = null; constructor( readonly id: string, private readonly cleanup: SessionCleanup, ) {} get state(): ConnectionState { return this.stateMachine.state; } transition(next: ConnectionState): void { this.stateMachine.transition(next); } async teardown(reason: SessionTeardownReason): Promise { if (this.teardownPromise) return this.teardownPromise; this.teardownPromise = (async () => { if (this.state !== "idle" && this.state !== "disconnecting") { this.stateMachine.transition("disconnecting"); } else if (this.state === "idle") { this.stateMachine.transition("disconnecting"); } try { await this.cleanup(reason); } catch { // Teardown is best effort and must not be interrupted by an already // closed socket, TeamSpeak client, or codec resource. } finally { if (this.state === "disconnecting") this.stateMachine.transition("idle"); } })(); return this.teardownPromise; } } export class SessionManager { private readonly sessions = new Map(); private accepting = true; private peak = 0; private created = 0; admit(id: string, cleanup: SessionCleanup): ManagedSession | null { if (!this.accepting || !canAcceptSession(this.sessions.size)) return null; if (this.sessions.has(id)) throw new Error(`Session already exists: ${id}`); const session = new ManagedSession(id, cleanup); this.sessions.set(id, session); this.created++; this.peak = Math.max(this.peak, this.sessions.size); return session; } get(id: string): ManagedSession | undefined { return this.sessions.get(id); } get activeCount(): number { return this.sessions.size; } get peakCount(): number { return this.peak; } get createdCount(): number { return this.created; } get isAccepting(): boolean { return this.accepting; } async teardown(id: string, reason: SessionTeardownReason): Promise { const session = this.sessions.get(id); if (!session) return; await session.teardown(reason); this.sessions.delete(id); } async shutdown(reason: SessionTeardownReason = "gateway-shutdown"): Promise { this.accepting = false; await Promise.all([...this.sessions.keys()].map((id) => this.teardown(id, reason))); } get maxSessions(): number { return MAX_ACTIVE_SESSIONS; } }