game-networking-sockets 0.3.0

Rust abstraction for Valve GameNetworkingSockets library.
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
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
//! # Rust wrapper for Valve GameNetworkingSockets
//!
//! This crate wraps the low-level GameNetworkingSockets library and gives you
//! two things:
//!
//! - **Type safety.** The socket type records its own state, so the compiler
//!   rejects any operation that the current state does not allow. Every public
//!   operation is safe to call.
//! - **A high-level API.** You never write FFI code. The API is plain,
//!   idiomatic Rust.
//!
//! # Example
//!
//! ```
//! use gns::{GnsGlobal, GnsSocket, IsCreated};
//! use std::net::Ipv6Addr;
//! use std::time::Duration;
//!
//! // Do not use `unwrap` in production. This example uses it to keep the
//! // interesting calls easy to read.
//!
//! // Initialize the global networking state. A process has exactly one.
//! let gns_global = GnsGlobal::get().unwrap();
//!
//! // Create a socket. The type parameter records the socket state, and
//! // `GnsSocket::new` is only available in the initial `IsCreated` state.
//! let gns_socket = GnsSocket::<IsCreated>::new(gns_global);
//!
//! // Choose your own port.
//! let port = 9001;
//!
//! // `connect` moves the socket from `IsCreated` to `IsClient`, which gives
//! // you the client operations.
//! let client = gns_socket.connect(Ipv6Addr::LOCALHOST.into(), port).unwrap();
//!
//! // A connected socket needs three calls in your main loop:
//! //
//! // 1. Poll for new messages.
//! // 2. Poll for connection status changes.
//! // 3. Poll for the low-level callbacks that the underlying library needs.
//! //
//! // Clients and servers use the same three calls. Only the scope differs. On
//! // a client they cover the single connection. On a server they cover every
//! // connected client.
//!
//! // Run the low-level callbacks.
//! gns_global.poll_callbacks();
//!
//! // Receive at most 100 messages and print each payload.
//! for message in client.receive_messages::<100>().expect("failed to recv").into_iter() {
//!   println!("{}", core::str::from_utf8(message.payload()).unwrap());
//! }
//!
//! // This example ignores events. A real program reads them to react when the
//! // connection opens or closes.
//! for _event in client.receive_events() {
//! }
//!
//! // Wait before the next iteration.
//! std::thread::sleep(Duration::from_millis(10))
//! ```
//!
//! # How events reach a socket
//!
//! Each [`GnsSocket`] registers a weak reference to its event queue with
//! [`GnsGlobal`]. When GameNetworkingSockets reports a connection-state change,
//! the callback uses that registry to find the socket the event belongs to.
//! Dropping the socket removes its entry.

use crossbeam_queue::SegQueue;
pub use gns_sys as sys;
use std::sync::atomic::{AtomicI64, Ordering};
use std::{
    collections::HashMap,
    ffi::{c_void, CStr, CString},
    marker::PhantomData,
    mem::MaybeUninit,
    net::{IpAddr, Ipv4Addr, Ipv6Addr},
    sync::{Arc, Mutex, OnceLock, RwLock, Weak},
    time::Duration,
};
use sys::*;

#[inline]
fn get_interface() -> *mut ISteamNetworkingSockets {
    unsafe { SteamAPI_SteamNetworkingSockets_v009() }
}

#[inline]
fn get_utils() -> *mut ISteamNetworkingUtils {
    unsafe { SteamAPI_SteamNetworkingUtils_v003() }
}

/// A network message number. This alias exists to make signatures readable.
pub type GnsMessageNumber = u64;

/// An error returned by the wrapper.
///
/// Most variants wrap the [`EResult`] that the underlying API returned. The
/// rest cover setup paths that report failure without an `EResult`.
///
/// The enum is non-exhaustive because wrapping more of the underlying API
/// adds variants.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum GnsError {
    #[error("GameNetworkingSockets_Init failed: {0}")]
    Init(String),
    #[error("listen failed: invalid handle")]
    Listen,
    #[error("connect failed: invalid handle")]
    Connect,
    #[error("create socket pair failed")]
    SocketPair,
    #[error("receive failed: invalid connection or poll group handle")]
    Receive,
    #[error("accept failed: could not set connection poll group")]
    Accept,
    #[error("close failed: invalid connection handle")]
    Close,
    #[error("steam api: {0:?}")]
    Api(EResult),
    #[error("config: {0}")]
    Config(&'static str),
}

pub type GnsResult<T> = Result<T, GnsError>;

/// Converts a `SteamNetworkingIPAddr` into a Rust [`IpAddr`], unmapping
/// IPv4-mapped IPv6 addresses back to plain IPv4.
#[inline]
fn ip_from_steam_ip_addr(addr: &SteamNetworkingIPAddr) -> IpAddr {
    let ipv4 = unsafe { addr.__bindgen_anon_1.m_ipv4 };
    if ipv4.m_8zeros == 0 && ipv4.m_0000 == 0 && ipv4.m_ffff == 0xffff {
        IpAddr::from(Ipv4Addr::from(ipv4.m_ip))
    } else {
        IpAddr::from(Ipv6Addr::from(unsafe { addr.__bindgen_anon_1.m_ipv6 }))
    }
}

/// Converts an `EResult` returned by an FFI call into a [`GnsResult`].
#[inline]
fn check(e: EResult) -> GnsResult<()> {
    match e {
        EResult::k_EResultOK => Ok(()),
        e => Err(GnsError::Api(e)),
    }
}

/// Owns the initialization and teardown of GameNetworkingSockets and its
/// singletons.
///
/// Call [`GnsGlobal::get()`] to obtain the instance. The first call initializes
/// GameNetworkingSockets, and later calls return the same instance.
pub struct GnsGlobal {
    utils: GnsUtils,
    next_queue_id: AtomicI64,
    /// Maps each socket to its event queue.
    ///
    /// Reads dominate: every connection-state callback from the
    /// GameNetworkingSockets service thread performs one lookup. Writes happen
    /// only when a socket is created or dropped, or in the rare case where a
    /// callback arrives for a socket that was dropped moments earlier. An
    /// `RwLock` lets those reads run concurrently.
    event_queues: RwLock<HashMap<i64, Weak<SegQueue<GnsConnectionEvent>>>>,
}

static GNS_GLOBAL: OnceLock<GnsGlobal> = OnceLock::new();

impl Drop for GnsGlobal {
    #[inline]
    fn drop(&mut self) {
        // Stop the service thread and tear down the internal state.
        //
        // GameNetworkingSockets does not support an init, kill, init cycle on
        // every version, so this runs only when the singleton itself is
        // dropped.
        unsafe { GameNetworkingSockets_Kill() }
    }
}

impl GnsGlobal {
    /// Returns a reference to the [`GnsGlobal`] instance.
    ///
    /// The first call initializes GameNetworkingSockets through
    /// [`sys::GameNetworkingSockets_Init`]. Later calls return the instance
    /// that call created.
    ///
    /// # Errors
    /// Returns [`GnsError::Init`] with the message that GameNetworkingSockets
    /// produced if initialization fails.
    pub fn get() -> GnsResult<&'static Self> {
        // Fast path: no lock
        if let Some(g) = GNS_GLOBAL.get() {
            return Ok(g);
        }
        // use get_or_try_init once stabilized: https://github.com/rust-lang/rust/issues/109737
        static INIT_LOCK: Mutex<()> = Mutex::new(());
        let _guard = INIT_LOCK.lock().unwrap();
        if let Some(g) = GNS_GLOBAL.get() {
            return Ok(g);
        }
        unsafe {
            let mut error: SteamDatagramErrMsg = MaybeUninit::zeroed().assume_init();
            if !GameNetworkingSockets_Init(core::ptr::null(), &mut error) {
                return Err(GnsError::Init(
                    CStr::from_ptr(error.as_ptr())
                        .to_str()
                        .unwrap_or("")
                        .to_owned(),
                ));
            }
        }
        let _ = GNS_GLOBAL.set(GnsGlobal {
            utils: GnsUtils(()),
            next_queue_id: AtomicI64::new(0),
            event_queues: RwLock::new(HashMap::new()),
        });
        Ok(GNS_GLOBAL.get().expect("impossible; qed;"))
    }

    #[inline]
    pub fn poll_callbacks(&self) {
        unsafe {
            SteamAPI_ISteamNetworkingSockets_RunCallbacks(get_interface());
        }
    }

    #[inline]
    pub fn utils(&self) -> &GnsUtils {
        &self.utils
    }

    #[inline]
    pub fn queue_count(&self) -> usize {
        self.event_queues.read().unwrap().len()
    }

    #[inline]
    fn create_queue(&self) -> (i64, Arc<SegQueue<GnsConnectionEvent>>) {
        let queue = Arc::new(SegQueue::new());
        let queue_id = self.next_queue_id.fetch_add(1, Ordering::SeqCst);
        self.event_queues
            .write()
            .unwrap()
            .insert(queue_id, Arc::downgrade(&queue));
        (queue_id, queue)
    }
}

