D
Deno

help

Sequentially write to TPC connection

Aandrinheusser3/3/2023
Hey, I experience errors when multiple async functions write to a TCP (Deno.Conn) connection. How can I make sure my data is sent fully, even if it can't be written to the send buffer in one function call? Using this code, I observed data (buf) being written partially, then data from other async functions being written before the remainder of the partially written data is written.
const id = (Math.random() + 1).toString(36).substring(7);
let nwritten = 0;
let pass = 0;
while (nwritten < buf.byteLength) {
console.log(id, pass - 1, 'starting pass ', pass, { nwritten, length: buf.byteLength })

nwritten += await this.#conn.write(buf.subarray(nwritten));

console.log(id, pass, ' written: ' + `${nwritten} / ${buf.byteLength}`);
pass++;
}
console.log(id, pass, ' fininshed writing: ' + nwritten + ' / ' + buf.length)
const id = (Math.random() + 1).toString(36).substring(7);
let nwritten = 0;
let pass = 0;
while (nwritten < buf.byteLength) {
console.log(id, pass - 1, 'starting pass ', pass, { nwritten, length: buf.byteLength })

nwritten += await this.#conn.write(buf.subarray(nwritten));

console.log(id, pass, ' written: ' + `${nwritten} / ${buf.byteLength}`);
pass++;
}
console.log(id, pass, ' fininshed writing: ' + nwritten + ' / ' + buf.length)
AAapoAlas3/3/2023
Std at least has some APIs named to "writeAll" which may be relevant.
Aandrinheusser3/3/2023
Yes, this "heavily inspired" the code included in the post. It's basically the same minus console.log.

Looking for more? Join the community!