The browser wire

What a DataChannel client (a browser, or the desktop client’s libdatachannel) and ngnet’s server transport say to each other. It is the game server’s existing contract, unchanged, so browsers already deployed keep working.

Signalling

No trickle ICE: the client waits until its ICE gathering is complete (at most 5 s), then posts the offer once to a master:

The master relays the offer to the server’s long poll; the server answers within 10 s (it gives up at 8 s with a reject), and the master returns {"sdp": "<answer>"}. The answer holds every candidate of the server. Errors: 404 not listed (or no such code), 409 the server takes no WebRTC, 429 too many offers (10 a minute per address), 503 the server rejected the offer ({"error": "server full"} etc.), 504 no answer.

One UDP port

The server uses libdatachannel with libjuice and the ICE UDP mux: every peer shares one UDP port (webrtc_port, default game port + 1), so a firewall needs one rule. STUN and TURN servers come from stun, turn, turn_username, turn_credential; the same list is published in the server’s row (ice_servers) so the client uses the same ones. The server’s DTLS certificate is made in memory at every start, or once and kept in cert_file when set, and its fingerprint is published in the row (dtls_fingerprint).

DataChannels

The client opens two DataChannels before the offer:

labeloptionsENet equivalent
reliabledefaults (ordered, reliable)channel 0, reliable
unreliableordered: false, maxRetransmits: 0channel 1, unreliable

Any other label is closed by the server. A binary message is one game packet (the bytes ENet would carry, one per message). The contract promises messages of at most 16 KB; larger ones are sent anyway and logged (EVT webrtc action=too_big). A client should not queue unreliable packets behind a slow link (the game client drops them when more than 64 KB are buffered).

Control lines (text messages)

directionmessagemeaning
client to serverconnect <n> on reliableonce both channels are open and before any game packet; n is the opaque uint32 connect data (decimal). The server hands it to the program as ENet’s connect data (ngnet_server_events.connect, or ENET_EVENT_TYPE_CONNECT event.data in the shim). A game packet before it is dropped.
client to serverdisconnect <reason>the client leaves; the slot is freed now
server to clientdisconnect <reason>the server hangs up (ENet’s disconnect data); sent before closing, the server waits up to 2 s for it to be delivered (CONN_CLOSE / CONN_CLOSE_LATER)

Unknown text lines are ignored (a newer peer’s line is no reason to hang up). A channel or the connection closing without a disconnect is a timeout (reason 0).

Timings

answer an offer8 s
from the answer to connect <n>20 s, else the peer is dropped (why=no_connect)
goodbye flush2 s
offers or peers waiting for connectat most 32

Events (ngnet_set_event)

EVT webrtc action=offer|answer|channel|connected|closing|closed|text_ignored|too_big ...: structured lines for tests and logs, delivered to the hook, never required.