ling-lang 2030.1.39

Ling - The Omniglot Systems Language
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
// src/runtime/net.rs — line-oriented TCP transport, N-client star topology.
//
// One process is either a host (accepts many clients) or a client (connects
// to exactly one host). Socket I/O runs on background threads so the game
// loop never blocks: `send`/`send_to` enqueue lines, `recv`/`recv_from`
// dequeue them in arrival order. Intended for LAN-latency state sync.
//
//   net_host(port)          → listen, accept up to MAX_CLIENTS peers
//   net_join(ip, port)      → connect to a host
//   net_send(str)           → host: broadcast to all clients. client: send to host.
//   net_send_to(id, str)    → host only: unicast to one client
//   net_recv()              → oldest queued line, message only ("" if none)
//   net_recv_from()         → oldest queued line as "senderId|line" ("" if none)
//   net_clients()           → host: connected client ids, comma-separated
//   net_client_count()      → number of connected peers
//   net_events()            → drains "connect:id"/"disconnect:id" log, newline-joined
//   net_status()            → 0 = idle/closed, 1 = waiting, 2 = >=1 peer connected
//   net_close()             → tear down the current host/client role and reset to idle
//
// `net_recv` and `net_recv_from` drain the same inbound queue — pick one
// style per program. Mixing them splits messages between the two arbitrarily.

use std::collections::{HashMap, VecDeque};
use std::io::{BufRead, BufReader, Write};
use std::net::{Shutdown, TcpListener, TcpStream};
use std::sync::atomic::{AtomicBool, AtomicU64, AtomicU8, Ordering};
use std::sync::mpsc::{channel, Receiver, Sender};
use std::sync::{Arc, Mutex};

const MAX_CLIENTS: usize = 64;
const MAX_QUEUE: usize = 4096;
const MAX_EVENTS: usize = 256;

type Inbound = Arc<Mutex<VecDeque<(u64, String)>>>;
type Events = Arc<Mutex<VecDeque<String>>>;

struct ClientHandle {
    out_tx: Sender<String>,
    // clone of the peer socket, kept only so `close()` can force it shut;
    // ordinary I/O goes through `out_tx` / the reader thread, never this.
    stream: Option<TcpStream>,
}

enum Role {
    Host {
        next_id: AtomicU64,
        clients: Mutex<HashMap<u64, ClientHandle>>,
        accept_stop: Arc<AtomicBool>,
    },
    Client { out_tx: Sender<String>, stream: Arc<Mutex<Option<TcpStream>>> },
}

struct Hub {
    status: Arc<AtomicU8>,
    inbound: Inbound,
    events: Events,
    role: Role,
}

static NET: Mutex<Option<Hub>> = Mutex::new(None);

fn push_bounded<T>(q: &mut VecDeque<T>, cap: usize, item: T) {
    if q.len() >= cap {
        q.pop_front();
    }
    q.push_back(item);
}

fn push_event(events: &Events, line: String) {
    if let Ok(mut g) = events.lock() {
        push_bounded(&mut g, MAX_EVENTS, line);
    }
}

// Drive one established connection: a reader thread pushes every line it
// receives into `inbound` tagged with `id`, while this thread drains `rx`
// and writes each queued outgoing line to the socket.
//
// `on_disconnect` runs from the reader thread the moment it sees EOF/error —
// it is the only reliable disconnect signal, since the writer thread may be
// parked on an empty `rx` indefinitely otherwise. It is expected to drop the
// registry's `Sender<String>` for this peer, which closes `rx` and ends the
// writer loop below.
fn run_peer(
    stream: TcpStream,
    id: u64,
    inbound: Inbound,
    rx: Receiver<String>,
    on_disconnect: impl FnOnce() + Send + 'static,
) {
    let _ = stream.set_nodelay(true);

    if let Ok(read_stream) = stream.try_clone() {
        let inbound = inbound.clone();
        std::thread::spawn(move || {
            let mut reader = BufReader::new(read_stream);
            loop {
                let mut line = String::new();
                match reader.read_line(&mut line) {
                    Ok(0) => break,
                    Ok(_) => {
                        if let Ok(mut g) = inbound.lock() {
                            push_bounded(
                                &mut g,
                                MAX_QUEUE,
                                (id, line.trim_end_matches(['\r', '\n']).to_string()),
                            );
                        }
                    },
                    Err(_) => break,
                }
            }
            on_disconnect();
        });
    }

    let mut w = stream;
    for msg in rx {
        if w.write_all(msg.as_bytes()).is_err() || w.write_all(b"\n").is_err() {
            break;
        }
        let _ = w.flush();
    }
}

