UNPKG

@hazae41/kcp

Version:

Zero-copy KCP protocol for the web

41 lines (38 loc) 1.48 kB
import { Future } from '@hazae41/future'; import { KcpSegment } from '../segment/index.mjs'; class SecretKcpWriter { parent; constructor(parent) { this.parent = parent; } async onWrite(fragment) { const { lowDelay = 300, highDelay = 3000 } = this.parent.params; const conversation = this.parent.conversation; const command = KcpSegment.commands.push; const serial = this.parent.sendCounter++; const unackSerial = this.parent.recvCounter; const segment = KcpSegment.newOrThrow({ conversation, command, serial, unackSerial, fragment }); this.parent.output.enqueue(segment); const start = Date.now(); const retry = setInterval(() => { if (this.parent.closed) { clearInterval(retry); return; } const delay = Date.now() - start; if (delay > highDelay) { clearInterval(retry); return; } this.parent.output.enqueue(segment); }, lowDelay); const { resolveOnClose, resolveOnError } = this.parent; const resolveOnAck = new Future(); Promise .race([resolveOnAck.promise, resolveOnClose.promise, resolveOnError.promise]) .finally(() => clearInterval(retry)); this.parent.resolveOnAckBySerial.set(serial, resolveOnAck); } } export { SecretKcpWriter }; //# sourceMappingURL=index.mjs.map