The native API and the client
3. The native API
#include <ngnet/ngnet.h>
static void on_connect(void* user, conn_t* conn, uint32_t connect_data) { conn->owner = my_player_for(conn); }
static void on_receive(void* user, conn_t* conn, uint8_t channel, const uint8_t* data, size_t len) { ... }
static void on_closed(void* user, conn_t* conn) { ... }
ngnet_set("name", "My server");
ngnet_server_events_t ev = { .user = game, .connect = on_connect, .receive = on_receive, .closed = on_closed };
ngnet_server_start(32887, &ev);
for (;;) { ngnet_service(); /* ... the tick ... */ }
ngnet_server_stop();conn_send(conn, channel, data, len, reliable), conn_disconnect(conn, reason, how), conn_rtt, conn_pending are in ngnet/conn.h. Everything runs on the caller’s thread: HTTP (libcurl) and libdatachannel use threads of their own and only queue; their results are delivered as callbacks from ngnet_service().
The client
ngnet/transport.h is the client’s one connection to a server: transport_connect (ENet), transport_connect_endpoint (WebRTC through a master: a server id or a join code, plus ICE servers), transport_service, transport_send, transport_disconnect, … The desktop WebRTC transport is libdatachannel’s C API; the browser one is src/transport_web.c with web/ngnet_web.js (Emscripten: --js-library web/ngnet_web.js). Errors are codes (transport_error_code()) with an English default text (transport_error()), and an optional ngnet_set_tr() hook translates them. ngnet/masterlist.h reads /serverlist.json from the masters the program names, each row remembering which master listed it; ngnet_list_parse needs no network (a browser feeds it its own fetch).