net/core

sourcenet/core.sx

socket

when posix, when freestandingprivate
extern (C) fn socket(domain: i32, type_: i32, protocol: i32) i32 = trust { ... } = "sx_socket"

bind

when posix, when freestandingprivate
extern (C) fn bind(fd: i32, addr: ref void, addrlen: i32) i32 = trust { ... } = "sx_bind"

listen

when posix, when freestandingprivate
extern (C) fn listen(fd: i32, backlog: i32) i32 = trust { ... } = "sx_listen"

accept

when posix, when freestandingprivate
extern (C) fn accept(fd: i32, addr: ref void, addrlen: ref void) i32 = trust { ... } = "sx_accept"

send

when posix, when freestandingprivate
extern (C) fn send(fd: i32, buf: ref void, len: i64, flags: i32) i64 = trust { ... } = "sx_send"

recv

when posix, when freestandingprivate
extern (C) fn recv(fd: i32, buf: ref void, len: i64, flags: i32) i64 = trust { ... } = "sx_recv"

close

when posix, when freestandingprivate
extern (C) fn close(fd: i32) i32 = trust { ... } = "sx_close"

htons

when posix, when freestandingprivate
extern (C) fn htons(port: i32) i32 = trust { ... } = "sx_htons"

setsockopt

when posix, when freestandingprivate
extern (C) fn setsockopt(fd: i32, level: i32, opt: i32, vl: ref void, len: i32) i32 = trust { ... } = "sx_setsockopt"

fcntl

when posix, when freestandingprivate
extern (C) fn fcntl(fd: i32, cmd: i32, arg: i32) i32 = trust { ... } = "sx_fcntl"

poll

when posix, when freestandingprivate
extern (C) fn poll(fds: ref void, count: usize, timeout: i32) i32 = trust { ... } = "sx_poll"

socket

when posix, otherwiseprivate
extern (C, header) fn socket(domain: i32, type_: i32, protocol: i32) i32 = trust { ... } = "socket"

bind

when posix, otherwiseprivate
extern (C, header) fn bind(fd: i32, addr: ref void, addrlen: i32) i32 = trust { ... } = "bind"

listen

when posix, otherwiseprivate
extern (C, header) fn listen(fd: i32, backlog: i32) i32 = trust { ... } = "listen"

accept

when posix, otherwiseprivate
extern (C, header) fn accept(fd: i32, addr: ref void, addrlen: ref void) i32 = trust { ... } = "accept"

send

when posix, otherwiseprivate
extern (C, header) fn send(fd: i32, buf: ref void, len: i64, flags: i32) i64 = trust { ... } = "send"

recv

when posix, otherwiseprivate
extern (C, header) fn recv(fd: i32, buf: ref void, len: i64, flags: i32) i64 = trust { ... } = "recv"

close

when posix, otherwiseprivate
extern (C, header) fn close(fd: i32) i32 = trust { ... } = "close"

htons

when posix, otherwiseprivate
extern (C, header) fn htons(port: i32) i32 = trust { ... } = "htons"

setsockopt

when posix, otherwiseprivate
extern (C, header) fn setsockopt(fd: i32, level: i32, opt: i32, vl: ref void, len: i32) i32 = trust { ... } = "setsockopt"

fcntl

when posix, otherwiseprivate
extern (C, header) fn fcntl(fd: i32, cmd: i32, ...) i32 = trust { ... } = "fcntl"

poll

when posix, otherwiseprivate
extern (C, header) fn poll(fds: ref void, count: usize, timeout: i32) i32 = trust { ... } = "poll"

WSAStartup

when windows, when freestandingprivate
extern (C) fn WSAStartup(version: u16, data: ref void) i32 = trust { ... } = "sx_wsa_startup"

WSACleanup

when windows, when freestandingprivate
extern (C) fn WSACleanup() i32 = trust { ... } = "sx_wsa_cleanup"

win_socket

when windows, when freestandingprivate
extern (C) fn win_socket(domain: i32, type_: i32, protocol: i32) i32 = trust { ... } = "sx_socket"

win_bind

when windows, when freestandingprivate
extern (C) fn win_bind(fd: u64, addr: ref void, addrlen: i32) i32 = trust { ... } = "sx_bind"

win_listen

when windows, when freestandingprivate
extern (C) fn win_listen(fd: u64, backlog: i32) i32 = trust { ... } = "sx_listen"

win_accept

when windows, when freestandingprivate
extern (C) fn win_accept(fd: u64, addr: ref void, addrlen: ref void) i32 = trust { ... } = "sx_accept"

win_send

when windows, when freestandingprivate
extern (C) fn win_send(fd: u64, buf: ref void, len: i32, flags: i32) i64 = trust { ... } = "sx_send"

win_recv

when windows, when freestandingprivate
extern (C) fn win_recv(fd: u64, buf: ref void, len: i32, flags: i32) i64 = trust { ... } = "sx_recv"

closesocket

when windows, when freestandingprivate
extern (C) fn closesocket(fd: u64) i32 = trust { ... } = "sx_closesocket"

win_htons

when windows, when freestandingprivate
extern (C) fn win_htons(port: u16) u16 = trust { ... } = "sx_htons"

win_setsockopt

when windows, when freestandingprivate
extern (C) fn win_setsockopt(fd: u64, level: i32, opt: i32, vl: ref void, len: i32) i32 = trust { ... } = "sx_setsockopt"

WSAPoll

