/** * Base connection class with state machine, reconnection, and message buffering */ import { EventEmitter } from 'eventemitter3'; import { ConnectionConfig } from './config.js'; import { ConnectionState, ConnectionEventMap } from './events.js'; import { ExponentialBackoff } from '../utils/exponential-backoff.js'; import { MessageBuffer } from '../utils/message-buffer.js'; import { WebRTCAdapter } from '../webrtc/adapter.js'; /** * Abstract base class for WebRTC connections with durability features */ export declare abstract class RondevuConnection extends EventEmitter { protected rtcConfig?: RTCConfiguration | undefined; protected pc: RTCPeerConnection | null; protected dc: RTCDataChannel | null; protected state: ConnectionState; protected config: ConnectionConfig; protected webrtcAdapter: WebRTCAdapter; protected messageBuffer: MessageBuffer | null; protected backoff: ExponentialBackoff | null; protected reconnectTimeout: ReturnType | null; protected reconnectAttempts: number; protected connectionTimeout: ReturnType | null; protected iceGatheringTimeout: ReturnType | null; protected icePollingInterval: ReturnType | null; protected lastIcePollTime: number; protected answerProcessed: boolean; protected answerSdpFingerprint: string | null; protected iceCandidateBuffer: RTCIceCandidate[]; protected iceCandidateFlushTimer: ReturnType | null; protected static readonly ICE_BUFFER_DELAY_MS = 20; constructor(rtcConfig?: RTCConfiguration | undefined, userConfig?: Partial, webrtcAdapter?: WebRTCAdapter); /** * Transition to a new state and emit events */ protected transitionTo(newState: ConnectionState, reason?: string): void; /** * Create and configure RTCPeerConnection */ protected createPeerConnection(): RTCPeerConnection; /** * Setup data channel event handlers */ protected setupDataChannelHandlers(dc: RTCDataChannel): void; /** * Handle local ICE candidate generation * Buffers candidates for batched sending to reduce server requests */ protected handleIceCandidate(event: RTCPeerConnectionIceEvent): void; /** * Buffer an ICE candidate for batched sending */ protected bufferIceCandidate(candidate: RTCIceCandidate): void; /** * Flush buffered ICE candidates to the server */ protected flushIceCandidates(): void; /** * Handle ICE connection state changes (primary state driver) */ protected handleIceConnectionStateChange(): void; /** * Handle connection state changes (backup validation) */ protected handleConnectionStateChange(): void; /** * Handle ICE gathering state changes */ protected handleIceGatheringStateChange(): void; /** * Handle data channel open event */ protected handleDataChannelOpen(): void; /** * Handle data channel close event */ protected handleDataChannelClose(): void; /** * Handle data channel error event */ protected handleDataChannelError(error: Event): void; /** * Handle incoming message */ protected handleMessage(event: MessageEvent): void; /** * Called when connection is successfully established */ protected onConnected(): void; /** * Start ICE candidate polling */ protected startIcePolling(): void; /** * Stop ICE candidate polling */ protected stopIcePolling(): void; /** * Get the API instance - subclasses must provide */ protected abstract getApi(): any; /** * Get the owner public key - subclasses must provide */ protected abstract getOwnerPublicKey(): string; /** * Get the offer ID - subclasses must provide */ protected abstract getOfferId(): string; /** * Get the ICE candidate role this connection should accept. * Returns null for no filtering (offerer), or specific role (answerer accepts 'offerer'). */ protected abstract getIceCandidateRole(): 'offerer' | null; /** * Poll for remote ICE candidates (consolidated implementation) * Subclasses implement getIceCandidateRole() to specify filtering */ protected pollIceCandidates(): void; /** * Start connection timeout */ protected startConnectionTimeout(): void; /** * Clear connection timeout */ protected clearConnectionTimeout(): void; /** * Start ICE gathering timeout */ protected startIceGatheringTimeout(): void; /** * Clear ICE gathering timeout */ protected clearIceGatheringTimeout(): void; /** * Schedule reconnection attempt */ protected scheduleReconnect(): void; /** * Cancel scheduled reconnection */ protected cancelReconnect(): void; /** * Send a message directly (bypasses buffer) */ protected sendDirect(data: string | ArrayBuffer | Blob): void; /** * Send a message with automatic buffering */ send(data: string | ArrayBuffer | Blob): void; /** * Buffer a message for later delivery */ protected bufferMessage(data: string | ArrayBuffer | Blob): void; /** * Get current connection state */ getState(): ConnectionState; /** * Get the data channel */ getDataChannel(): RTCDataChannel | null; /** * Get the peer connection */ getPeerConnection(): RTCPeerConnection | null; /** * Close the connection */ close(): void; /** * Complete cleanup of all resources */ protected cleanup(): void; /** * Debug logging helper */ protected debug(...args: unknown[]): void; protected abstract sendBufferedIceCandidates(candidates: RTCIceCandidate[]): void; protected abstract attemptReconnect(): void; }