/// An opaque wrapper around [`sys::HSteamListenSocket`].
#[repr(transparent)]
pub(crate) struct GnsListenSocket(HSteamListenSocket);

/// An opaque wrapper around [`sys::HSteamNetPollGroup`].
#[repr(transparent)]
pub(crate) struct GnsPollGroup(HSteamNetPollGroup);

/// The initial state of a [`GnsSocket`].
///
/// A socket in this state is neither a client nor a server yet, so it holds no
/// data.
pub struct IsCreated;

mod private {
    pub trait Sealed {}
    impl Sealed for super::IsServer {}
    impl Sealed for super::IsClient {}
}

/// The operations that every ready [`GnsSocket`] supports.
///
/// A ready socket is either a client or a server. Both can read connection
/// events and receive messages.
pub trait IsReady: private::Sealed {
    /// Returns the connection event queue. The queue is thread-safe.
    fn queue(&self) -> &SegQueue<GnsConnectionEvent>;
    /// Receives up to `slots.len()` messages into `slots`.
    ///
    /// Returns the number of slots that GameNetworkingSockets filled, or
    /// [`GnsError::Receive`] if the underlying handle is invalid.
    fn receive(&self, slots: &mut [MaybeUninit<*mut ISteamNetworkingMessage>]) -> GnsResult<usize>;
}

/// The state of a [`GnsSocket`] that acts as a server, normally reached
/// through [`GnsSocket::listen`].
///
/// In this state the socket holds what it needs to accept connections and poll
/// them for messages.
pub struct IsServer {
    queue: Arc<SegQueue<GnsConnectionEvent>>,
    queue_id: i64,
    global: &'static GnsGlobal,
    listen_socket: GnsListenSocket,
    poll_group: GnsPollGroup,
}

impl Drop for IsServer {
    #[inline]
    fn drop(&mut self) {
        unsafe {
            SteamAPI_ISteamNetworkingSockets_CloseListenSocket(
                get_interface(),
                self.listen_socket.0,
            );
            SteamAPI_ISteamNetworkingSockets_DestroyPollGroup(get_interface(), self.poll_group.0);
        }
        self.global
            .event_queues
            .write()
            .unwrap()
            .remove(&self.queue_id);
    }
}

impl IsReady for IsServer {
    #[inline]
    fn queue(&self) -> &SegQueue<GnsConnectionEvent> {
        &self.queue
    }

    fn receive(&self, slots: &mut [MaybeUninit<*mut ISteamNetworkingMessage>]) -> GnsResult<usize> {
        let result = unsafe {
            SteamAPI_ISteamNetworkingSockets_ReceiveMessagesOnPollGroup(
                get_interface(),
                self.poll_group.0,
                slots.as_mut_ptr() as _,
                slots.len() as _,
            ) as _
        };
        if result == usize::MAX {
            Err(GnsError::Receive)
        } else {
            Ok(result)
        }
    }
}

/// The state of a [`GnsSocket`] that acts as a client, normally reached
/// through [`GnsSocket::connect`].
///
/// In this state the socket holds what it needs to send and receive messages.
pub struct IsClient {
    queue: Arc<SegQueue<GnsConnectionEvent>>,
    queue_id: i64,
    global: &'static GnsGlobal,
    connection: GnsConnection,
}

impl Drop for IsClient {
    fn drop(&mut self) {
        unsafe {
            SteamAPI_ISteamNetworkingSockets_CloseConnection(
                get_interface(),
                self.connection.0,
                0,
                core::ptr::null(),
                false,
            );
        }
        self.global
            .event_queues
            .write()
            .unwrap()
            .remove(&self.queue_id);
    }
}

impl IsReady for IsClient {
    #[inline]
    fn queue(&self) -> &SegQueue<GnsConnectionEvent> {
        &self.queue
    }

    fn receive(&self, slots: &mut [MaybeUninit<*mut ISteamNetworkingMessage>]) -> GnsResult<usize> {
        let result = unsafe {
            SteamAPI_ISteamNetworkingSockets_ReceiveMessagesOnConnection(
                get_interface(),
                self.connection.0,
                slots.as_mut_ptr() as _,
                slots.len() as _,
            ) as _
        };
        if result == usize::MAX {
            Err(GnsError::Receive)
        } else {
            Ok(result)
        }
    }
}

pub struct ToReceive(());

pub struct ToSend(());

/// A single receive slot.
///
/// Each slot is an uninitialized cell that GameNetworkingSockets fills with one
/// `*mut ISteamNetworkingMessage`. Build a buffer of slots, for example
/// `[const { MessageSlot::uninit() }; 128]`, and pass it to
/// [`GnsSocket::receive_messages_into`].
pub type MessageSlot = MaybeUninit<*mut ISteamNetworkingMessage>;

/// Rebuilds the owned message stored in `slot`.
///
/// # Safety
/// GameNetworkingSockets must have initialized `slot`, meaning the slot lies
/// within the prefix length that `receive` reported. The slot must not have
/// been taken already, otherwise the message is released more than once.
#[inline]
unsafe fn take_message(slot: &MessageSlot) -> GnsNetworkMessage<ToReceive> {
    GnsNetworkMessage(unsafe { slot.assume_init() }, PhantomData)
}

/// Tracks progress through a buffer of receive slots.
///
/// The slots in `slots[..len]` are initialized, and `pos` is the next slot to
/// hand out. This type holds the unsafe take and release logic in one place so
/// that the owning and borrowing iterators cannot drift apart.
struct SlotCursor {
    len: usize,
    pos: usize,
}

impl SlotCursor {
    fn next(&mut self, slots: &[MessageSlot]) -> Option<GnsNetworkMessage<ToReceive>> {
        if self.pos < self.len {
            // Safety: GameNetworkingSockets initialized `slots[..len]`, and
            // `pos` only increases, so each slot is taken at most once.
            let message = unsafe { take_message(&slots[self.pos]) };
            self.pos += 1;
            Some(message)
        } else {
            None
        }
    }

    #[inline]
    fn remaining(&self) -> usize {
        self.len - self.pos
    }

    /// Releases every slot that was not handed out. Safe to call more than
    /// once.
    fn drain_unconsumed(&mut self, slots: &[MessageSlot]) {
        for slot in &slots[self.pos..self.len] {
            // Safety: same invariant as `next`. These slots are initialized
            // and were never handed out, so each is released exactly once.
            drop(unsafe { take_message(slot) });
        }
        self.pos = self.len;
    }
}

/// An iterator over the messages from one [`GnsSocket::receive_messages`]
/// call.
///
/// The iterator owns its `K`-slot pointer buffer inline, so it performs no heap
/// allocation. It yields each [`GnsNetworkMessage<ToReceive>`] by value, and
/// releases any message you did not consume when it is dropped.
///
/// See [`GnsSocket::receive_messages_into`] for a variant that borrows a buffer
/// you own, which also avoids moving the inline array.
pub struct ReceivedMessages<const K: usize> {
    slots: [MessageSlot; K],
    cursor: SlotCursor,
}

impl<const K: usize> Iterator for ReceivedMessages<K> {
    type Item = GnsNetworkMessage<ToReceive>;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        self.cursor.next(&self.slots)
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        let remaining = self.cursor.remaining();
        (remaining, Some(remaining))
    }
}

impl<const K: usize> ExactSizeIterator for ReceivedMessages<K> {}

impl<const K: usize> core::iter::FusedIterator for ReceivedMessages<K> {}

impl<const K: usize> Drop for ReceivedMessages<K> {
    #[inline]
    fn drop(&mut self) {
        self.cursor.drain_unconsumed(&self.slots);
    }
}

/// An iterator returned by [`GnsSocket::receive_messages_into`].
///
/// The iterator borrows your buffer for its whole lifetime, so you cannot reuse
/// the buffer while messages are still outstanding. It yields each
/// [`GnsNetworkMessage<ToReceive>`] by value.
///
/// Nothing is allocated and the pointer buffer never moves. Only the individual
/// message pointers move. Any message you did not consume is released when the
/// iterator is dropped.
pub struct ReceivedMessagesInto<'a> {
    slots: &'a mut [MessageSlot],
    cursor: SlotCursor,
}

impl Iterator for ReceivedMessagesInto<'_> {
    type Item = GnsNetworkMessage<ToReceive>;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        self.cursor.next(self.slots)
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        let remaining = self.cursor.remaining();
        (remaining, Some(remaining))
    }
}

impl ExactSizeIterator for ReceivedMessagesInto<'_> {}

impl core::iter::FusedIterator for ReceivedMessagesInto<'_> {}

