code wiki / _hdl_build / nx_quic_pipe_test.nx
nx_quic_pipe_test.nx source
↩ module page · 101 lines · 4978 B
1// nx_quic_pipe_test.nx -- RUNG 8c.2 gate: the FULL resilient datagram pipe END-TO-END over a REAL UDP
2// socket. A block of video frames is FEC-encoded (R8a), each datagram carries [index][frame] in a
3// DATAGRAM frame (R1) sent over UDP (R8c.1); ONE source datagram is dropped IN TRANSIT (never sent =
4// simulated link loss); the receiver reconstructs it from the survivors + parity -- zero retransmit, the
5// thing that beats the TCP/ARQ stall. This proves the resilience exceed works on real sockets, sovereign.
6// license_tier: ORIGINAL
7import "nx_quic_fec.nx"
8import "nx_g_puts_lib.nx"
9import "nx_quic_udp.nx"
10
11func g_pn(v: i64) -> i64 {
12 if v==0 { sys_write(1,"0" as *u8,1); return 0 }
13 let b: *u8 = sys_mmap(28); var x: i64 = v; if x<0 { sys_write(1,"-" as *u8,1); x=0-x }
14 var d: i64=0; var y: i64=x
15 while y>0 { d=d+1; y=y/10 }
16 var i: i64=d-1; y=x
17 while i>=0 { b[i]=(48+(y%10)) as u8; y=y/10; i=i-1 }
18 sys_write(1,b,d); return 0
19}
20func g_check(name: *u8, cond: i64) -> i64 {
21 if cond==1 { g_puts(" PASS " as *u8) } else { g_puts(" FAIL " as *u8) }
22 g_puts(name); g_puts("\n" as *u8); return cond
23}
24// send source/parity packet `idx` (idx=255 for parity): datagram payload = [idx][data L bytes], wrapped
25// in a DATAGRAM frame, over UDP.
26func send_pkt(sfd: i64, port: i64, idx: i64, data: *u8, L: i64) -> i64 {
27 let pl: *u8 = sys_mmap(L+1)
28 pl[0] = idx as u8
29 var i: i64=0; while i<L { pl[1+i]=data[i]; i=i+1 }
30 let fr: *u8 = sys_mmap(L+16)
31 let fl: i64 = quic_datagram_encode(fr, pl, L+1)
32 return quic_udp_sendto(sfd, port, fr, fl)
33}
34
35func main() -> i64 {
36 g_puts("nx_quic_pipe gate -- resilient FEC datagram pipe end-to-end over real UDP\n" as *u8)
37 var pass: i64=0; var total: i64=0
38 let N: i64 = 4; let L: i64 = 50; let PORT: i64 = 19444
39
40 // 4 distinct source frames + FEC parity
41 let source: *u8 = sys_mmap(N*L)
42 var i: i64=0; while i<N { var j: i64=0; while j<L { source[i*L+j]=((i*53+j*3+9)&255) as u8; j=j+1 } i=i+1 }
43 let parity: *u8 = sys_mmap(L)
44 quic_fec_encode(source, N, L, parity)
45
46 let rfd: i64 = quic_udp_bind(PORT)
47 pass = pass + g_check("bind resilient-pipe receiver on 127.0.0.1:19444" as *u8, (rfd >= 0) as i64); total=total+1
48 if rfd >= 0 {
49 sys_set_socket_timeout(rfd, 3)
50 let sfd: i64 = quic_udp_socket()
51 // SEND source 0,1,3 + parity. source 2 is DROPPED in transit (never sent = link loss).
52 send_pkt(sfd, PORT, 0, (source as i64 + 0*L) as *u8, L)
53 send_pkt(sfd, PORT, 1, (source as i64 + 1*L) as *u8, L)
54 send_pkt(sfd, PORT, 3, (source as i64 + 3*L) as *u8, L)
55 send_pkt(sfd, PORT, 255, parity, L)
56
57 // RECEIVE 4 datagrams, place by index
58 let rsource: *u8 = sys_mmap(N*L)
59 let rparity: *u8 = sys_mmap(L)
60 let present: *i64 = sys_mmap(8*N) as *i64
61 present[0]=0; present[1]=0; present[2]=0; present[3]=0
62 var pp_present: i64=0
63 let rbuf: *u8 = sys_mmap(256)
64 let poff: *i64 = sys_mmap(8) as *i64
65 var got: i64=0
66 while got < 4 {
67 let n: i64 = quic_udp_recv(rfd, rbuf, 256)
68 if n <= 0 { got = 4 } // timeout/error -> stop
69 else {
70 let pl_len: i64 = quic_datagram_parse(rbuf, n, poff)
71 if pl_len == L+1 {
72 let base: i64 = poff[0]
73 let idx: i64 = rbuf[base] as i64
74 if idx == 255 { var j: i64=0; while j<L { rparity[j]=rbuf[base+1+j]; j=j+1 } pp_present=1 }
75 else { var j: i64=0; while j<L { rsource[idx*L+j]=rbuf[base+1+j]; j=j+1 } present[idx]=1 }
76 }
77 got = got + 1
78 }
79 }
80 var recv_ok: i64=1
81 if present[0]!=1 { recv_ok=0 }
82 if present[1]!=1 { recv_ok=0 }
83 if present[3]!=1 { recv_ok=0 }
84 if pp_present!=1 { recv_ok=0 }
85 if present[2]!=0 { recv_ok=0 } // source 2 genuinely lost
86 pass = pass + g_check("over UDP: source 0/1/3 + parity arrive, source 2 lost in transit" as *u8, recv_ok); total=total+1
87
88 // FEC-RECOVER the lost source 2 -- zero retransmit
89 let out: *u8 = sys_mmap(L)
90 let rec_idx: i64 = quic_fec_recover_one(rsource, present, N, L, rparity, pp_present, out)
91 var rec_ok: i64=0
92 if rec_idx==2 { rec_ok=1; var k: i64=0; while k<L { if out[k]!=source[2*L+k] { rec_ok=0; k=L } else { k=k+1 } } }
93 pass = pass + g_check("FEC reconstructs the lost frame byte-exact over the wire (ZERO retransmit)" as *u8, rec_ok); total=total+1
94 sys_close(sfd); sys_close(rfd)
95 }
96
97 g_puts("---- quic_pipe gate: passed " as *u8); g_pn(pass); g_puts(" / " as *u8); g_pn(total); g_puts(" ----\n" as *u8)
98 if pass == total { g_puts("VERDICT: GREEN (resilient FEC datagram pipe works end-to-end over real UDP -- the exceed, on the wire)\n" as *u8); return 0 }
99 g_puts("VERDICT: RED\n" as *u8)
100 return 1
101}