Import packages/webrtc/src/transport.ts

This commit is contained in:
QQ 2026-11-14 15:05:57 +08:00
parent 233084c740
commit 2a0164d8cc
1 changed files with 47 additions and 0 deletions

View File

@ -0,0 +1,47 @@
import type { TransportStats } from '@dsh-remote/protocol'
export interface RemoteTransport {
connect(): Promise<void>
send(data: Uint8Array): Promise<void>
onMessage(cb: (data: Uint8Array) => void): () => void
onClose?(cb: (error?: Error) => void): () => void
close(): Promise<void>
getStats(): TransportStats
}
export interface SecureHandshakeTransport extends RemoteTransport {
markSecureReady?(): void
connectionInfo(): { connectionId: string; localDeviceId: string; remoteDeviceId: string }
sendHandshake(step: number, data: Uint8Array): Promise<void>
onHandshake(cb: (step: number, data: Uint8Array) => void): () => void
}
type MessageHandler = (data: Uint8Array) => void
export abstract class BaseTransport implements RemoteTransport {
protected handlers = new Set<MessageHandler>()
protected closeHandlers = new Set<(error?: Error) => void>()
onMessage(cb: MessageHandler): () => void {
this.handlers.add(cb)
return () => this.handlers.delete(cb)
}
onClose(cb: (error?: Error) => void): () => void {
this.closeHandlers.add(cb)
return () => this.closeHandlers.delete(cb)
}
protected emit(data: Uint8Array): void {
for (const handler of this.handlers) handler(data)
}
protected emitClose(error?: Error): void {
for (const handler of this.closeHandlers) handler(error)
}
abstract connect(): Promise<void>
abstract send(data: Uint8Array): Promise<void>
abstract close(): Promise<void>
abstract getStats(): TransportStats
}