impl Drop for ReceivedMessagesInto<'_> {
    #[inline]
    fn drop(&mut self) {
        self.cursor.drain_unconsumed(self.slots);
    }
}

bitflags::bitflags! {
    /// A type-safe wrapper over the `k_nSteamNetworkingSend_*` flags.
    ///
    /// The bit values match the raw `c_int` constants.
    #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
    pub struct SendFlags: i32 {
        const UNRELIABLE                  = sys::k_nSteamNetworkingSend_Unreliable;
        const NO_NAGLE                    = sys::k_nSteamNetworkingSend_NoNagle;
        const NO_DELAY                    = sys::k_nSteamNetworkingSend_NoDelay;
        const RELIABLE                    = sys::k_nSteamNetworkingSend_Reliable;
        const USE_CURRENT_THREAD          = sys::k_nSteamNetworkingSend_UseCurrentThread;
        const AUTO_RESTART_BROKEN_SESSION = sys::k_nSteamNetworkingSend_AutoRestartBrokenSession;
    }
}

/// A connection lane.
///
/// `priority` is a signed C `int` where a lower value means a higher priority.
/// `weight` is the relative scheduling weight within one priority class.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct GnsLane {
    pub priority: i32,
    pub weight: u16,
}

impl GnsLane {
    #[inline]
    pub const fn new(priority: i32, weight: u16) -> Self {
        Self { priority, weight }
    }
}

/// A lane identifier.
pub type GnsLaneId = u16;

/// The result of one message in a [`GnsSocket::send_messages`] batch.
///
/// `Skipped` mirrors how GameNetworkingSockets handles a failed batch. Once a
/// message fails on a connection, every later message in the same batch that
/// targets that connection is skipped without being attempted, and its result
/// is reported as `0`. A skipped message keeps its payload, so the wrapper
/// returns it to you, the same way it returns a `Failed` message.
#[must_use = "Failed/Skipped variants own a message that needs inspection or drop"]
pub enum SendOutcome {
    Sent(GnsMessageNumber),
    Failed(EResult, GnsNetworkMessage<ToSend>),
    Skipped(GnsNetworkMessage<ToSend>),
}

/// An owned byte buffer for an outbound message.
///
/// GameNetworkingSockets reads `m_pData` on its service thread after
/// `SendMessages` returns, so a message must own its bytes until
/// GameNetworkingSockets releases it.
///
/// [`into_raw`](Self::into_raw) returns a `(pointer, length)` pair that the
/// wrapper stores unchanged in `m_pData` and `m_cbSize`. When
/// GameNetworkingSockets releases the message, the wrapper passes those same
/// values to [`from_raw`](Self::from_raw) to rebuild `Self`, then drops the
/// result.
///
/// This mirrors `Box::into_raw` and `Box::from_raw`, so you can express how the
/// buffer is freed with an ordinary Rust `Drop` implementation.
///
/// # Safety
/// `from_raw(p, n)` must be sound whenever `(p, n)` came from an earlier
/// `into_raw` call on the same implementation. In other words, `from_raw` must
/// undo `into_raw` exactly.
///
/// `into_raw` must not run the `Drop` implementation of `Self`, because
/// ownership passes to GameNetworkingSockets.
pub unsafe trait Payload: Send + 'static {
    fn into_raw(self) -> (*mut u8, usize);
    /// # Safety
    /// `ptr` and `len` must be the values that an earlier
    /// [`into_raw`](Self::into_raw) call on this same implementation returned,
    /// and that ownership must not have been reclaimed already.
    unsafe fn from_raw(ptr: *mut u8, len: usize) -> Self;
}

/// The `m_pfnFreeData` callback that the wrapper installs on every
/// `GnsNetworkMessage<ToSend>`.
///
/// It reads `m_pData` and `m_cbSize`, rebuilds `P` with
/// [`Payload::from_raw`], and drops the result.
extern "C" fn free_payload<P: Payload>(msg: *mut ISteamNetworkingMessage) {
    let ptr = unsafe { (*msg).m_pData } as *mut u8;
    let len = unsafe { (*msg).m_cbSize } as usize;
    // Safety: `GnsNetworkMessage::<ToSend>::new` wrote `ptr` and `len` from
    // `P::into_raw`, and GameNetworkingSockets releases each message once.
    drop(unsafe { P::from_raw(ptr, len) });
}

unsafe impl Payload for Box<[u8]> {
    #[inline]
    fn into_raw(self) -> (*mut u8, usize) {
        let len = self.len();
        let raw = Box::into_raw(self) as *mut u8;
        (raw, len)
    }
    #[inline]
    unsafe fn from_raw(ptr: *mut u8, len: usize) -> Self {
        let slice = core::ptr::slice_from_raw_parts_mut(ptr, len);
        unsafe { Box::from_raw(slice) }
    }
}

// This goes through `Box<[u8]>`. `into_boxed_slice` shrinks the buffer to fit,
// which costs one reallocation when the capacity differs from the length, so
// the pointer and length are enough to rebuild the value.
unsafe impl Payload for Vec<u8> {
    #[inline]
    fn into_raw(self) -> (*mut u8, usize) {
        <Box<[u8]> as Payload>::into_raw(self.into_boxed_slice())
    }
    #[inline]
    unsafe fn from_raw(ptr: *mut u8, len: usize) -> Self {
        unsafe { Vec::from_raw_parts(ptr, len, len) }
    }
}

unsafe impl Payload for String {
    #[inline]
    fn into_raw(self) -> (*mut u8, usize) {
        <Vec<u8> as Payload>::into_raw(self.into_bytes())
    }
    #[inline]
    unsafe fn from_raw(ptr: *mut u8, len: usize) -> Self {
        unsafe { String::from_raw_parts(ptr, len, len) }
    }
}

unsafe impl Payload for Arc<[u8]> {
    #[inline]
    fn into_raw(self) -> (*mut u8, usize) {
        let len = self.len();
        let raw = Arc::into_raw(self) as *const u8 as *mut u8;
        (raw, len)
    }
    #[inline]
    unsafe fn from_raw(ptr: *mut u8, len: usize) -> Self {
        let slice = core::ptr::slice_from_raw_parts(ptr as *const u8, len);
        unsafe { Arc::from_raw(slice) }
    }
}

unsafe impl Payload for &'static [u8] {
    #[inline]
    fn into_raw(self) -> (*mut u8, usize) {
        (self.as_ptr() as *mut u8, self.len())
    }
    #[inline]
    unsafe fn from_raw(ptr: *mut u8, len: usize) -> Self {
        unsafe { core::slice::from_raw_parts(ptr as *const u8, len) }
    }
}

unsafe impl Payload for &'static str {
    #[inline]
    fn into_raw(self) -> (*mut u8, usize) {
        (self.as_ptr() as *mut u8, self.len())
    }
    #[inline]
    unsafe fn from_raw(ptr: *mut u8, len: usize) -> Self {
        let bytes = unsafe { core::slice::from_raw_parts(ptr as *const u8, len) };
        unsafe { core::str::from_utf8_unchecked(bytes) }
    }
}

/// A GameNetworkingSockets message, tagged with its direction.
///
/// The library produces `ToReceive` messages. You create `ToSend` messages with
/// [`GnsUtils::allocate_message`], and they own their payload through
/// [`Payload`]. Both kinds are released when dropped.
#[repr(transparent)]
pub struct GnsNetworkMessage<T>(*mut ISteamNetworkingMessage, PhantomData<T>);

impl<T> Drop for GnsNetworkMessage<T> {
    #[inline]
    fn drop(&mut self) {
        if !self.0.is_null() {
            unsafe {
                SteamAPI_SteamNetworkingMessage_t_Release(self.0);
            }
        }
    }
}

impl<T> GnsNetworkMessage<T> {
    /// Returns the raw `*mut ISteamNetworkingMessage` and forgets the wrapper.
    ///
    /// # Safety
    /// You take over releasing the message, for example by calling
    /// `SteamAPI_SteamNetworkingMessage_t_Release`. For a `ToSend` message that
    /// release also runs the `m_pfnFreeData` callback that [`Payload`]
    /// installed.
    #[inline]
    pub unsafe fn into_inner(self) -> *mut ISteamNetworkingMessage {
        // Hold off the wrapper's destructor. Releasing the message is now
        // the caller's job. Without this the message would be released here
        // and the returned pointer would dangle.
        core::mem::ManuallyDrop::new(self).0
    }

    #[inline]
    pub fn payload(&self) -> &[u8] {
        unsafe {
            core::slice::from_raw_parts((*self.0).m_pData as *const u8, (*self.0).m_cbSize as _)
        }
    }

    #[inline]
    pub fn message_number(&self) -> u64 {
        unsafe { (*self.0).m_nMessageNumber as _ }
    }

