calog/libs/calogNet.h

42 lines
2 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)
// enetHost(port, maxPeers) -> hostHandle
// enetConnect(hostHandle, host, port, channels) -> peerHandle
// enetService(hostHandle, timeoutMs) -> { type[, peer, channel, data] }
// enetSend(peerHandle, channel, data, reliable)
// enetDisconnect(peerHandle)
// enetClose(hostHandle)
// Payloads are binary-safe strings. The blocking natives (accept/recv/recvFrom/enetService)
// 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). Three transports, all always compiled in and first-class:
// TCP, UDP, and ENet (reliable UDP over UDP).
#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