calog/libs/calogNet.h

36 lines
1.7 KiB
C

// calogNet.h -- calog network library.
//
// Registers inline natives that a script of any engine can call:
// tcpConnect(host, port) -> handle
// tcpListen(port) -> handle
// tcpAccept(handle) -> handle (blocks for a client)
// tcpSend(handle, data) -> bytesSent (sends all of data)
// tcpRecv(handle, maxBytes) -> data (up to maxBytes; nil at end of stream)
// tcpClose(handle)
// udpOpen(port) -> handle (port 0 = ephemeral)
// udpSendTo(handle, host, port, data) -> bytesSent
// udpRecvFrom(handle, maxBytes) -> { data, host, port }
// udpClose(handle)
// Payloads are binary-safe strings. The blocking natives (accept/recv/recvFrom) are INLINE,
// so they stall only the calling script's context thread, not the host. Handles are NOT
// reference-counted: a socket/host/peer handle belongs to the context that created it, and
// using one handle concurrently from two contexts -- or closing it while another context is
// mid-operation -- is undefined (use-after-free). IPv4 for v1;
// hostname resolution uses getaddrinfo (works in dynamic builds; a fully-static glibc build
// cannot resolve names -- connect by IP, or link musl). Backends: TCP/UDP always; ENet is
// added behind CALOG_WITH_ENET.
#ifndef CALOG_NET_H
#define CALOG_NET_H
#include "calog.h"
// Register the network natives on a runtime. Idempotent across runtimes (they share a
// process-wide socket registry). Returns calogOkE or an error.
int32_t calogNetRegister(CalogT *calog);
// Close any still-open sockets and free the process-wide registry. Call it AFTER the
// runtime is torn down (calogDestroy), since it invalidates the natives' state.
void calogNetShutdown(void);
#endif