fn install(role: Role) -> (Arc<AtomicU8>, Inbound, Events) {
    let status = Arc::new(AtomicU8::new(1));
    let inbound = Arc::new(Mutex::new(VecDeque::new()));
    let events = Arc::new(Mutex::new(VecDeque::new()));
    if let Ok(mut g) = NET.lock() {
        *g = Some(Hub { status: status.clone(), inbound: inbound.clone(), events: events.clone(), role });
    }
    (status, inbound, events)
}

// ── multi-client host (net_host / net_recv_from / net_send_to) ─────────────
pub fn host(port: u16) {
    let accept_stop = Arc::new(AtomicBool::new(false));
    let (status, inbound, events) = install(Role::Host {
        next_id: AtomicU64::new(1),
        clients: Mutex::new(HashMap::new()),
        accept_stop: accept_stop.clone(),
    });
    std::thread::spawn(move || {
        let listener = match TcpListener::bind(("0.0.0.0", port)) {
            Ok(l) => l,
            Err(_) => {
                status.store(0, Ordering::SeqCst);
                return;
            },
        };
        let _ = listener.set_nonblocking(true);
        loop {
            if accept_stop.load(Ordering::SeqCst) {
                break;
            }
            let stream = match listener.accept() {
                Ok((s, _)) => s,
                Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => {
                    std::thread::sleep(Duration::from_millis(20));
                    continue;
                },
                Err(_) => continue,
            };
            let g = NET.lock().ok();
            let Some(guard) = g else { break };
            let Some(hub) = guard.as_ref() else { break };
            let Role::Host { next_id, clients, .. } = &hub.role else { break };
            let count = clients.lock().map(|m| m.len()).unwrap_or(0);
            if count >= MAX_CLIENTS {
                drop(guard);
                continue;
            }
            let id = next_id.fetch_add(1, Ordering::SeqCst);
            let (tx, rx) = channel::<String>();
            let shutdown_handle = stream.try_clone().ok();
            if let Ok(mut m) = clients.lock() {
                m.insert(id, ClientHandle { out_tx: tx, stream: shutdown_handle });
            }
            drop(guard);

            status.store(2, Ordering::SeqCst);
            push_event(&events, format!("connect:{id}"));

            let inbound2 = inbound.clone();
            let events2 = events.clone();
            std::thread::spawn(move || {
                let events3 = events2.clone();
                run_peer(stream, id, inbound2, rx, move || {
                    if let Ok(g) = NET.lock() {
                        if let Some(hub) = g.as_ref() {
                            if let Role::Host { clients, .. } = &hub.role {
                                if let Ok(mut m) = clients.lock() {
                                    m.remove(&id);
                                    if m.is_empty() {
                                        hub.status.store(1, Ordering::SeqCst);
                                    }
                                }
                            }
                        }
                    }
                    push_event(&events3, format!("disconnect:{id}"));
                });
            });
        }
    });
}

