Skip to content

Commit a99b8b0

Browse files
authored
Merge pull request #23 from endorhq/flush-tcp-chunks
fix: force TCP binding to send the data immediately
2 parents b82738b + 0a08873 commit a99b8b0

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

packages/tcpip/src/stack.test.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,57 @@ describe('tcp', () => {
842842

843843
expect(received.value).toStrictEqual(data);
844844
});
845+
846+
test('tcp packets are sent immediately without delay', async () => {
847+
const stack1 = await createStack();
848+
const stack2 = await createStack();
849+
850+
const tun1 = await stack1.createTunInterface({
851+
ip: '192.168.1.1/24',
852+
});
853+
854+
const tun2 = await stack2.createTunInterface({
855+
ip: '192.168.1.2/24',
856+
});
857+
858+
// Connect the two interfaces
859+
tun1.readable.pipeTo(tun2.writable);
860+
tun2.readable.pipeTo(tun1.writable);
861+
862+
const listener = await stack2.listenTcp({
863+
port: 8080,
864+
});
865+
866+
const [outbound, inbound] = await Promise.all([
867+
stack1.connectTcp({
868+
host: '192.168.1.2',
869+
port: 8080,
870+
}),
871+
nextValue(listener),
872+
]);
873+
874+
const data = new Uint8Array([0x01, 0x02, 0x03, 0x04]);
875+
876+
const inboundReader = inbound.readable.getReader();
877+
const outboundWriter = outbound.writable.getWriter();
878+
879+
// Record start time
880+
const startTime = performance.now();
881+
882+
// Write and read immediately
883+
await outboundWriter.write(data);
884+
const received = await inboundReader.read();
885+
886+
// Record end time
887+
const endTime = performance.now();
888+
889+
expect(received.value).toStrictEqual(data);
890+
891+
// Check timing - with tcp_output enabled, this should be very fast
892+
// Without tcp_output, there would be a significant delay (~300ms)
893+
const elapsed = endTime - startTime;
894+
expect(elapsed).toBeCloseTo(0, -1);
895+
});
845896
});
846897

847898
describe('udp', () => {

packages/tcpip/wasm/tcp.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ uint16_t send_tcp_chunk(struct tcp_pcb *conn, uint8_t *chunk, uint16_t length) {
3131
return 0;
3232
}
3333

34+
// Force sending chunks immediately
35+
err_t out_result = tcp_output(conn);
36+
if (out_result != ERR_OK) {
37+
return 0;
38+
}
39+
3440
return bytes_to_send;
3541
}
3642

0 commit comments

Comments
 (0)