when windows, when freestandingprivate
extern (C) fn WSAPoll(fds: ref void, count: u32, timeout: i32) i32 = trust { ... } = "sx_poll"

ioctlsocket

when windows, when freestandingprivate
extern (C) fn ioctlsocket(fd: u64, cmd: i32, argp: ref u32) i32 = trust { ... } = "sx_ioctlsocket"

WSAStartup

when windows, otherwiseprivate
extern (C, header) fn WSAStartup(version: u16, data: ref void) i32 = trust { ... } = "WSAStartup"

WSACleanup

when windows, otherwiseprivate
extern (C, header) fn WSACleanup() i32 = trust { ... } = "WSACleanup"

win_socket

when windows, otherwiseprivate
extern (C, header) fn win_socket(domain: i32, type_: i32, protocol: i32) u64 = trust { ... } = "socket"

win_bind

when windows, otherwiseprivate
extern (C, header) fn win_bind(fd: u64, addr: ref void, addrlen: i32) i32 = trust { ... } = "bind"

win_listen

when windows, otherwiseprivate
extern (C, header) fn win_listen(fd: u64, backlog: i32) i32 = trust { ... } = "listen"

win_accept

when windows, otherwiseprivate
extern (C, header) fn win_accept(fd: u64, addr: ref void, addrlen: ref void) u64 = trust { ... } = "accept"

win_send

when windows, otherwiseprivate
extern (C, header) fn win_send(fd: u64, buf: ref void, len: i32, flags: i32) i32 = trust { ... } = "send"

win_recv

when windows, otherwiseprivate
extern (C, header) fn win_recv(fd: u64, buf: ref void, len: i32, flags: i32) i32 = trust { ... } = "recv"

closesocket

when windows, otherwiseprivate
extern (C, header) fn closesocket(fd: u64) i32 = trust { ... } = "closesocket"

win_htons

when windows, otherwiseprivate
extern (C, header) fn win_htons(port: u16) u16 = trust { ... } = "htons"

win_setsockopt

when windows, otherwiseprivate
extern (C, header) fn win_setsockopt(fd: u64, level: i32, opt: i32, vl: ref void, len: i32) i32 = trust { ... } = "setsockopt"

WSAPoll

when windows, otherwiseprivate
extern (C, header) fn WSAPoll(fds: ref void, count: u32, timeout: i32) i32 = trust { ... } = "WSAPoll"

ioctlsocket

when windows, otherwiseprivate
extern (C, header) fn ioctlsocket(fd: u64, cmd: i32, argp: ref u32) i32 = trust { ... } = "ioctlsocket"

AF_INET

pub val AF_INET = 2

IPPROTO_TCP

pub val IPPROTO_TCP = 6

POLL_READ

pub val POLL_READ = 1

POLL_WRITE

pub val POLL_WRITE = 2

POLL_ERROR

pub val POLL_ERROR = 4

poll_events_to_platform

private
fn poll_events_to_platform(events: i32) i16

poll_events_from_platform

private
fn poll_events_from_platform(events: i32) i32

set_non_blocking

pub fn (TcpServer) set_non_blocking(fd: i64) bool = trust { ... }

Put a socket into non-blocking mode.

EventLoop

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.

Fields

NameType
fdsmut list[i64]
eventsmut list[i64]
reventsmut list[i64]

new

pub fn (EventLoop) new[A: allocators.GeneralAllocator](ac: A) result[EventLoop, allocators.AllocError]

Create a new event loop.

watch

pub fn (EventLoop) watch(loop: EventLoop, fd: i64, events: i32) void = trust { ... }

Add or update a descriptor and the events to watch.

unwatch

pub fn (EventLoop) unwatch(loop: EventLoop, fd: i64) void = trust { ... }

Stop watching a descriptor.

count

pub fn (EventLoop) count(loop: EventLoop) i64

Return the number of watched descriptors.

fd_at

pub fn (EventLoop) fd_at(loop: EventLoop, index: i64) i64 = trust { ... }

Return the descriptor at index.

revents_at

pub fn (EventLoop) revents_at(loop: EventLoop, index: i64) i32 = trust { ... }

Return the ready events for index after the most recent wait.

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.

free

pub fn (EventLoop) free[A: allocators.GeneralAllocator](ac: A, loop: mut EventLoop) void

Free the backing storage of a list embedded directly in a struct.

Free an event loop and its internal lists.

ServerHandle

type ServerHandle = { port: mut i64, fd: mut i64 }

Fields

NameType
portmut i64
fdmut i64

TcpServer

type TcpServer = {  }

listen

pub fn (TcpServer) listen[A: allocators.GeneralAllocator](ac: A, port: i32) ref ServerHandle

Create a TCP server bound to port and start listening. Returns a server handle allocated with the provided allocator; caller must call TcpServer.close().

accept

pub fn (TcpServer) accept(server: ref ServerHandle) i64 = trust { ... }

Accept the next incoming connection. Returns the client socket fd, or -1 on error.

send

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.

recv

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.

close_client

pub fn (TcpServer) close_client(client_fd: i64) void = trust { ... }

Close a client socket fd.

close

pub fn (TcpServer) close[A: allocators.GeneralAllocator](ac: A, server: ref ServerHandle) void

Shut down and free the server handle.

port

pub fn (TcpServer) port(server: ref ServerHandle) i64

Read the listening port from a server handle.

fd

pub fn (TcpServer) fd(server: ref ServerHandle) i64

Read the listening fd from a server handle.