pub fn join(ip: &str, port: u16) {
    let (tx, rx) = channel::<String>();
    let shutdown_slot: Arc<Mutex<Option<TcpStream>>> = Arc::new(Mutex::new(None));
    let (status, inbound, _events) = install(Role::Client { out_tx: tx, stream: shutdown_slot.clone() });
    let ip = ip.to_string();
    std::thread::spawn(move || {
        let stream = match TcpStream::connect((ip.as_str(), port)) {
            Ok(s) => s,
            Err(_) => {
                status.store(0, Ordering::SeqCst);
                return;
            },
        };
        if let Ok(mut g) = shutdown_slot.lock() {
            *g = stream.try_clone().ok();
        }
        status.store(2, Ordering::SeqCst);
        run_peer(stream, 0, inbound, rx, move || {
            if let Ok(g) = NET.lock() {
                if let Some(hub) = g.as_ref() {
                    hub.status.store(0, Ordering::SeqCst);
                }
            }
        });
    });
}
// Up to 64 concurrent connections, each with its own id (assigned on accept)
// and its own outbound queue. Inbound lines from every connection land in one
// shared FIFO `inbox`, drained via recv_from() as "<id>|<line>".


/// Tear down the current role (host or client) and reset to idle. Sockets are
/// shut down so the reader/writer threads they own unblock and exit; the
/// registry itself is dropped by returning the `Hub` out of `NET` and letting
/// it fall out of scope. A later `host`/`join` reinstalls a fresh one.
pub fn close() {
    let hub = match NET.lock() {
        Ok(mut g) => g.take(),
        Err(_) => None,
    };
    let Some(hub) = hub else { return };
    hub.status.store(0, Ordering::SeqCst);
    match &hub.role {
        Role::Host { accept_stop, clients, .. } => {
            accept_stop.store(true, Ordering::SeqCst);
            if let Ok(m) = clients.lock() {
                for c in m.values() {
                    if let Some(s) = &c.stream {
                        let _ = s.shutdown(Shutdown::Both);
                    }
                }
            }
        },
        Role::Client { stream, .. } => {
            if let Ok(s) = stream.lock() {
                if let Some(s) = s.as_ref() {
                    let _ = s.shutdown(Shutdown::Both);
                }
            }
        },
    }
}

pub fn send(s: &str) {
    if let Ok(g) = NET.lock() {
        if let Some(hub) = g.as_ref() {
            match &hub.role {
                Role::Host { clients, .. } => {
                    if let Ok(m) = clients.lock() {
                        for c in m.values() {
                            let _ = c.out_tx.send(s.to_string());
                        }
                    }
                },
                Role::Client { out_tx, .. } => {
                    let _ = out_tx.send(s.to_string());
                },
            }
        }
    }
}

pub fn send_to(id: u64, s: &str) {
    if let Ok(g) = NET.lock() {
        if let Some(hub) = g.as_ref() {
            if let Role::Host { clients, .. } = &hub.role {
                if let Ok(m) = clients.lock() {
                    if let Some(c) = m.get(&id) {
                        let _ = c.out_tx.send(s.to_string());
                    }
                }
            }
        }
    }
}

pub fn recv() -> String {
    if let Ok(g) = NET.lock() {
        if let Some(hub) = g.as_ref() {
            if let Ok(mut q) = hub.inbound.lock() {
                if let Some((_, msg)) = q.pop_front() {
                    return msg;
                }
            }
        }
    }
    String::new()
}

pub fn recv_from() -> String {
    if let Ok(g) = NET.lock() {
        if let Some(hub) = g.as_ref() {
            if let Ok(mut q) = hub.inbound.lock() {
                if let Some((id, msg)) = q.pop_front() {
                    return format!("{id}|{msg}");
                }
            }
        }
    }
    String::new()
}

pub fn clients() -> String {
    if let Ok(g) = NET.lock() {
        if let Some(hub) = g.as_ref() {
            if let Role::Host { clients, .. } = &hub.role {
                if let Ok(m) = clients.lock() {
                    let mut ids: Vec<u64> = m.keys().copied().collect();
                    ids.sort_unstable();
                    return ids.iter().map(|i| i.to_string()).collect::<Vec<_>>().join(",");
                }
            }
        }
    }
    String::new()
}

