The ENet shim
1. The ENet shim
cc -include ngnet/enet.h $(pkg-config --cflags ngnet) -c server.c
cc server.o ... $(pkg-config --libs ngnet)-include puts macros after ENet’s header that redirect enet_host_create, enet_host_destroy, enet_host_service, enet_host_check_events, enet_host_broadcast, enet_host_flush, enet_host_connect, enet_peer_send, enet_peer_disconnect, enet_peer_disconnect_now, enet_peer_disconnect_later, enet_peer_reset, enet_peer_timeout and enet_peer_ping to ngnet_* wrappers with the same signatures. The program’s ENet code is unchanged. What the wrappers add:
- A host created with a bind address is the game server: the library attaches to it. It starts on the program’s first
enet_host_service, so settings made after the host exists (a Lua config) are in time. It opens WebRTC ongame port + 1, announces to the masters (signed), answers the master’s UDPHELLOINFOprobe, andenet_host_destroydetaches (browsers getdisconnect, the masters getleave). A host created with aNULLaddress is passed through untouched.ngnet_attach(host)/ngnet_detach(host)do it explicitly. - Each **browser player is a real
ENetPeer** the library allocates (outsidehost->peers): stateCONNECTED,address= the browser’s real IP (from the master’s offer),roundTripTimekept updated,datafree for the program.enet_host_servicereturns itsCONNECT(event.data= the browser’s connect data),RECEIVE(a realENetPacket, destroyed withenet_packet_destroy) andDISCONNECTevents like any other peer’s.enet_peer_sendto it goes over the DataChannel and frees the packet as ENet would;enet_host_broadcastreaches native and browser peers. A reliable packet goes on the reliable DataChannel, an unreliable one on the unreliable DataChannel.ngnet_peer_is_browser(peer)tells them apart. - Timeouts of
enet_host_serviceare honoured: it waits on ENet’s socket and wakes for browser events. - Zero-config announce. A program that announces to a classic 0.75 master (a second host created with a
NULLaddress, connect data31, first reliable packetu8 max_players, u16 port LE, name\0, mode\0, map\0, then single byte player counts) needs nongnet_setat all: the library reads those bytes and uses the name, mode, map, max players and player count for its own signed announce, with protocol0.75. Explicitngnet_setvalues win. - HELLOINFO: the library installs
host->interceptand answers the master’s probe with{"kind":"info",...}built from what it knows. A program that installs its own intercept keeps it; it can callngnet_intercept(host, event)first to keep the answer.
ENet header selection: NGNET_ENET_HEADER (default <enet/enet.h>); define NGNET_ENET6 for enet6, whose enet_host_create takes an address type first.