@hazae41/kcp
Version:
Zero-copy KCP protocol for the web
43 lines (39 loc) • 1.52 kB
JavaScript
;
var future = require('@hazae41/future');
var segment = require('./segment.cjs');
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 = segment.KcpSegment.commands.push;
const serial = this.parent.sendCounter++;
const unackSerial = this.parent.recvCounter;
const segment$1 = segment.KcpSegment.newOrThrow({ conversation, command, serial, unackSerial, fragment });
this.parent.output.enqueue(segment$1);
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$1);
}, lowDelay);
const { resolveOnClose, resolveOnError } = this.parent;
const resolveOnAck = new future.Future();
Promise
.race([resolveOnAck.promise, resolveOnClose.promise, resolveOnError.promise])
.finally(() => clearInterval(retry));
this.parent.resolveOnAckBySerial.set(serial, resolveOnAck);
}
}
exports.SecretKcpWriter = SecretKcpWriter;
//# sourceMappingURL=writer.cjs.map