pub fn client_count() -> usize {
    if let Ok(g) = NET.lock() {
        if let Some(hub) = g.as_ref() {
            return match &hub.role {
                Role::Host { clients, .. } => clients.lock().map(|m| m.len()).unwrap_or(0),
                Role::Client { .. } => usize::from(hub.status.load(Ordering::SeqCst) == 2),
            };
        }
    }
    0
}

pub fn events() -> String {
    if let Ok(g) = NET.lock() {
        if let Some(hub) = g.as_ref() {
            if let Ok(mut q) = hub.events.lock() {
                let out: Vec<String> = q.drain(..).collect();
                return out.join("\n");
            }
        }
    }
    String::new()
}

pub fn status() -> u8 {
    if let Ok(g) = NET.lock() {
        if let Some(hub) = g.as_ref() {
            return hub.status.load(Ordering::SeqCst);
        }
    }
    0
}

// ── LAN lobby discovery (UDP broadcast) ─────────────────────────────────────
// A host periodically broadcasts an "info" line; joiners listen and build a
// live server list. Info is opaque to the transport (the game encodes
// name|stage|private|... into it).

use std::net::UdpSocket;
use std::time::{Duration, Instant};

static ANNOUNCE: Mutex<Option<String>> = Mutex::new(None); // Some(info) while announcing
static ANNOUNCE_RUN: AtomicU8 = AtomicU8::new(0);
static DISCOVER: Mutex<Option<HashMap<String, (String, Instant)>>> = Mutex::new(None);
static DISCOVER_RUN: AtomicU8 = AtomicU8::new(0);

/// Start (or update) broadcasting `info` on the LAN discovery `port` (~1 Hz).
pub fn announce(port: u16, info: &str) {
    if let Ok(mut g) = ANNOUNCE.lock() {
        *g = Some(info.to_string());
    }
    if ANNOUNCE_RUN.swap(1, Ordering::SeqCst) == 1 {
        return;
    }
    std::thread::spawn(move || {
        let sock = match UdpSocket::bind("0.0.0.0:0") {
            Ok(s) => s,
            Err(_) => {
                ANNOUNCE_RUN.store(0, Ordering::SeqCst);
                return;
            },
        };
        let _ = sock.set_broadcast(true);
        let addr = format!("255.255.255.255:{port}");
        loop {
            let info = ANNOUNCE.lock().ok().and_then(|g| g.clone());
            match info {
                Some(s) => {
                    let _ = sock.send_to(s.as_bytes(), &addr);
                },
                None => break,
            }
            std::thread::sleep(Duration::from_millis(1000));
        }
        ANNOUNCE_RUN.store(0, Ordering::SeqCst);
    });
}

/// Stop broadcasting.
pub fn announce_stop() {
    if let Ok(mut g) = ANNOUNCE.lock() {
        *g = None;
    }
}

/// Host self-test: can we actually bind the TCP host port, and what's our LAN IP?
/// (External reachability still depends on firewall + router port-forward.)
pub fn test_bind(port: u16) -> String {
    let mut out = String::new();
    match TcpListener::bind(("0.0.0.0", port)) {
        Ok(l) => {
            let _ = l;
            out.push_str(&format!("TCP bind 0.0.0.0:{port}: OK (host can listen)\n"));
        },
        Err(e) => {
            out.push_str(&format!("TCP bind 0.0.0.0:{port}: FAILED -- {e}\n"));
        },
    }
    if let Ok(s) = UdpSocket::bind("0.0.0.0:0") {
        if s.connect("8.8.8.8:80").is_ok() {
            if let Ok(a) = s.local_addr() {
                out.push_str(&format!(
                    "LAN IP (give to same-network joiners): {}\n",
                    a.ip()
                ));
            }
        }
    }
    out
}