    /// Returns the local timestamp at which the message arrived, in
    /// microseconds.
    ///
    /// The value shares its timebase with [`GnsUtils::local_timestamp`], so
    /// compare the two to compute how long a message sat in the receive queue.
    /// It is meaningless on a message you allocated yourself.
    #[inline]
    pub fn time_received(&self) -> SteamNetworkingMicroseconds {
        unsafe { (*self.0).m_usecTimeReceived }
    }

    #[inline]
    pub fn lane(&self) -> GnsLaneId {
        unsafe { (*self.0).m_idxLane }
    }

    #[inline]
    pub fn flags(&self) -> SendFlags {
        SendFlags::from_bits_retain(unsafe { (*self.0).m_nFlags })
    }

    #[inline]
    pub fn user_data(&self) -> u64 {
        unsafe { (*self.0).m_nUserData as _ }
    }

    #[inline]
    pub fn connection(&self) -> GnsConnection {
        GnsConnection(unsafe { (*self.0).m_conn })
    }

    #[inline]
    pub fn connection_user_data(&self) -> u64 {
        unsafe { (*self.0).m_nConnUserData as _ }
    }
}

impl GnsNetworkMessage<ToSend> {
    #[inline]
    fn new<P: Payload>(
        ptr: *mut ISteamNetworkingMessage,
        conn: GnsConnection,
        flags: SendFlags,
        payload: P,
    ) -> Self {
        let (data_ptr, len) = payload.into_raw();
        unsafe {
            (*ptr).m_pData = data_ptr as *mut c_void;
            (*ptr).m_cbSize = len as i32;
            (*ptr).m_pfnFreeData = Some(free_payload::<P>);
        }
        GnsNetworkMessage(ptr, PhantomData)
            .set_flags(flags)
            .set_connection(conn)
    }

    #[inline]
    pub fn set_connection(self, GnsConnection(conn): GnsConnection) -> Self {
        unsafe { (*self.0).m_conn = conn }
        self
    }

    #[inline]
    pub fn set_lane(self, lane: GnsLaneId) -> Self {
        unsafe { (*self.0).m_idxLane = lane }
        self
    }

    #[inline]
    pub fn set_flags(self, flags: SendFlags) -> Self {
        unsafe { (*self.0).m_nFlags = flags.bits() as _ }
        self
    }

    #[inline]
    pub fn set_user_data(self, userdata: u64) -> Self {
        unsafe { (*self.0).m_nUserData = userdata as _ }
        self
    }
}

#[repr(transparent)]
#[derive(Default, Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct GnsConnection(HSteamNetConnection);

impl GnsConnection {
    /// Wraps a raw `HSteamNetConnection` handle.
    ///
    /// GameNetworkingSockets validates the handle when you use it. It rejects
    /// any handle that does not match a live connection.
    #[inline]
    pub const fn from_raw(handle: HSteamNetConnection) -> Self {
        Self(handle)
    }

    /// Returns `true` if this is not the invalid-connection value (`0`).
    #[inline]
    pub fn is_valid(self) -> bool {
        self.0 != k_HSteamNetConnection_Invalid
    }
}

#[derive(Default, Copy, Clone)]
pub struct GnsConnectionInfo(SteamNetConnectionInfo_t);

impl GnsConnectionInfo {
    #[inline]
    pub fn state(&self) -> ESteamNetworkingConnectionState {
        self.0.m_eState
    }

    #[inline]
    pub fn end_reason(&self) -> u32 {
        self.0.m_eEndReason as u32
    }

    #[inline]
    pub fn end_debug(&self) -> &str {
        unsafe { CStr::from_ptr(self.0.m_szEndDebug.as_ptr()) }
            .to_str()
            .unwrap_or("")
    }

    #[inline]
    pub fn remote_address(&self) -> IpAddr {
        ip_from_steam_ip_addr(&self.0.m_addrRemote)
    }

    #[inline]
    pub fn remote_port(&self) -> u16 {
        self.0.m_addrRemote.m_port
    }
}

#[derive(Debug, Default, Copy, Clone, Hash, PartialOrd, Ord, PartialEq, Eq)]
pub struct GnsConnectionRealTimeLaneStatus(SteamNetConnectionRealTimeLaneStatus_t);

impl GnsConnectionRealTimeLaneStatus {
    #[inline]
    pub fn pending_bytes_unreliable(&self) -> u32 {
        self.0.m_cbPendingUnreliable as _
    }

    #[inline]
    pub fn pending_bytes_reliable(&self) -> u32 {
        self.0.m_cbPendingReliable as _
    }

    #[inline]
    pub fn bytes_sent_unacked_reliable(&self) -> u32 {
        self.0.m_cbSentUnackedReliable as _
    }

    #[inline]
    pub fn approximated_queue_time(&self) -> Duration {
        Duration::from_micros(self.0.m_usecQueueTime as _)
    }
}

#[derive(Default, Debug, Copy, Clone, PartialOrd, PartialEq)]
pub struct GnsConnectionRealTimeStatus(SteamNetConnectionRealTimeStatus_t);

impl GnsConnectionRealTimeStatus {
    #[inline]
    pub fn state(&self) -> ESteamNetworkingConnectionState {
        self.0.m_eState
    }

    #[inline]
    pub fn ping(&self) -> u32 {
        self.0.m_nPing as _
    }

    #[inline]
    pub fn quality_local(&self) -> f32 {
        self.0.m_flConnectionQualityLocal
    }

    #[inline]
    pub fn quality_remote(&self) -> f32 {
        self.0.m_flConnectionQualityRemote
    }

    #[inline]
    pub fn out_packets_per_sec(&self) -> f32 {
        self.0.m_flOutPacketsPerSec
    }

    #[inline]
    pub fn out_bytes_per_sec(&self) -> f32 {
        self.0.m_flOutBytesPerSec
    }

    #[inline]
    pub fn in_packets_per_sec(&self) -> f32 {
        self.0.m_flInPacketsPerSec
    }

    #[inline]
    pub fn in_bytes_per_sec(&self) -> f32 {
        self.0.m_flInBytesPerSec
    }

    #[inline]
    pub fn send_rate_bytes_per_sec(&self) -> u32 {
        self.0.m_nSendRateBytesPerSecond as _
    }

    #[inline]
    pub fn pending_bytes_unreliable(&self) -> u32 {
        self.0.m_cbPendingUnreliable as _
    }

    #[inline]
    pub fn pending_bytes_reliable(&self) -> u32 {
        self.0.m_cbPendingReliable as _
    }

    #[inline]
    pub fn bytes_sent_unacked_reliable(&self) -> u32 {
        self.0.m_cbSentUnackedReliable as _
    }

    #[inline]
    pub fn approximated_queue_time(&self) -> Duration {
        Duration::from_micros(self.0.m_usecQueueTime as _)
    }

    /// Returns the highest packet jitter seen since the last time you read this
    /// value. Reading it clears the high water mark.
    ///
    /// Returns `None` if no jitter data is available, which happens when the
    /// underlying value is negative or the connection type does not measure
    /// jitter.
    #[inline]
    pub fn max_jitter_usec(&self) -> Option<i32> {
        let val = self.0.m_usecMaxJitter;
        if val < 0 {
            None
        } else {
            Some(val)
        }
    }
}

#[derive(Default, Copy, Clone)]
pub struct GnsConnectionEvent(SteamNetConnectionStatusChangedCallback_t);

impl GnsConnectionEvent {
    #[inline]
    pub fn old_state(&self) -> ESteamNetworkingConnectionState {
        self.0.m_eOldState
    }

    #[inline]
    pub fn connection(&self) -> GnsConnection {
        GnsConnection(self.0.m_hConn)
    }

    #[inline]
    pub fn info(&self) -> GnsConnectionInfo {
        GnsConnectionInfo(self.0.m_info)
    }
}

/// A network socket, and the main type of this library.
///
/// Use [`GnsSocket::connect`] to create a client socket and
/// [`GnsSocket::listen`] to create a server socket. Every operation on a socket
/// is safe.
///
/// Dropping a socket frees everything that belongs to it. It does not free the
/// [`GnsGlobal`] instance.
pub struct GnsSocket<S> {
    global: &'static GnsGlobal,
    state: S,
}

