Minimal TCP server primitives, sitting beside sig_thread.h. POSIX
(BSD sockets) only for now; an RTOS port replaces just this shim.
Not part of sig::sig: it is the optional sig::net target (SIG_NET),
used by sig::http_server. IPv4 only.
A negative timeout_ms (to accept/read/write_all) means wait forever
(it is passed straight to poll()); 0 polls without blocking.
`addr` must be a numeric IPv4 address (e.g. "127.0.0.1",
"0.0.0.0"): SIG_ERR_INVALID_ARG otherwise. port 0 binds an
ephemeral port; the bound port is stored in *out_port (nullable).
Bind/listen failure (address in use, permission) is
SIG_ERR_PLATFORM.
sig_net_accept
sig_status_t sig_net_accept(sig_net_listener * l, int timeout_ms, sig_net_conn ** out)
Parameter
Type
l
sig_net_listener *
timeout_ms
int
out
sig_net_conn **
Waits up to timeout_ms for a connection. SIG_ERR_TIMEOUT if none
arrived (also for a spurious wakeup); *out untouched in that case.
Reads up to len bytes, waiting up to timeout_ms for the first
byte. SIG_OK with *out_n == 0 means the peer closed.
SIG_ERR_TIMEOUT if nothing arrived in time.
Writes all len bytes; timeout_ms bounds each wait for the socket to
become writable. SIG_ERR_TIMEOUT on a stalled peer, SIG_ERR_PLATFORM
on a broken connection. Never raises SIGPIPE.
sig_net_listener_fd
int sig_net_listener_fd(const sig_net_listener * l)
Parameter
Type
l
const sig_net_listener *
Event-loop use. The sockets are always non-blocking, so with timeout_ms 0
sig_net_accept() and sig_net_read() are polls: SIG_ERR_TIMEOUT means "not
ready, try when the loop says so". The descriptors are for registering with a
sig_loop; they stay owned by the listener/connection and are valid until it
is closed (unwatch first).
Event-loop use. The sockets are always non-blocking, so with timeout_ms 0
sig_net_accept() and sig_net_read() are polls: SIG_ERR_TIMEOUT means "not
ready, try when the loop says so". The descriptors are for registering with a
sig_loop; they stay owned by the listener/connection and are valid until it
is closed (unwatch first).
Writes as much of buf as the socket takes right now without waiting:
SIG_OK with *out_n bytes accepted (possibly fewer than len, never 0 for a
non-empty write), or SIG_ERR_TIMEOUT if it would block (*out_n is 0 -- wait
for the socket to become writable and call again), or SIG_ERR_PLATFORM on a
broken connection. Never raises SIGPIPE.