/// Return discovered servers seen in the last 5 s as "ip|info\n…" (starts the
/// listener on first call).
pub fn discover(port: u16) -> String {
    if DISCOVER_RUN.swap(1, Ordering::SeqCst) == 0 {
        if let Ok(mut g) = DISCOVER.lock() {
            *g = Some(HashMap::new());
        }
        std::thread::spawn(move || {
            let sock = match UdpSocket::bind(("0.0.0.0", port)) {
                Ok(s) => s,
                Err(_) => {
                    DISCOVER_RUN.store(0, Ordering::SeqCst);
                    return;
                },
            };
            let _ = sock.set_read_timeout(Some(Duration::from_millis(700)));
            let mut buf = [0u8; 512];
            loop {
                if let Ok((n, src)) = sock.recv_from(&mut buf) {
                    let info = String::from_utf8_lossy(&buf[..n]).replace(['\n', '\r'], " ");
                    if let Ok(mut g) = DISCOVER.lock() {
                        if let Some(m) = g.as_mut() {
                            m.insert(src.ip().to_string(), (info, Instant::now()));
                        }
                    }
                }
            }
        });
    }
    let mut out = String::new();
    if let Ok(g) = DISCOVER.lock() {
        if let Some(m) = g.as_ref() {
            for (ip, (info, t)) in m.iter() {
                if t.elapsed() < Duration::from_secs(5) {
                    out.push_str(ip);
                    out.push('|');
                    out.push_str(info);
                    out.push('\n');
                }
            }
        }
    }
    out
}

