Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions packages/tcpip/src/stack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,57 @@ describe('tcp', () => {

expect(received.value).toStrictEqual(data);
});

test('tcp packets are sent immediately without delay', async () => {
const stack1 = await createStack();
const stack2 = await createStack();

const tun1 = await stack1.createTunInterface({
ip: '192.168.1.1/24',
});

const tun2 = await stack2.createTunInterface({
ip: '192.168.1.2/24',
});

// Connect the two interfaces
tun1.readable.pipeTo(tun2.writable);
tun2.readable.pipeTo(tun1.writable);

const listener = await stack2.listenTcp({
port: 8080,
});

const [outbound, inbound] = await Promise.all([
stack1.connectTcp({
host: '192.168.1.2',
port: 8080,
}),
nextValue(listener),
]);

const data = new Uint8Array([0x01, 0x02, 0x03, 0x04]);

const inboundReader = inbound.readable.getReader();
const outboundWriter = outbound.writable.getWriter();

// Record start time
const startTime = performance.now();

// Write and read immediately
await outboundWriter.write(data);
const received = await inboundReader.read();

// Record end time
const endTime = performance.now();

expect(received.value).toStrictEqual(data);

// Check timing - with tcp_output enabled, this should be very fast
// Without tcp_output, there would be a significant delay (~300ms)
const elapsed = endTime - startTime;
expect(elapsed).toBeCloseTo(0, -1);
});
});

describe('udp', () => {
Expand Down
6 changes: 6 additions & 0 deletions packages/tcpip/wasm/tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ uint16_t send_tcp_chunk(struct tcp_pcb *conn, uint8_t *chunk, uint16_t length) {
return 0;
}

// Force sending chunks immediately
err_t out_result = tcp_output(conn);
if (out_result != ERR_OK) {
return 0;
}

return bytes_to_send;
}

Expand Down