ludic/packages/ludic.net/punch.ludic
Orkuncakilkaya 19fcf60599 wip(0.S3): packages and examples migrated again from their pre-0.S sources in one run
ludic migrate state packages <every example program> packages/ludic.lab/example/plate.ludic
  1804 vars into 126 states, 64 into lets; 23498 edits in 460 files

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-25 15:31:34 +03:00

51 lines
2.1 KiB
Text

# punch.ludic - the host sends a few packets to each new guest's outside address, so its router
# expects the guest, while the guest's hello goes the other way: both routers have seen traffic
# go out, so the replies come in
function net__new_guest(net_st: mut NetState, pub: string, local: string) -> void {
let key = `{pub}|{local}`
if net_st.net__seen == null { net_st.net__seen = new []string }
for i in 0 .. len(net_st.net__seen) { if net_st.net__seen[i] == key { return } }
push(net_st.net__seen, key)
net__log(`room {net_st.net__room}: a guest at {pub} / {local}`)
if len(pub) > 0 and net_parse_code(net_st, pub) { net__punch_add(net_st, net_st.net__code_ip, net_st.net__code_port) }
if net_parse_code(net_st, local) { net__punch_add(net_st, net_st.net__code_ip, net_st.net__code_port) }
}
function net__punch_add(net_st: mut NetState, ip: int, port: int) -> void {
if net_st.net__punch_ip == null {
net_st.net__punch_ip = words(16)
net_st.net__punch_port = words(16)
net_st.net__punch_left = words(16)
for k in 0 .. 16 { net_st.net__punch_left[k] = 0 }
}
for k in 0 .. 16 {
if net_st.net__punch_left[k] > 0 { continue }
net_st.net__punch_ip[k] = ip
net_st.net__punch_port[k] = port
net_st.net__punch_left[k] = 10
return
}
}
# twice a second while a guest is new: a packet the guest's side ignores, which is the point
function net__punch_tick(net_st: mut NetState, dt: float) -> void {
if net_st.net__punch_ip == null { return }
net_st.net__punch_t = net_st.net__punch_t - dt
if net_st.net__punch_t > 0.0 { return }
net_st.net__punch_t = 0.5
if net_st.net__punch_b == null {
net_st.net__punch_b = buffer(4)
nb_put8(net_st.net__punch_b, 0, 'M')
nb_put8(net_st.net__punch_b, 1, 'P')
}
for k in 0 .. 16 {
if net_st.net__punch_left[k] <= 0 { continue }
NetSocket.send(net_st.net__sock, net_st.net__punch_ip[k], net_st.net__punch_port[k], net_st.net__punch_b, 4)
net_st.net__punch_left[k] -= 1
}
}
function net__punch_clear(net_st: mut NetState) -> void {
if net_st.net__punch_left != null { for k in 0 .. 16 { net_st.net__punch_left[k] = 0 } }
}