// `NET` is one global slot per process, so these tests share it and must run
// serially — take `TEST_LOCK` first in every test.
#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Read as _;
    use std::time::Duration;

    static TEST_LOCK: Mutex<()> = Mutex::new(());

    fn deadline_poll(mut cond: impl FnMut() -> bool) -> bool {
        let start = Instant::now();
        while start.elapsed() < Duration::from_secs(3) {
            if cond() {
                return true;
            }
            std::thread::sleep(Duration::from_millis(5));
        }
        false
    }

    fn wait_connect(addr: &str) -> TcpStream {
        let start = Instant::now();
        loop {
            if let Ok(s) = TcpStream::connect(addr) {
                return s;
            }
            assert!(start.elapsed() < Duration::from_secs(3), "could not connect to {addr}");
            std::thread::sleep(Duration::from_millis(5));
        }
    }

    #[test]
    fn host_accepts_multiple_clients_and_broadcasts_to_all() {
        let _g = TEST_LOCK.lock().unwrap();
        let port = 18901;
        host(port);
        let addr = format!("127.0.0.1:{port}");
        let mut peers: Vec<TcpStream> = (0..3).map(|_| wait_connect(&addr)).collect();

        assert!(deadline_poll(|| client_count() == 3), "expected 3 clients connected");
        let ids: Vec<u64> = clients().split(',').map(|s| s.parse().unwrap()).collect();
        assert_eq!(ids.len(), 3);

        let ev = events();
        assert_eq!(ev.matches("connect:").count(), 3);

        send("hello");
        for p in peers.iter_mut() {
            let mut reader = BufReader::new(p.try_clone().unwrap());
            let mut line = String::new();
            reader.read_line(&mut line).unwrap();
            assert_eq!(line.trim_end(), "hello");
        }
    }

    #[test]
    fn recv_from_preserves_order_and_does_not_duplicate() {
        let _g = TEST_LOCK.lock().unwrap();
        let port = 18902;
        host(port);
        let addr = format!("127.0.0.1:{port}");
        let mut peer = wait_connect(&addr);
        assert!(deadline_poll(|| client_count() == 1));

        for i in 0..5 {
            peer.write_all(format!("m{i}\n").as_bytes()).unwrap();
        }
        peer.flush().unwrap();

        let mut got = Vec::new();
        assert!(deadline_poll(|| {
            loop {
                let s = recv_from();
                if s.is_empty() {
                    break;
                }
                got.push(s);
            }
            got.len() == 5
        }));

        let expected_id = got[0].split('|').next().unwrap().to_string();
        for (i, line) in got.iter().enumerate() {
            assert_eq!(*line, format!("{expected_id}|m{i}"));
        }
        assert_eq!(recv_from(), "", "queue must be drained, not re-delivered");
    }

    #[test]
    fn disconnect_is_observed_and_client_is_dropped_from_the_registry() {
        let _g = TEST_LOCK.lock().unwrap();
        let port = 18903;
        host(port);
        let addr = format!("127.0.0.1:{port}");
        let peer = wait_connect(&addr);
        assert!(deadline_poll(|| client_count() == 1));

        drop(peer);
        assert!(deadline_poll(|| client_count() == 0), "disconnect must drop the client");
        assert!(deadline_poll(|| events().contains("disconnect:")));
    }

    #[test]
    fn join_reports_connected_status_and_exchanges_lines_with_a_bare_host() {
        let _g = TEST_LOCK.lock().unwrap();
        let port = 18904;
        let listener = TcpListener::bind(("127.0.0.1", port)).unwrap();
        let handle = std::thread::spawn(move || {
            let (mut s, _) = listener.accept().unwrap();
            s.write_all(b"from-host\n").unwrap();
            let mut reader = BufReader::new(s.try_clone().unwrap());
            let mut line = String::new();
            reader.read_line(&mut line).unwrap();
            line
        });

        join("127.0.0.1", port);
        assert!(deadline_poll(|| status() == 2), "client must report connected");

        let mut got = String::new();
        assert!(deadline_poll(|| {
            got = recv();
            !got.is_empty()
        }));
        assert_eq!(got, "from-host");

        send("ping");
        let echoed = handle.join().unwrap();
        assert_eq!(echoed.trim_end(), "ping");
    }

    #[test]
    fn client_cap_rejects_connections_past_the_limit() {
        let _g = TEST_LOCK.lock().unwrap();
        let port = 18905;
        host(port);
        let addr = format!("127.0.0.1:{port}");
        let mut peers: Vec<TcpStream> = (0..MAX_CLIENTS + 2).map(|_| wait_connect(&addr)).collect();

        assert!(deadline_poll(|| client_count() == MAX_CLIENTS));

        // The rejected sockets get dropped by the server without ever joining
        // the registry, so a read on them observes EOF.
        let rejected = peers.last_mut().unwrap();
        let mut buf = [0u8; 8];
        assert!(deadline_poll(|| matches!(rejected.read(&mut buf), Ok(0))));
    }

    #[test]
    fn close_on_client_role_drops_the_socket_and_resets_status() {
        let _g = TEST_LOCK.lock().unwrap();
        let port = 18906;
        let listener = TcpListener::bind(("127.0.0.1", port)).unwrap();
        let handle = std::thread::spawn(move || {
            let (s, _) = listener.accept().unwrap();
            // hold the socket open; only our shutdown() should end this read
            let mut reader = BufReader::new(s);
            let mut line = String::new();
            let n = reader.read_line(&mut line).unwrap_or(0);
            n == 0
        });

        join("127.0.0.1", port);
        assert!(deadline_poll(|| status() == 2));

        close();
        assert_eq!(status(), 0);
        assert_eq!(recv(), "", "closed hub must not serve stale state");

        let host_saw_eof = handle.join().unwrap();
        assert!(host_saw_eof, "close() must shut the socket down, not just forget it");
    }

    #[test]
    fn close_on_host_role_stops_accepting_and_drops_all_clients() {
        let _g = TEST_LOCK.lock().unwrap();
        let port = 18907;
        host(port);
        let addr = format!("127.0.0.1:{port}");
        let mut peers: Vec<TcpStream> = (0..3).map(|_| wait_connect(&addr)).collect();
        assert!(deadline_poll(|| client_count() == 3));

        close();
        assert_eq!(status(), 0);
        assert_eq!(client_count(), 0);

        for p in peers.iter_mut() {
            let mut buf = [0u8; 8];
            assert!(deadline_poll(|| matches!(p.read(&mut buf), Ok(0))));
        }

        // the accept loop must actually have stopped, not just forgotten its
        // registry: once the listener itself drops, new connects are refused
        assert!(
            deadline_poll(|| TcpStream::connect(&addr).is_err()),
            "listener must be gone after close(), not still accepting"
        );
    }
}