andrinheusserA
Denoβ€’3y agoβ€’
2 replies
andrinheusser

Sequentially write to TPC connection

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)
Was this page helpful?