AF_INET
pub val AF_INET = 2extern (C) fn socket(domain: i32, type_: i32, protocol: i32) i32 = trust { ... } = "sx_socket"extern (C) fn bind(fd: i32, addr: ref void, addrlen: i32) i32 = trust { ... } = "sx_bind"extern (C) fn listen(fd: i32, backlog: i32) i32 = trust { ... } = "sx_listen"extern (C) fn accept(fd: i32, addr: ref void, addrlen: ref void) i32 = trust { ... } = "sx_accept"extern (C) fn send(fd: i32, buf: ref void, len: i64, flags: i32) i64 = trust { ... } = "sx_send"extern (C) fn recv(fd: i32, buf: ref void, len: i64, flags: i32) i64 = trust { ... } = "sx_recv"extern (C) fn close(fd: i32) i32 = trust { ... } = "sx_close"extern (C) fn htons(port: i32) i32 = trust { ... } = "sx_htons"extern (C) fn setsockopt(fd: i32, level: i32, opt: i32, vl: ref void, len: i32) i32 = trust { ... } = "sx_setsockopt"extern (C) fn fcntl(fd: i32, cmd: i32, arg: i32) i32 = trust { ... } = "sx_fcntl"extern (C) fn poll(fds: ref void, count: usize, timeout: i32) i32 = trust { ... } = "sx_poll"extern (C, header) fn socket(domain: i32, type_: i32, protocol: i32) i32 = trust { ... } = "socket"extern (C, header) fn bind(fd: i32, addr: ref void, addrlen: i32) i32 = trust { ... } = "bind"extern (C, header) fn listen(fd: i32, backlog: i32) i32 = trust { ... } = "listen"extern (C, header) fn accept(fd: i32, addr: ref void, addrlen: ref void) i32 = trust { ... } = "accept"extern (C, header) fn send(fd: i32, buf: ref void, len: i64, flags: i32) i64 = trust { ... } = "send"extern (C, header) fn recv(fd: i32, buf: ref void, len: i64, flags: i32) i64 = trust { ... } = "recv"extern (C, header) fn close(fd: i32) i32 = trust { ... } = "close"extern (C, header) fn htons(port: i32) i32 = trust { ... } = "htons"extern (C, header) fn setsockopt(fd: i32, level: i32, opt: i32, vl: ref void, len: i32) i32 = trust { ... } = "setsockopt"extern (C, header) fn fcntl(fd: i32, cmd: i32, ...) i32 = trust { ... } = "fcntl"extern (C, header) fn poll(fds: ref void, count: usize, timeout: i32) i32 = trust { ... } = "poll"extern (C) fn WSAStartup(version: u16, data: ref void) i32 = trust { ... } = "sx_wsa_startup"extern (C) fn WSACleanup() i32 = trust { ... } = "sx_wsa_cleanup"extern (C) fn win_socket(domain: i32, type_: i32, protocol: i32) i32 = trust { ... } = "sx_socket"extern (C) fn win_bind(fd: u64, addr: ref void, addrlen: i32) i32 = trust { ... } = "sx_bind"extern (C) fn win_listen(fd: u64, backlog: i32) i32 = trust { ... } = "sx_listen"extern (C) fn win_accept(fd: u64, addr: ref void, addrlen: ref void) i32 = trust { ... } = "sx_accept"extern (C) fn win_send(fd: u64, buf: ref void, len: i32, flags: i32) i64 = trust { ... } = "sx_send"extern (C) fn win_recv(fd: u64, buf: ref void, len: i32, flags: i32) i64 = trust { ... } = "sx_recv"extern (C) fn closesocket(fd: u64) i32 = trust { ... } = "sx_closesocket"extern (C) fn win_htons(port: u16) u16 = trust { ... } = "sx_htons"extern (C) fn win_setsockopt(fd: u64, level: i32, opt: i32, vl: ref void, len: i32) i32 = trust { ... } = "sx_setsockopt"extern (C) fn WSAPoll(fds: ref void, count: u32, timeout: i32) i32 = trust { ... } = "sx_poll"extern (C) fn ioctlsocket(fd: u64, cmd: i32, argp: ref u32) i32 = trust { ... } = "sx_ioctlsocket"extern (C, header) fn WSAStartup(version: u16, data: ref void) i32 = trust { ... } = "WSAStartup"extern (C, header) fn WSACleanup() i32 = trust { ... } = "WSACleanup"extern (C, header) fn win_socket(domain: i32, type_: i32, protocol: i32) u64 = trust { ... } = "socket"extern (C, header) fn win_bind(fd: u64, addr: ref void, addrlen: i32) i32 = trust { ... } = "bind"extern (C, header) fn win_listen(fd: u64, backlog: i32) i32 = trust { ... } = "listen"extern (C, header) fn win_accept(fd: u64, addr: ref void, addrlen: ref void) u64 = trust { ... } = "accept"extern (C, header) fn win_send(fd: u64, buf: ref void, len: i32, flags: i32) i32 = trust { ... } = "send"extern (C, header) fn win_recv(fd: u64, buf: ref void, len: i32, flags: i32) i32 = trust { ... } = "recv"extern (C, header) fn closesocket(fd: u64) i32 = trust { ... } = "closesocket"extern (C, header) fn win_htons(port: u16) u16 = trust { ... } = "htons"extern (C, header) fn win_setsockopt(fd: u64, level: i32, opt: i32, vl: ref void, len: i32) i32 = trust { ... } = "setsockopt"extern (C, header) fn WSAPoll(fds: ref void, count: u32, timeout: i32) i32 = trust { ... } = "WSAPoll"extern (C, header) fn ioctlsocket(fd: u64, cmd: i32, argp: ref u32) i32 = trust { ... } = "ioctlsocket"pub val AF_INET = 2pub val IPPROTO_TCP = 6pub val POLL_READ = 1pub val POLL_WRITE = 2pub val POLL_ERROR = 4fn poll_events_to_platform(events: i32) i16fn poll_events_from_platform(events: i32) i32pub fn (TcpServer) set_non_blocking(fd: i64) bool = trust { ... }Put a socket into non-blocking mode.
pub type EventLoop = { fds: mut list[i64], events: mut list[i64], revents: mut list[i64] }A poll-based event loop. Register file descriptors with watch, call wait, then inspect ready descriptors with fd_at and revents_at.
| Name | Type |
|---|---|
fds | mut list[i64] |
events | mut list[i64] |
revents | mut list[i64] |
pub fn (EventLoop) new[A: allocators.GeneralAllocator](ac: A) result[EventLoop, allocators.AllocError]Create a new event loop.
pub fn (EventLoop) watch(loop: EventLoop, fd: i64, events: i32) void = trust { ... }Add or update a descriptor and the events to watch.
pub fn (EventLoop) unwatch(loop: EventLoop, fd: i64) void = trust { ... }Stop watching a descriptor.
pub fn (EventLoop) count(loop: EventLoop) i64Return the number of watched descriptors.
pub fn (EventLoop) fd_at(loop: EventLoop, index: i64) i64 = trust { ... }Return the descriptor at index.
pub fn (EventLoop) revents_at(loop: EventLoop, index: i64) i32 = trust { ... }Return the ready events for index after the most recent wait.
pub fn (EventLoop) wait(loop: EventLoop, timeout_ms: i32) i32 = trust { ... }Wait for events. timeout_ms is in milliseconds; use a negative value to wait indefinitely. Returns the number of ready descriptors, 0 on timeout, or -1 on failure.
pub fn (EventLoop) free[A: allocators.GeneralAllocator](ac: A, loop: mut EventLoop) voidFree the backing storage of a list embedded directly in a struct.
Free an event loop and its internal lists.
type ServerHandle = { port: mut i64, fd: mut i64 }| Name | Type |
|---|---|
port | mut i64 |
fd | mut i64 |
type TcpServer = { }pub fn (TcpServer) listen[A: allocators.GeneralAllocator](ac: A, port: i32) ref ServerHandleCreate a TCP server bound to port and start listening. Returns a server handle allocated with the provided allocator; caller must call TcpServer.close().
pub fn (TcpServer) accept(server: ref ServerHandle) i64 = trust { ... }Accept the next incoming connection. Returns the client socket fd, or -1 on error.
pub fn (TcpServer) send(client_fd: i64, data: ref u8, len: i64) i64 = trust { ... }Send bytes over a client socket fd. Returns the number of bytes sent, or -1 on error.
pub fn (TcpServer) recv(client_fd: i64, buf: ref u8, max_len: i64) i64 = trust { ... }Receive bytes from a client socket fd into buf (at most max_len bytes). Returns the number of bytes received, or -1 on error / 0 on close.
pub fn (TcpServer) close_client(client_fd: i64) void = trust { ... }Close a client socket fd.
pub fn (TcpServer) close[A: allocators.GeneralAllocator](ac: A, server: ref ServerHandle) voidShut down and free the server handle.
pub fn (TcpServer) port(server: ref ServerHandle) i64Read the listening port from a server handle.
pub fn (TcpServer) fd(server: ref ServerHandle) i64Read the listening fd from a server handle.