code wiki / (root) / nx_quic_udp.nx

nx_quic_udp.nx source

↩ module page · 34 lines · 1700 B

1// nx_quic_udp.nx -- RUNG 8c.1 of the sovereign QUIC transport: the UDP datagram endpoint. QUIC runs over 2// UDP (not TCP) -- that is precisely what lets a lost packet be FEC-recovered instead of TCP-retransmitted 3// (the resilience exceed). This is the sovereign socket layer the live relay's QUIC server stands on: 4// bind a UDP socket, send a QUIC packet, receive one. Reuses the proven nx_sc_sockaddr (sockaddr_in 5// byte-exact) from the IoT broadcast path. license_tier: ORIGINAL 6import "nx_syscalls.nx" 7import "nx_iot_sc_broadcast.nx" // AF_INET, SOCK_DGRAM, nx_sc_sockaddr 8 9// open a UDP socket bound to 127.0.0.1:port. returns fd >= 0, or -1 on socket/bind failure. 10func quic_udp_bind(port: i64) -> i64 { 11 let fd: i64 = sys_socket(AF_INET, SOCK_DGRAM, 0) 12 if fd < 0 { return 0 - 1 } 13 let sa: *u8 = sys_mmap(16) 14 nx_sc_sockaddr(sa, 127, 0, 0, 1, port) 15 if sys_bind(fd, sa, 16) < 0 { sys_close(fd); return 0 - 1 } 16 return fd 17} 18// open an unbound UDP socket for sending. returns fd. 19func quic_udp_socket() -> i64 { return sys_socket(AF_INET, SOCK_DGRAM, 0) } 20// send buf[0..len) to 127.0.0.1:port. returns sys_sendto result (len on success). 21func quic_udp_sendto(fd: i64, port: i64, buf: *u8, len: i64) -> i64 { 22 let sa: *u8 = sys_mmap(16) 23 nx_sc_sockaddr(sa, 127, 0, 0, 1, port) 24 return sys_sendto(fd, buf, len, 0, sa, 16) 25} 26// receive one datagram into buf (cap). returns bytes received (or <0). caller should set a recv timeout. 27func quic_udp_recv(fd: i64, buf: *u8, cap: i64) -> i64 { 28 let src: *u8 = sys_mmap(16) 29 let alen: *i64 = sys_mmap(8) as *i64 30 alen[0] = 16 31 return sys_recvfrom(fd, buf, cap, 0, src, alen) 32} 33 34func main() -> i64 { return 0 }