wip(0.S3): the packages migrated by ludic migrate state packages - every package test green

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Orkun ÇAKILKAYA 2026-09-25 14:02:21 +03:00
parent 1e8b5b0523
commit 07505e7ef2
294 changed files with 14996 additions and 14675 deletions

View file

@ -1,106 +1,105 @@
# session.ludic - hosting, joining and leaving. The host is pid 1 and hands out 2 upwards; this
# machine's pid is what its packets say. A token picked by the host names the session.
var net__why: int = 0
# open a socket on `port` and take guests; false (net_why) when the port is taken
export function net_host(port: int, token: int) -> bool {
net__init()
if net_live() { return true }
net__sock = NetSocket.open(port)
if net__sock <= 0 {
net__sock = 0
net__why = NET_WHY_PORT_TAKEN
export function net_host(net_st: mut NetState, port: int, token: int) -> bool {
net__init(net_st)
if net_live(net_st) { return true }
net_st.net__sock = NetSocket.open(port)
if net_st.net__sock <= 0 {
net_st.net__sock = 0
net_st.net__why = NET_WHY_PORT_TAKEN
return false
}
for i in 0 .. NET_PEERS { np_close(i) }
net_become_host(token)
net__log(`hosting on port {NetSocket.port(net__sock)}`)
net_room_host()
for i in 0 .. NET_PEERS { np_close(net_st, i) }
net_become_host(net_st, token)
net__log(`hosting on port {NetSocket.port(net_st.net__sock)}`)
net_room_host(net_st)
return true
}
# dial a host at an address; the hello (net_hello_set) goes until a welcome or a refusal
export function net_join(ip: int, port: int) -> bool {
net__init()
if net_live() { return false }
if not net__socket() { return false }
for i in 0 .. NET_PEERS { np_close(i) }
net__token = 0
net__my_pid = 0
net__state = NET_JOINING
net_dial(ip, port)
export function net_join(net_st: mut NetState, ip: int, port: int) -> bool {
net__init(net_st)
if net_live(net_st) { return false }
if not net__socket(net_st) { return false }
for i in 0 .. NET_PEERS { np_close(net_st, i) }
net_st.net__token = 0
net_st.net__my_pid = 0
net_st.net__state = NET_JOINING
net_dial(net_st, ip, port)
net__log(`joining {Udp.ip_text(ip)}:{port}`)
return true
}
function net__socket() -> bool {
if net__sock <= 0 { net__sock = NetSocket.open(0) }
if net__sock > 0 { return true }
net__sock = 0
net__why = NET_WHY_NO_SOCKET
function net__socket(net_st: mut NetState) -> bool {
if net_st.net__sock <= 0 { net_st.net__sock = NetSocket.open(0) }
if net_st.net__sock > 0 { return true }
net_st.net__sock = 0
net_st.net__why = NET_WHY_NO_SOCKET
return false
}
# the message just written is the hello: what a host is asked to let this machine in with
export function net_hello_set() -> void {
net__init()
nb_copy(net__hello_b, 0, net__wb, 0, net__wl)
net__hello_l = net__wl
if net__hello_l > NET_MTU - 40 { net__hello_l = NET_MTU - 40 }
export function net_hello_set(net_st: mut NetState) -> void {
net__init(net_st)
nb_copy(net_st.net__hello_b, 0, net_st.net__wb, 0, net_st.net__wl)
net_st.net__hello_l = net_st.net__wl
if net_st.net__hello_l > NET_MTU - 40 { net_st.net__hello_l = NET_MTU - 40 }
}
# (again) to that address, with a fresh connection and the hello sent anew
export function net_dial(ip: int, port: int) -> void {
net__init()
np_close(0)
net__join_ip = ip
net__join_port = port
net__open(0, ip, port, 1)
net__hello_t = -1.0
export function net_dial(net_st: mut NetState, ip: int, port: int) -> void {
net__init(net_st)
np_close(net_st, 0)
net_st.net__join_ip = ip
net_st.net__join_port = port
net__open(net_st, 0, ip, port, 1)
net_st.net__hello_t = -1.0
}
# a host that set out on a new trip, or a new host: the same socket asks to be let in there
export function net_follow(ip: int, port: int) -> void {
net_dial(ip, port)
net__token = 0
net__my_pid = 0
net__state = NET_JOINING
export function net_follow(net_st: mut NetState, ip: int, port: int) -> void {
net_dial(net_st, ip, port)
net_st.net__token = 0
net_st.net__my_pid = 0
net_st.net__state = NET_JOINING
}
# the old host is gone and this machine keeps the socket, alone (it may become the host)
export function net_stand_alone() -> void { net__state = NET_IDLE }
export function net_stand_alone(net_st: mut NetState) -> void { net_st.net__state = NET_IDLE }
export function net_become_host(token: int) -> void {
net__init()
net__state = NET_HOSTING
net__my_pid = 1
net__next_pid = 2
net__token = token
export function net_become_host(net_st: mut NetState, token: int) -> void {
net__init(net_st)
net_st.net__state = NET_HOSTING
net_st.net__my_pid = 1
net_st.net__next_pid = 2
net_st.net__token = token
}
# a welcome: this machine is `pid` in the session `token`
export function net_joined_as(token: int, pid: int) -> void {
net__token = token
net__my_pid = pid
net__state = NET_JOINED
net__wait_more = 0
export function net_joined_as(net_st: mut NetState, token: int, pid: int) -> void {
net_st.net__token = token
net_st.net__my_pid = pid
net_st.net__state = NET_JOINED
net_st.net__wait_more = 0
}
# the host lets the stranger in `slot` in: its pid, from 2 upwards
export function net_accept(slot: int) -> int {
if net__state != NET_HOSTING or not net_peer_on(slot) { return 0 }
let p = net__peers[slot]
export function net_accept(net_st: mut NetState, slot: int) -> int {
if net_st.net__state != NET_HOSTING or not net_peer_on(net_st, slot) { return 0 }
let p = net_st.net__peers[slot]
if p.pid >= 2 { return p.pid }
p.pid = net__next_pid
net__next_pid += 1
p.pid = net_st.net__next_pid
net_st.net__next_pid += 1
return p.pid
}
# a slow host is given this many more seconds to answer (a map switch, a matchmaker)
export function net_hello_wait(secs: int) -> void { net__wait_more = secs }
export function net_hello_wait(net_st: mut NetState, secs: int) -> void { net_st.net__wait_more = secs }
# a room code: joining, with no address yet - the matchmaker's answer brings one
function net__wait_for_host() -> void {
net__state = NET_JOINING
net__hello_t = 0.0
function net__wait_for_host(net_st: mut NetState) -> void {
net_st.net__state = NET_JOINING
net_st.net__hello_t = 0.0
}