sig_net.h

include/sig/net/sig_net.h

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.

sig_net_listener

typedef struct sig_net_listener sig_net_listener;

sig_net_conn

typedef struct sig_net_conn sig_net_conn;

sig_net_listen

sig_status_t sig_net_listen(const char * addr, uint16_t port, sig_net_listener ** out, uint16_t * out_port)
ParameterType
addrconst char *
portuint16_t
outsig_net_listener **
out_portuint16_t *

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)
ParameterType
lsig_net_listener *
timeout_msint
outsig_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.

sig_net_read

sig_status_t sig_net_read(sig_net_conn * c, void * buf, size_t len, int timeout_ms, size_t * out_n)
ParameterType
csig_net_conn *
bufvoid *
lensize_t
timeout_msint
out_nsize_t *

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.

sig_net_write_all

sig_status_t sig_net_write_all(sig_net_conn * c, const void * buf, size_t len, int timeout_ms)
ParameterType
csig_net_conn *
bufconst void *
lensize_t
timeout_msint

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)
ParameterType
lconst 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).

sig_net_conn_fd

int sig_net_conn_fd(const sig_net_conn * c)
ParameterType
cconst sig_net_conn *

shared with sig_net_listener_fd

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).

sig_net_write

sig_status_t sig_net_write(sig_net_conn * c, const void * buf, size_t len, size_t * out_n)
ParameterType
csig_net_conn *
bufconst void *
lensize_t
out_nsize_t *

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.

sig_net_conn_close

void sig_net_conn_close(sig_net_conn * c)
ParameterType
csig_net_conn *

sig_net_listener_close

void sig_net_listener_close(sig_net_listener * l)
ParameterType
lsig_net_listener *