impl<S> GnsSocket<S>
where
    S: IsReady,
{
    /// Returns the status of a connection and of its lanes.
    ///
    /// Configure the lanes with [`Self::configure_connection_lanes`] before you
    /// call this.
    pub fn get_connection_real_time_status(
        &self,
        GnsConnection(conn): GnsConnection,
        nb_of_lanes: u32,
    ) -> GnsResult<(
        GnsConnectionRealTimeStatus,
        Vec<GnsConnectionRealTimeLaneStatus>,
    )> {
        let mut lanes: Vec<GnsConnectionRealTimeLaneStatus> =
            vec![Default::default(); nb_of_lanes as _];
        let mut status: GnsConnectionRealTimeStatus = Default::default();
        check(unsafe {
            SteamAPI_ISteamNetworkingSockets_GetConnectionRealTimeStatus(
                get_interface(),
                conn,
                &mut status as *mut GnsConnectionRealTimeStatus
                    as *mut SteamNetConnectionRealTimeStatus_t,
                nb_of_lanes as _,
                lanes.as_mut_ptr() as *mut SteamNetConnectionRealTimeLaneStatus_t,
            )
        })?;
        Ok((status, lanes))
    }

    pub fn get_connection_info(
        &self,
        GnsConnection(conn): GnsConnection,
    ) -> Option<GnsConnectionInfo> {
        let mut info: SteamNetConnectionInfo_t = Default::default();
        if unsafe {
            SteamAPI_ISteamNetworkingSockets_GetConnectionInfo(get_interface(), conn, &mut info)
        } {
            Some(GnsConnectionInfo(info))
        } else {
            None
        }
    }

    /// Returns a verbose human-readable description of the state of a
    /// connection, intended for diagnostics and debug dumps.
    ///
    /// The format is subject to change between GameNetworkingSockets versions,
    /// so do not parse it. Returns `None` if the connection handle is invalid.
    pub fn get_detailed_connection_status(
        &self,
        GnsConnection(conn): GnsConnection,
    ) -> Option<String> {
        let mut buf = vec![0u8; 2048];
        loop {
            let result = unsafe {
                SteamAPI_ISteamNetworkingSockets_GetDetailedConnectionStatus(
                    get_interface(),
                    conn,
                    buf.as_mut_ptr() as *mut std::ffi::c_char,
                    buf.len() as _,
                )
            };
            if result < 0 {
                return None;
            }
            if result == 0 {
                let text = CStr::from_bytes_until_nul(&buf).ok()?;
                return Some(text.to_string_lossy().into_owned());
            }
            // The buffer was too small and `result` is the required size.
            buf.resize(result as usize, 0);
        }
    }

    /// Returns the debug name of a connection, previously set with
    /// [`Self::set_connection_name`].
    ///
    /// Returns `None` if the connection handle is invalid.
    pub fn get_connection_name(&self, GnsConnection(conn): GnsConnection) -> Option<String> {
        let mut buf = [0u8; 256];
        if unsafe {
            SteamAPI_ISteamNetworkingSockets_GetConnectionName(
                get_interface(),
                conn,
                buf.as_mut_ptr() as *mut std::ffi::c_char,
                buf.len() as _,
            )
        } {
            let name = CStr::from_bytes_until_nul(&buf).ok()?;
            Some(name.to_string_lossy().into_owned())
        } else {
            None
        }
    }

    /// Sets the debug name of a connection.
    ///
    /// The name shows up in diagnostics such as
    /// [`Self::get_detailed_connection_status`] and the debug output, which
    /// makes multi-connection logs much easier to read.
    ///
    /// # Errors
    /// Returns [`GnsError::Config`] if `name` contains an interior NUL byte.
    pub fn set_connection_name(
        &self,
        GnsConnection(conn): GnsConnection,
        name: &str,
    ) -> GnsResult<()> {
        let c = CString::new(name).map_err(|_| GnsError::Config("interior NUL"))?;
        unsafe {
            SteamAPI_ISteamNetworkingSockets_SetConnectionName(get_interface(), conn, c.as_ptr());
        }
        Ok(())
    }

    pub fn flush_messages_on_connection(
        &self,
        GnsConnection(conn): GnsConnection,
    ) -> GnsResult<()> {
        check(unsafe {
            SteamAPI_ISteamNetworkingSockets_FlushMessagesOnConnection(get_interface(), conn)
        })
    }

    /// Closes a connection.
    ///
    /// The wrapper forwards `debug` to the peer when you pass `Some`. Pass
    /// `None` to send no diagnostic string and avoid allocating.
    ///
    /// # Errors
    /// Returns [`GnsError::Close`] if the connection handle is invalid, for
    /// example because the connection is already closed.
    pub fn close_connection(
        &self,
        GnsConnection(conn): GnsConnection,
        reason: u32,
        debug: Option<&CStr>,
        linger: bool,
    ) -> GnsResult<()> {
        let debug_ptr = debug.map(|d| d.as_ptr()).unwrap_or(core::ptr::null());
        if unsafe {
            SteamAPI_ISteamNetworkingSockets_CloseConnection(
                get_interface(),
                conn,
                reason as _,
                debug_ptr,
                linger,
            )
        } {
            Ok(())
        } else {
            Err(GnsError::Close)
        }
    }

    /// Receives up to `K` messages and returns an iterator over the ones that
    /// were available.
    ///
    /// Each message is yielded by value, so you can keep it, forward it, or let
    /// it drop, which releases it. Any message left in the iterator is released
    /// when the iterator is dropped.
    ///
    /// The `K`-slot pointer buffer lives inline in the returned iterator, so
    /// this call allocates nothing and copies no payload. Use
    /// [`receive_messages_into`](Self::receive_messages_into) to reuse one
    /// buffer across calls and avoid moving the inline array.
    ///
    /// # Errors
    /// Returns [`GnsError::Receive`] if the connection or poll group handle is
    /// invalid.
    pub fn receive_messages<const K: usize>(&self) -> GnsResult<ReceivedMessages<K>> {
        let mut slots: [MessageSlot; K] = [const { MessageSlot::uninit() }; K];
        let len = self.state.receive(&mut slots)?;
        Ok(ReceivedMessages {
            slots,
            cursor: SlotCursor { len, pos: 0 },
        })
    }

    /// Receives up to `buffer.len()` messages into a buffer you own, and
    /// returns an iterator over the ones that were available.
    ///
    /// This is the variant of [`receive_messages`](Self::receive_messages) that
    /// neither allocates nor moves the buffer. GameNetworkingSockets fills
    /// `buffer` in place and the returned iterator borrows it, so reusing one
    /// buffer across a polling loop costs nothing per call.
    ///
    /// # Errors
    /// Returns [`GnsError::Receive`] if the connection or poll group handle is
    /// invalid.
    pub fn receive_messages_into<'a>(
        &self,
        buffer: &'a mut [MessageSlot],
    ) -> GnsResult<ReceivedMessagesInto<'a>> {
        let len = self.state.receive(buffer)?;
        Ok(ReceivedMessagesInto {
            slots: buffer,
            cursor: SlotCursor { len, pos: 0 },
        })
    }

    /// Returns an iterator that drains the pending connection events.
    ///
    /// Unlike [`receive_messages`](Self::receive_messages), you supply no
    /// buffer. Events arrive on an internal lock-free queue that the
    /// connection-status callback fills, and this call pops from that queue.
    pub fn receive_events(&self) -> impl Iterator<Item = GnsConnectionEvent> + '_ {
        core::iter::from_fn(|| self.state.queue().pop())
    }

    pub fn configure_connection_lanes(
        &self,
        GnsConnection(connection): GnsConnection,
        lanes: &[GnsLane],
    ) -> GnsResult<()> {
        let (priorities, weights): (Vec<i32>, Vec<u16>) =
            lanes.iter().map(|l| (l.priority, l.weight)).unzip();
        check(unsafe {
            SteamAPI_ISteamNetworkingSockets_ConfigureConnectionLanes(
                get_interface(),
                connection,
                lanes.len() as _,
                priorities.as_ptr(),
                weights.as_ptr(),
            )
        })
    }

    /// Sends a single message to its target connection.
    ///
    /// This is a convenience wrapper over
    /// [`send_messages`](Self::send_messages) for the common one-message case.
    pub fn send_message(&self, message: GnsNetworkMessage<ToSend>) -> GnsResult<GnsMessageNumber> {
        match self.send_messages(core::iter::once(message)).pop() {
            Some(SendOutcome::Sent(number)) => Ok(number),
            Some(SendOutcome::Failed(result, _)) => Err(GnsError::Api(result)),
            // A single message is never `Skipped`, because that only happens
            // to a message queued behind an earlier failure on the same
            // connection. `send_messages` also returns one outcome per input.
            _ => Err(GnsError::Api(EResult::k_EResultFail)),
        }
    }

    /// Sends each message to its target connection.
    ///
    /// The returned `Vec` holds one [`SendOutcome`] per input message, in the
    /// same order.
    pub fn send_messages(
        &self,
        messages: impl IntoIterator<Item = GnsNetworkMessage<ToSend>>,
    ) -> Vec<SendOutcome> {
        // Pass `bDeleteFailedMessages = false` so that the C library consumes
        // the messages it sends and leaves the failed and skipped ones for the
        // wrapper to wrap again. `ManuallyDrop` holds off the Rust destructor
        // across the FFI call.
        let mut raw: Vec<*mut ISteamNetworkingMessage> = messages
            .into_iter()
            .map(|message| {
                let message = core::mem::ManuallyDrop::new(message);
                message.0
            })
            .collect();
        let mut result = vec![0i64; raw.len()];
        unsafe {
            SteamAPI_ISteamNetworkingSockets_SendMessages(
                get_interface(),
                raw.len() as _,
                raw.as_mut_ptr(),
                result.as_mut_ptr(),
                false,
            );
        }
        result
            .into_iter()
            .zip(raw)
            .map(|(value, ptr)| {
                if value > 0 {
                    SendOutcome::Sent(value as _)
                } else if value < 0 {
                    // Sound because gns-sys pins GameNetworkingSockets as a
                    // submodule, so the generated `EResult` covers every value
                    // the library produces.
                    let result = unsafe { core::mem::transmute::<u32, EResult>((-value) as u32) };
                    SendOutcome::Failed(result, GnsNetworkMessage(ptr, PhantomData))
                } else {
                    SendOutcome::Skipped(GnsNetworkMessage(ptr, PhantomData))
                }
            })
            .collect()
    }
}

