Files
game/modules/socket.sx
2026-02-22 13:22:29 +02:00

34 lines
1.1 KiB
Plaintext

// POSIX socket module (macOS only)
// sockaddr_in layout and constants are platform-specific.
libc :: #library "c";
// POSIX socket API
socket :: (domain: s32, kind: s32, protocol: s32) -> s32 #foreign libc;
setsockopt :: (fd: s32, level: s32, optname: s32, optval: *s32, optlen: u32) -> s32 #foreign libc;
bind :: (fd: s32, addr: *SockAddr, addrlen: u32) -> s32 #foreign libc;
listen :: (fd: s32, backlog: s32) -> s32 #foreign libc;
accept :: (fd: s32, addr: *SockAddr, addrlen: *u32) -> s32 #foreign libc;
read :: (fd: s32, buf: [*]u8, count: s64) -> s64 #foreign libc;
write :: (fd: s32, buf: [*]u8, count: s64) -> s64 #foreign libc;
close :: (fd: s32) -> s32 #foreign libc;
// Constants (macOS)
AF_INET :s32: 2;
SOCK_STREAM :s32: 1;
SOL_SOCKET :s32: 0xFFFF;
SO_REUSEADDR :s32: 0x4;
// macOS sockaddr_in (16 bytes, has sin_len field)
SockAddr :: struct {
sin_len: u8;
sin_family: u8;
sin_port: u16;
sin_addr: u32 = 0;
sin_zero: u64 = 0;
}
htons :: (port: s64) -> u16 {
cast(u16) (((port & 0xFF) << 8) | ((port >> 8) & 0xFF));
}