$def | (255 itoc) "|" $def |s (255 itoc) "|" strcat $def MYPORT 5555 lvar packets ( D = Data packet ) ( A = Acknowledgement packet ) : packet_del ( guid -- ) packets @ swap array_delitem dup packets ! not if "_PKTCHK" timer_stop then ; : packet_add ( guid data -- ) packets @ dup not if 1 "_PKTCHK" timer_start then rot array_insertitem packets ! ; : packet_send[ str:Data str:Host int:Port -- ] prog "@pid" getprop "PACKET" { host @ ":" port @ | data @ }cat event_send ; $libdef packet_send PUBLIC packet_send : recv_udp_handler ( ... -- ) pop pop dup "from" [] var! from "data" [] | split swap atoi var! port | split swap dup "A" stringcmp not if pop ( port|A|guid ) packet_del else dup "D" stringcmp not if pop | split MYPORT intostr |s "A" |s strcat rot strcat from @ port @ udpsend pop me @ "PKT: " from @ strcat "::" strcat rot strcat notify else pop pop then then ; : send_pkt_handler ( ... -- ) pop pop "data" [] | split var! data ":" rsplit atoi var! port var! host systime_precise intostr var! guid { MYPORT | "D" | guid @ | data @ }cat guid @ { host @ ":" port @ | }cat 3 pick strcat packet_add host @ port @ udpsend pop ; : main ( s -- ) 0 array_make_dict packets ! background "UDP.*" "UDP_RECV" 'recv_udp_handler 0 onevent "USER.PACKET" "PKT_SEND" 'send_pkt_handler 0 onevent prog "@pid" pid setprop MYPORT udpopen pop begin event_wait "TIMER._PKTCHK" stringcmp not if packets @ foreach over strtof systime_precise swap - 10 > if pop packet_del continue else | split swap ":" split atoi udpsend pop pop then repeat packets @ if 1 "_PKTCHK" timer_start then then pop repeat ;