impl GnsSocket<IsCreated> {
    /// The C callback that GameNetworkingSockets invokes on a connection-state
    /// change.
    ///
    /// The wrapper stores the queue ID in the connection user data, which is
    /// how this callback finds the right queue in [`GnsGlobal`].
    unsafe extern "C" fn on_connection_state_changed(
        info: &mut SteamNetConnectionStatusChangedCallback_t,
    ) {
        let gns_global = GnsGlobal::get()
            // Reaching this point at all means GnsGlobal is initialized.
            .expect("GnsGlobal should be initialized");

        let queue_id = info.m_info.m_nUserData as _;
        // Fast path: take the read lock, look up the queue, and push if the
        // weak reference still upgrades.
        let needs_purge = {
            let queues = gns_global.event_queues.read().unwrap();
            match queues.get(&queue_id).and_then(Weak::upgrade) {
                Some(queue) => {
                    queue.push(GnsConnectionEvent(*info));
                    false
                }
                None => queues.contains_key(&queue_id),
            }
        };
        // Slow path: the socket was dropped while this callback ran, so the
        // entry is still in the map but the queue is gone. Take the write lock
        // to remove it. Queue IDs are never reused, so removing a key that
        // another thread already removed does no harm.
        if needs_purge {
            gns_global.event_queues.write().unwrap().remove(&queue_id);
        }
    }

    /// Creates a socket in the [`IsCreated`] state.
    #[inline]
    pub fn new(global: &'static GnsGlobal) -> Self {
        GnsSocket {
            global,
            state: IsCreated,
        }
    }

    fn setup_common(
        address: IpAddr,
        port: u16,
        queue_id: int64,
    ) -> (SteamNetworkingIPAddr, [SteamNetworkingConfigValue_t; 2]) {
        let addr = SteamNetworkingIPAddr {
            __bindgen_anon_1: match address {
                IpAddr::V4(address) => SteamNetworkingIPAddr__bindgen_ty_2 {
                    m_ipv4: SteamNetworkingIPAddr_IPv4MappedAddress {
                        m_8zeros: 0,
                        m_0000: 0,
                        m_ffff: 0xffff,
                        m_ip: address.octets(),
                    },
                },
                IpAddr::V6(address) => SteamNetworkingIPAddr__bindgen_ty_2 {
                    m_ipv6: address.octets(),
                },
            },
            m_port: port,
        };
        let options = [SteamNetworkingConfigValue_t {
            m_eDataType: ESteamNetworkingConfigDataType::k_ESteamNetworkingConfig_Ptr,
            m_eValue: ESteamNetworkingConfigValue::k_ESteamNetworkingConfig_Callback_ConnectionStatusChanged,
            m_val: SteamNetworkingConfigValue_t__bindgen_ty_1 {
              m_ptr: Self::on_connection_state_changed as *const fn(&SteamNetConnectionStatusChangedCallback_t) as *mut c_void
            }
          }, SteamNetworkingConfigValue_t {
            m_eDataType: ESteamNetworkingConfigDataType::k_ESteamNetworkingConfig_Int64,
            m_eValue: ESteamNetworkingConfigValue::k_ESteamNetworkingConfig_ConnectionUserData,
            m_val: SteamNetworkingConfigValue_t__bindgen_ty_1 {
              m_int64: queue_id
            }
        }];
        (addr, options)
    }

    /// Listens for incoming connections.
    ///
    /// This moves the socket from [`IsCreated`] to [`IsServer`], which gives
    /// you the server operations.
    pub fn listen(self, address: IpAddr, port: u16) -> GnsResult<GnsSocket<IsServer>> {
        let (queue_id, queue) = self.global.create_queue();
        let (addr, options) = Self::setup_common(address, port, queue_id);
        let listen_socket = unsafe {
            SteamAPI_ISteamNetworkingSockets_CreateListenSocketIP(
                get_interface(),
                &addr,
                options.len() as _,
                options.as_ptr(),
            )
        };
        if listen_socket == k_HSteamListenSocket_Invalid {
            Err(GnsError::Listen)
        } else {
            let poll_group =
                unsafe { SteamAPI_ISteamNetworkingSockets_CreatePollGroup(get_interface()) };
            if poll_group == k_HSteamNetPollGroup_Invalid {
                Err(GnsError::Listen)
            } else {
                Ok(GnsSocket {
                    global: self.global,
                    state: IsServer {
                        queue,
                        queue_id,
                        global: self.global,
                        listen_socket: GnsListenSocket(listen_socket),
                        poll_group: GnsPollGroup(poll_group),
                    },
                })
            }
        }
    }

    /// Connects to a remote host.
    ///
    /// This moves the socket from [`IsCreated`] to [`IsClient`], which gives
    /// you the client operations.
    pub fn connect(self, address: IpAddr, port: u16) -> GnsResult<GnsSocket<IsClient>> {
        let (queue_id, queue) = self.global.create_queue();
        let (addr, options) = Self::setup_common(address, port, queue_id);
        let connection = unsafe {
            SteamAPI_ISteamNetworkingSockets_ConnectByIPAddress(
                get_interface(),
                &addr,
                options.len() as _,
                options.as_ptr(),
            )
        };
        if connection == k_HSteamNetConnection_Invalid {
            Err(GnsError::Connect)
        } else {
            Ok(GnsSocket {
                global: self.global,
                state: IsClient {
                    queue,
                    queue_id,
                    global: self.global,
                    connection: GnsConnection(connection),
                },
            })
        }
    }

    /// Creates a pair of connections that talk to each other, mainly for
    /// tests and loopback communication between parts of one process.
    ///
    /// With `use_network_loopback` set, the traffic goes through the local
    /// network stack over `127.0.0.1`. Without it, the payloads take an
    /// internal shortcut. See `ISteamNetworkingSockets::CreateSocketPair` for
    /// the trade-offs.
    ///
    /// Both sockets come back in the [`IsClient`] state and already connected.
    /// The connections are created before the wrapper can attach its
    /// connection-state callback, so the initial transition to the connected
    /// state never shows up in [`GnsSocket::receive_events`]. Later events,
    /// such as the peer closing the connection, are delivered normally.
    pub fn socket_pair(
        self,
        use_network_loopback: bool,
    ) -> GnsResult<(GnsSocket<IsClient>, GnsSocket<IsClient>)> {
        let (queue_id_a, queue_a) = self.global.create_queue();
        let (queue_id_b, queue_b) = self.global.create_queue();
        let mut conn_a = k_HSteamNetConnection_Invalid;
        let mut conn_b = k_HSteamNetConnection_Invalid;
        let ok = unsafe {
            SteamAPI_ISteamNetworkingSockets_CreateSocketPair(
                get_interface(),
                &mut conn_a,
                &mut conn_b,
                use_network_loopback,
                core::ptr::null(),
                core::ptr::null(),
            )
        };
        if !ok {
            let mut queues = self.global.event_queues.write().unwrap();
            queues.remove(&queue_id_a);
            queues.remove(&queue_id_b);
            return Err(GnsError::SocketPair);
        }
        // Build the client states first so that their `Drop` implementations
        // close the connections and unregister the queues on any later error.
        let make_client = |connection, queue, queue_id| GnsSocket {
            global: self.global,
            state: IsClient {
                queue,
                queue_id,
                global: self.global,
                connection: GnsConnection(connection),
            },
        };
        let a = make_client(conn_a, queue_a, queue_id_a);
        let b = make_client(conn_b, queue_b, queue_id_b);
        // `CreateSocketPair` accepts no config options, so install the same
        // callback and queue ID that `setup_common` passes at creation time.
        for (conn, queue_id) in [(conn_a, queue_id_a), (conn_b, queue_id_b)] {
            unsafe {
                SteamAPI_ISteamNetworkingSockets_SetConnectionUserData(
                    get_interface(),
                    conn,
                    queue_id,
                );
            }
            self.global.utils().set_config_value_scoped(
                ESteamNetworkingConfigValue::k_ESteamNetworkingConfig_Callback_ConnectionStatusChanged,
                ESteamNetworkingConfigScope::k_ESteamNetworkingConfig_Connection,
                conn as isize,
                GnsConfig::Ptr(Self::on_connection_state_changed as *const fn(&SteamNetConnectionStatusChangedCallback_t) as *mut c_void),
            )?;
        }
        Ok((a, b))
    }
}

