Porting the game's server and client
Old symbol on the left, new one on the right. “stays” means the code is the game’s and keeps using ngnet’s types.
Server
Build
| before | now |
|---|---|
netlib/netlib.cmake (NGNET_COMMON_SOURCES, NGNET_ENET_CLIENT_SOURCES) | include(ngnet/ngnet.cmake): NGNET_SOURCES, NGNET_INCLUDE_DIR, NGNET_PRIVATE_INCLUDE_DIR, NGNET_DEFINITIONS, NGNET_LIBRARIES; or add_subdirectory(ngnet) and link ngnet::ngnet |
server/cmake/libdatachannel.cmake | ngnet/cmake/libdatachannel.cmake (same pinned v0.24.6, same offline FETCHCONTENT_SOURCE_DIR_LIBDATACHANNEL); drop the server’s copy |
SERVER_NET_LIBS datachannel OpenSSL::Crypto, ENABLE_CURL for the master | the library links libdatachannel, OpenSSL, libcurl itself (NGNET_LIBRARIES) |
<enet6/enet.h> in netlib | NGNET_ENET_HEADER = enet6/enet.h (defines NGNET_ENET6) and NGNET_ENET_INCLUDE_DIR / NGNET_ENET_LIBRARY |
Server/Net/RtcPeer.{cpp,h} | ngnet/src/rtcpeer.{cpp,h} (private); delete |
SP_WEB_HOST (Emscripten server in a Worker) | not in the library: Net/Worker.c and WebHostStubs.c stay in the game; build ngnet with -DNGNET_WEBRTC=OFF -DNGNET_MASTER=OFF there |
Types and callbacks
| before | now |
|---|---|
<ngnet/conn.h> conn_t.player_id, CONN_NO_PLAYER | conn_t.owner (void*, NULL = nobody owns it); keep the player slot id in a field of your own struct or store the player_t* |
conn_t, conn_transport_t, conn_send, conn_disconnect, conn_rtt, conn_pending, conn_close_t | same names, same header (ngnet/conn.h) |
server_conn_connect(server, conn, connect_data) | ngnet_server_events_t.connect(user, conn, connect_data) |
server_conn_receive(server, conn, data, len) | ngnet_server_events_t.receive(user, conn, channel, data, len) (the channel is new: 0 reliable, 1 unreliable) |
server_conn_closed(server, conn) | ngnet_server_events_t.closed(user, conn) |
server_conn_in_use(server, conn) | ngnet_server_events_t.in_use(user, conn) |
the ban and full checks in webrtc_offer (player_banned, num_players >= max_players) | ngnet_server_events_t.offer(user, ip, &reason): return non zero to reject, *reason is shown to the player |
user | server goes in events.user |
conn_key_of, conn_ipv6_prefix | same names, now in the library (ngnet/conn.h) |
conn_count_from(key, except) | conn_enet_count_from(...) + ngnet_server_count_from(key, except) |
conn_send_reliable, conn_player_prefix, NetGuard | stay (they know the game) |
EnetConn.c | stays: it fills conn_t for ENet peers; conn->owner replaces player_id. (Or drop it and use the ENet shim: see below) |
Lifecycle
| before | now |
|---|---|
webrtc_start(server) + master_start(server) | ngnet_server_start(game_port, &events) after the ngnet_set(...) calls below |
webrtc_poll(server) + master_tick(server) (every tick) | ngnet_service() |
webrtc_stop() + master_stop() | ngnet_server_stop() |
webrtc_port() | ngnet_webrtc_port() |
webrtc_fingerprint(), webrtc_ice_servers(), master_answer/reject, webrtc_offer | internal now |
webrtc_count_from | ngnet_server_count_from |
the [master] enabled=false, SP_MASTER=0, listen_private && !join_code, 0.75 profile with a join code | the game decides not to call ngnet_server_start, or ngnet_set("off", "1") |
[webrtc] enabled / SP_WEBRTC_ENABLED, server->join_code forcing it on | the game decides whether to call ngnet_server_start |
| IPv6 browsers taken, announce over IPv6 | ngnet_set("ipv6", "1"): the library is IPv4 only by default |
LOG_* of the library | ngnet_set_log(fn) (ngnet/host.h): levels NGNET_LOG_INFO/WARN/ERROR |
evt_log(NULL, "webrtc"/"master", ...) | ngnet_set_event(fn): fn("webrtc", "action=offer ..."); the field text is the same |
Configuration (the game keeps its TOML; it calls ngnet_set with what it reads)
| before | now |
|---|---|
[webrtc] port, SP_WEBRTC_PORT | ngnet_set("webrtc_port", ...) (or NGNET_WEBRTC_PORT) |
[webrtc] stun, SP_WEBRTC_STUN | ngnet_set("stun", "a,b") |
[webrtc] turn, turns, SP_WEBRTC_TURN(S) | ngnet_set("turn", "turn:h:3478,turns:h:5349") (one list: the scheme is in each URL) |
[webrtc] turn_username, turn_credential | ngnet_set("turn_username"/"turn_credential", ...) |
[master] url, SP_MASTER_URL | ngnet_set("master", url) (repeat for several; NGNET_MASTER) |
[master] key_file, SP_MASTER_KEY, default <data>/master.key | ngnet_set("key_file", path): **set it to the old <data>/master.key**: it is the same PEM Ed25519 format, so the server keeps its identity and its row id |
webrtc.pem in the data folder | ngnet_set("cert_file", "<data>/webrtc.pem"): the same PEM (certificate then key) is read as is, the fingerprint stays |
[master] country | ngnet_set("country", ...) |
[master] connect_tokens, SP_CONNECT_TOKENS | the game’s policy |
the announce fields (server_name, map_name, mode, protocol, players, ping_info(server), the 0.75 cap of 32) | ngnet_set_info(&(ngnet_info_t){name, map, mode, protocol, players_current, players_max, info_json}), called when they change (it is cheap; the announce is sent on change). info_json = ping_info() serialised. Protocol is any short string ("1.0", "0.75") |
join_code, listen_private | ngnet_set("join_code", code), ngnet_set("unlisted", "1") |
master_token_needed(conn) | the game’s, built on ngnet_master_listed() and ngnet_master_token_trusted(120) |
master_token_check(...) | ngnet_master_token_check(nonce, nlen, cert, clen, sig, slen, &why) |
master_stop() waits 3 s for leave | the same inside ngnet_server_stop() |
The HELLOINFO intercept (raw_udp_intercept_callback) stays the game’s: it knows the game’s ping_info. A server that sets host->intercept itself is untouched by the library.
Using the ENet shim in the game server (optional)
The game could also keep EnetConn.c and call the shim: compile the server with -include ngnet/enet.h; the ENetHost made by netaddr_host_create is made with enet_host_create and attaches itself, browsers appear as ENetPeers outside host->peers. The game’s per slot arrays (enet_conns[peer - host->peers]) do not fit browser peers, so the native API above is the right level for the game; the shim is for servers that cannot change.
Not extracted (still the game’s)
Net/Worker.c (the Web Worker transport), NetGuard, conn_send_reliable and the network stats, the HELLOINFO content, EnetConn.c sampling, the connect token *policy*.
Client
| before | now |
|---|---|
netlib/include/ngnet/{host,transport,conn,p2p}.h | ngnet/{host,transport,conn,p2p}.h (+ ngnet.h, masterlist.h) |
transport.c (driver) | in the library: transport_init, transport_connect, transport_service, … same signatures |
transport_use_address() | same |
transport_use_code() | transport_use_webrtc() (0 when the build has none) |
transport_has_code_join() | transport_has_webrtc() |
transport_connect(code, 0, 2, P10_CONNECT_MAGIC) after transport_use_code() | transport_connect_endpoint(&(ngnet_endpoint_t){.master = base, .join_code = code}, 2, connect_data) |
browser: transport_connect(host, port, ...) resolving the id with master_find_id/_master/_ice | the game finds the row, then transport_connect_endpoint(&(ngnet_endpoint_t){.master = row.master, .server_id = row.id, .ice_json = row.ice_json}, channels, connect_data). The library holds no allowlist and no connect data check (drop P10_CONNECT_MAGIC / VERSION_075 / VERSION_076 tests and the “Only our servers can be joined” refusal: the game decides before calling) |
SP_STUN | ngnet_endpoint_t.stun (comma separated), used when the row gives no ice_json |
tr("The master server could not be reached") etc. | transport_error_code() (NGNET_ERR_*) and transport_error() (English; the server’s own 503 reason, e.g. “server full”, is passed as it came). Set ngnet_set_tr(tr) to translate the defaults, or switch on the code |
transport_error() | same, plus transport_error_code() |
transport_connect_wait() | same |
transport_use_demo(), transport_demo_impl, demo_record_* in transport_service | transport_use_impl(&transport_demo_impl, "demo") and transport_set_observer(fn, user) (called for every event from a real connection: record there). transport_use_worker(): transport_use_impl(&transport_worker_impl, "worker"); struct transport_impl gained connect_endpoint (NULL) and error_code/error_detail in place of error |
transport_enet.c with netaddr_* (dual stack resolve, netaddr_host_create) | ngnet’s own is simpler: enet_address_set_host (IPv6 with enet6, ENET_ADDRESS_TYPE_ANY). It is not a drop in for the game’s dual stack behaviour: keep the game’s ENet transport as a transport_impl and register it with transport_use_impl, or use ngnet’s when plain resolution is enough. transport_enet_peer() is the same API |
transport_rtc.c (naett http.h, parson, lang.h, window.h) | ngnet/src/transport_rtc.c: libcurl on its own thread and the library’s JSON reader; build with NGNET_CLIENT + NGNET_WEBRTC (NGNET_CLIENT_WEBRTC defined) |
transport_webrtc.c + web/net.js sd_rtc_* | src/transport_web.c + web/ngnet_web.js (ngnet_rtc_connect(offerUrl, ice, connectData), ngnet_rtc_peek/pop/send/close/rtt/error; the JS builds no URL: C gives the offer URL). Link with --js-library ngnet/web/ngnet_web.js |
net.js sd_http_* (fetch for http.h) | not moved: the game keeps them for its own requests; use ngnet_list_parse on what they fetch |
master.c list reading (/serverlist.json, master_add) | ngnet_list_fetch / ngnet_list_start + ngnet_list_poll (native) or ngnet_list_parse (browser); rows are ngnet_list_row_t with master, id, webrtc, ice_json, info_json. The lists themselves (settings.masters, defaults, master_kind_of, 0.75 lists, aos:// identifier parsing, master_address, IPv4/IPv6 route choice) stay the game’s: the program passes the URLs |
master_url(), master_join_url() | the game builds endpoint.master; the join URLs are built by the library |
MASTER_URL_DEFAULT | the program’s: the library never picks a master for a client |
connecttoken.c (POST /v1/token) | not extracted (game protocol: ClientCertificate) |
transport_worker.c, workerbridge.js | not extracted (the page’s local server) |
p2p_code_* | same (ngnet/p2p.h) |
log_*, cevt | ngnet_set_log, ngnet_set_event |
Defaults to know
- Default master of the library:
https://master.shootanddestroy.obex.pw(the client’sMASTER_URL_DEFAULTand the server’sconfig.tomlboth have it;master.ngspades.commentioned in the task is not what they use). - Server side
receivehas the channel;closedhas no reason (a browser’sdisconnect <n>reason is not reported). conn_tmemory belongs to the library untilin_usesays no.- Everything is single threaded from the program’s side: call
ngnet_*from the tick thread only.