impl GnsSocket<IsServer> {
    /// Accepts an incoming connection. Only a socket in the [`IsServer`] state
    /// has this operation.
    pub fn accept(&self, connection: GnsConnection) -> GnsResult<()> {
        check(unsafe {
            SteamAPI_ISteamNetworkingSockets_AcceptConnection(get_interface(), connection.0)
        })?;
        if !unsafe {
            SteamAPI_ISteamNetworkingSockets_SetConnectionPollGroup(
                get_interface(),
                connection.0,
                self.state.poll_group.0,
            )
        } {
            // The poll group and the connection should both be valid here, so
            // this is not expected to happen.
            return Err(GnsError::Accept);
        }
        Ok(())
    }

    /// Returns the address the listen socket is bound to.
    ///
    /// The address part is the unspecified address when the socket listens on
    /// every interface, which is what an all-zeros IP passed to
    /// [`GnsSocket::listen`] requests.
    pub fn get_listen_socket_address(&self) -> Option<(IpAddr, u16)> {
        let mut addr: SteamNetworkingIPAddr = unsafe { MaybeUninit::zeroed().assume_init() };
        if unsafe {
            SteamAPI_ISteamNetworkingSockets_GetListenSocketAddress(
                get_interface(),
                self.state.listen_socket.0,
                &mut addr,
            )
        } {
            Some((ip_from_steam_ip_addr(&addr), addr.m_port))
        } else {
            None
        }
    }

    /// Sets a configuration value on the listen socket, for example a
    /// connection option that every accepted connection inherits as its
    /// default.
    pub fn set_listen_socket_config_value(
        &self,
        typ: ESteamNetworkingConfigValue,
        value: GnsConfig<'_>,
    ) -> GnsResult<()> {
        self.global.utils().set_config_value_scoped(
            typ,
            ESteamNetworkingConfigScope::k_ESteamNetworkingConfig_ListenSocket,
            self.state.listen_socket.0 as isize,
            value,
        )
    }

    /// Reads a configuration value back from the listen socket.
    pub fn get_listen_socket_config_value(
        &self,
        typ: ESteamNetworkingConfigValue,
    ) -> GnsResult<GnsConfigValue> {
        self.global.utils().get_config_value_scoped(
            typ,
            ESteamNetworkingConfigScope::k_ESteamNetworkingConfig_ListenSocket,
            self.state.listen_socket.0 as isize,
        )
    }
}

impl GnsSocket<IsClient> {
    /// Returns the socket connection. Only a socket in the [`IsClient`] state
    /// has this operation.
    #[inline]
    pub fn connection(&self) -> GnsConnection {
        self.state.connection
    }
}

/// A configuration value for [`GnsUtils::set_global_config_value`] and
/// [`GnsUtils::set_connection_config_value`].
///
/// The enum is non-exhaustive so that variants for further
/// GameNetworkingSockets data types can be added. You can still construct
/// every existing variant.
#[non_exhaustive]
pub enum GnsConfig<'a> {
    Float(f32),
    Int32(i32),
    /// Allocates a `CString` so that the value ends in a NUL byte. Use
    /// [`GnsConfig::CStr`] to skip that allocation when you already hold a
    /// `CStr`.
    String(&'a str),
    /// A string variant that does not allocate, because `&CStr` already ends
    /// in a NUL byte.
    CStr(&'a CStr),
    Ptr(*mut c_void),
}

/// A configuration value read back through [`GnsUtils::get_global_config_value`]
/// and friends.
///
/// Unlike [`GnsConfig`], which borrows what you pass in, this type owns its
/// data, and it distinguishes `Int64` because GameNetworkingSockets stores
/// some values (such as connection user data) as 64-bit integers.
///
/// The enum is non-exhaustive because it mirrors the GameNetworkingSockets
/// data-type enum, which can grow.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum GnsConfigValue {
    Float(f32),
    Int32(i32),
    Int64(i64),
    String(String),
    Ptr(*mut c_void),
}

pub struct GnsUtils(());

type MsgPtr = *const ::std::os::raw::c_char;

/// A debug callback that you supply.
///
/// It must be `Send + Sync` because GameNetworkingSockets invokes it from its
/// service thread, and it may capture state that your own threads share.
type DebugCallback = dyn Fn(ESteamNetworkingSocketsDebugOutputType, &str) + Send + Sync + 'static;

/// Holds the callback that [`GnsUtils::enable_debug_output`] installs.
///
/// GameNetworkingSockets invokes it from its service thread, so this `OnceLock`
/// is the synchronization point.
static DEBUG_CB: OnceLock<Box<DebugCallback>> = OnceLock::new();

unsafe extern "C" fn debug_trampoline(ty: ESteamNetworkingSocketsDebugOutputType, msg: MsgPtr) {
    if let Some(cb) = DEBUG_CB.get() {
        let s = unsafe { CStr::from_ptr(msg) }.to_str().unwrap_or("");
        cb(ty, s);
    }
}

impl GnsUtils {
    /// Installs a debug callback.
    ///
    /// Only the first call takes effect. Later calls are ignored.
    ///
    /// GameNetworkingSockets runs the callback on its service thread, which is
    /// why the callback must be `Send + Sync + 'static`. The callback may
    /// capture state, because the wrapper stores it as a boxed closure. The
    /// `&str` is borrowed only for the duration of the call.
    pub fn enable_debug_output(
        &self,
        ty: ESteamNetworkingSocketsDebugOutputType,
        f: impl Fn(ESteamNetworkingSocketsDebugOutputType, &str) + Send + Sync + 'static,
    ) {
        let _ = DEBUG_CB.set(Box::new(f));
        unsafe {
            SteamAPI_ISteamNetworkingUtils_SetDebugOutputFunction(
                get_utils(),
                ty,
                Some(debug_trampoline),
            );
        }
    }

    /// Allocates an outbound message and takes ownership of `payload`.
    ///
    /// The buffer stays alive until GameNetworkingSockets releases the message.
    /// At that point the wrapper rebuilds `P` with [`Payload::from_raw`] and
    /// drops it. Nothing is copied when the payload already owns heap memory.
    #[inline]
    pub fn allocate_message<P: Payload>(
        &self,
        conn: GnsConnection,
        flags: SendFlags,
        payload: P,
    ) -> GnsNetworkMessage<ToSend> {
        let message_ptr = unsafe { SteamAPI_ISteamNetworkingUtils_AllocateMessage(get_utils(), 0) };
        GnsNetworkMessage::new(message_ptr, conn, flags, payload)
    }

    /// Sets a global configuration value, for example
    /// `k_ESteamNetworkingConfig_FakePacketLag_Send` to 1000 ms.
    pub fn set_global_config_value(
        &self,
        typ: ESteamNetworkingConfigValue,
        value: GnsConfig<'_>,
    ) -> GnsResult<()> {
        let result = match value {
            GnsConfig::Float(x) => unsafe {
                SteamAPI_ISteamNetworkingUtils_SetGlobalConfigValueFloat(get_utils(), typ, x)
            },
            GnsConfig::Int32(x) => unsafe {
                SteamAPI_ISteamNetworkingUtils_SetGlobalConfigValueInt32(get_utils(), typ, x)
            },
            GnsConfig::String(x) => {
                let c = CString::new(x).map_err(|_| GnsError::Config("interior NUL"))?;
                unsafe {
                    SteamAPI_ISteamNetworkingUtils_SetGlobalConfigValueString(
                        get_utils(),
                        typ,
                        c.as_ptr(),
                    )
                }
            }
            GnsConfig::CStr(x) => unsafe {
                SteamAPI_ISteamNetworkingUtils_SetGlobalConfigValueString(
                    get_utils(),
                    typ,
                    x.as_ptr(),
                )
            },
            GnsConfig::Ptr(x) => unsafe {
                SteamAPI_ISteamNetworkingUtils_SetGlobalConfigValuePtr(get_utils(), typ, x)
            },
        };
        if result {
            Ok(())
        } else {
            Err(GnsError::Config("SetGlobalConfigValue rejected"))
        }
    }

    /// Sets a configuration value on one connection, for example
    /// `k_ESteamNetworkingConfig_SendRateMin` on an accepted connection.
    pub fn set_connection_config_value(
        &self,
        conn: GnsConnection,
        typ: ESteamNetworkingConfigValue,
        value: GnsConfig<'_>,
    ) -> GnsResult<()> {
        let result = match value {
            GnsConfig::Float(x) => unsafe {
                SteamAPI_ISteamNetworkingUtils_SetConnectionConfigValueFloat(
                    get_utils(),
                    conn.0,
                    typ,
                    x,
                )
            },
            GnsConfig::Int32(x) => unsafe {
                SteamAPI_ISteamNetworkingUtils_SetConnectionConfigValueInt32(
                    get_utils(),
                    conn.0,
                    typ,
                    x,
                )
            },
            GnsConfig::String(x) => {
                let c = CString::new(x).map_err(|_| GnsError::Config("interior NUL"))?;
                unsafe {
                    SteamAPI_ISteamNetworkingUtils_SetConnectionConfigValueString(
                        get_utils(),
                        conn.0,
                        typ,
                        c.as_ptr(),
                    )
                }
            }
            GnsConfig::CStr(x) => unsafe {
                SteamAPI_ISteamNetworkingUtils_SetConnectionConfigValueString(
                    get_utils(),
                    conn.0,
                    typ,
                    x.as_ptr(),
                )
            },
            GnsConfig::Ptr(x) => {
                return self.set_config_value_scoped(
                    typ,
                    ESteamNetworkingConfigScope::k_ESteamNetworkingConfig_Connection,
                    conn.0 as isize,
                    GnsConfig::Ptr(x),
                )
            }
        };
        if result {
            Ok(())
        } else {
            Err(GnsError::Config("SetConnectionConfigValue rejected"))
        }
    }

    /// Sets a configuration value on any scope through the generic
    /// `SetConfigValue` entry point.
    fn set_config_value_scoped(
        &self,
        typ: ESteamNetworkingConfigValue,
        scope: ESteamNetworkingConfigScope,
        scope_obj: isize,
        value: GnsConfig<'_>,
    ) -> GnsResult<()> {
        // Owner that must outlive the FFI call.
        let owned_string;
        // `SetConfigValue` reads a string value directly from `pArg`, but a
        // pointer value through one level of indirection: `pArg` must point to
        // the pointer.
        let (data_type, arg): (ESteamNetworkingConfigDataType, *const c_void) = match &value {
            GnsConfig::Float(x) => (
                ESteamNetworkingConfigDataType::k_ESteamNetworkingConfig_Float,
                x as *const f32 as _,
            ),
            GnsConfig::Int32(x) => (
                ESteamNetworkingConfigDataType::k_ESteamNetworkingConfig_Int32,
                x as *const i32 as _,
            ),
            GnsConfig::String(x) => {
                owned_string = CString::new(*x).map_err(|_| GnsError::Config("interior NUL"))?;
                (
                    ESteamNetworkingConfigDataType::k_ESteamNetworkingConfig_String,
                    owned_string.as_ptr() as _,
                )
            }
            GnsConfig::CStr(x) => (
                ESteamNetworkingConfigDataType::k_ESteamNetworkingConfig_String,
                x.as_ptr() as _,
            ),
            GnsConfig::Ptr(x) => (
                ESteamNetworkingConfigDataType::k_ESteamNetworkingConfig_Ptr,
                x as *const *mut c_void as _,
            ),
        };
        if unsafe {
            SteamAPI_ISteamNetworkingUtils_SetConfigValue(
                get_utils(),
                typ,
                scope,
                scope_obj,
                data_type,
                arg,
            )
        } {
            Ok(())
        } else {
            Err(GnsError::Config("SetConfigValue rejected"))
        }
    }

    /// Reads a configuration value from any scope.
    fn get_config_value_scoped(
        &self,
        typ: ESteamNetworkingConfigValue,
        scope: ESteamNetworkingConfigScope,
        scope_obj: isize,
    ) -> GnsResult<GnsConfigValue> {
        let mut data_type = ESteamNetworkingConfigDataType::k_ESteamNetworkingConfig_Int32;
        let mut buf = vec![0u8; 64];
        let mut len = buf.len();
        loop {
            let result = unsafe {
                SteamAPI_ISteamNetworkingUtils_GetConfigValue(
                    get_utils(),
                    typ,
                    scope,
                    scope_obj,
                    &mut data_type,
                    buf.as_mut_ptr() as *mut c_void,
                    &mut len,
                )
            };
            match result {
                ESteamNetworkingGetConfigValueResult::k_ESteamNetworkingGetConfigValue_OK
                | ESteamNetworkingGetConfigValueResult::k_ESteamNetworkingGetConfigValue_OKInherited => {
                    break
                }
                ESteamNetworkingGetConfigValueResult::k_ESteamNetworkingGetConfigValue_BufferTooSmall => {
                    // `len` now holds the required size.
                    buf.resize(len, 0);
                }
                ESteamNetworkingGetConfigValueResult::k_ESteamNetworkingGetConfigValue_BadValue => {
                    return Err(GnsError::Config("unknown config value"))
                }
                ESteamNetworkingGetConfigValueResult::k_ESteamNetworkingGetConfigValue_BadScopeObj => {
                    return Err(GnsError::Config("bad scope object"))
                }
                _ => return Err(GnsError::Config("GetConfigValue failed")),
            }
        }
        // `buf` is a byte buffer, so read the typed values unaligned.
        let value = match data_type {
            ESteamNetworkingConfigDataType::k_ESteamNetworkingConfig_Int32 => {
                GnsConfigValue::Int32(unsafe { (buf.as_ptr() as *const i32).read_unaligned() })
            }
            ESteamNetworkingConfigDataType::k_ESteamNetworkingConfig_Int64 => {
                GnsConfigValue::Int64(unsafe { (buf.as_ptr() as *const i64).read_unaligned() })
            }
            ESteamNetworkingConfigDataType::k_ESteamNetworkingConfig_Float => {
                GnsConfigValue::Float(unsafe { (buf.as_ptr() as *const f32).read_unaligned() })
            }
            ESteamNetworkingConfigDataType::k_ESteamNetworkingConfig_String => {
                let s = CStr::from_bytes_until_nul(&buf)
                    .map_err(|_| GnsError::Config("string value missing NUL"))?;
                GnsConfigValue::String(s.to_string_lossy().into_owned())
            }
            ESteamNetworkingConfigDataType::k_ESteamNetworkingConfig_Ptr => {
                GnsConfigValue::Ptr(unsafe {
                    (buf.as_ptr() as *const *mut c_void).read_unaligned()
                })
            }
            _ => return Err(GnsError::Config("unknown config data type")),
        };
        Ok(value)
    }

    /// Reads a global configuration value back, the counterpart of
    /// [`Self::set_global_config_value`].
    #[inline]
    pub fn get_global_config_value(
        &self,
        typ: ESteamNetworkingConfigValue,
    ) -> GnsResult<GnsConfigValue> {
        self.get_config_value_scoped(
            typ,
            ESteamNetworkingConfigScope::k_ESteamNetworkingConfig_Global,
            0,
        )
    }

    /// Reads a configuration value from one connection, the counterpart of
    /// [`Self::set_connection_config_value`].
    ///
    /// A value that was never set on the connection itself comes back from the
    /// enclosing scope, such as the listen socket or the global defaults.
    #[inline]
    pub fn get_connection_config_value(
        &self,
        conn: GnsConnection,
        typ: ESteamNetworkingConfigValue,
    ) -> GnsResult<GnsConfigValue> {
        self.get_config_value_scoped(
            typ,
            ESteamNetworkingConfigScope::k_ESteamNetworkingConfig_Connection,
            conn.0 as isize,
        )
    }

    /// Returns the name, data type, and maximally specific scope of a
    /// configuration value, or `None` if the value is unknown.
    pub fn get_config_value_info(
        &self,
        typ: ESteamNetworkingConfigValue,
    ) -> Option<(
        &'static str,
        ESteamNetworkingConfigDataType,
        ESteamNetworkingConfigScope,
    )> {
        let mut data_type = ESteamNetworkingConfigDataType::k_ESteamNetworkingConfig_Int32;
        let mut scope = ESteamNetworkingConfigScope::k_ESteamNetworkingConfig_Global;
        let name = unsafe {
            SteamAPI_ISteamNetworkingUtils_GetConfigValueInfo(
                get_utils(),
                typ,
                &mut data_type,
                &mut scope,
            )
        };
        if name.is_null() {
            None
        } else {
            // The name is a static string inside GameNetworkingSockets.
            let name = unsafe { CStr::from_ptr(name) }.to_str().unwrap_or("");
            Some((name, data_type, scope))
        }
    }

    /// Returns the current local timestamp, in microseconds.
    ///
    /// The timebase starts at a value that will never be confused with an
    /// interval, and it matches [`GnsNetworkMessage::time_received`].
    #[inline]
    pub fn local_timestamp(&self) -> SteamNetworkingMicroseconds {
        unsafe { SteamAPI_ISteamNetworkingUtils_GetLocalTimestamp(get_utils()) }
    }
}