Skip to content
modkitv0.2

Multiplayer sessions

The optional modkit/multiplayer.h package is a server-authoritative layer on top of mk_peer. It owns its hosts and channel table and gives one session a shared peer set across UDP, TCP and WebSocket listeners.

Enable it with --multiplayer. The flag enables its required networking dependency. The independent modkit/codec.h reader/writer API is always compiled and can also be used for saves and game-module ABI messages.

mk_multiplayer_host_desc_t host;
mk_multiplayer_host_desc_init(&host);
host.transport = MK_TRANSPORT_UDP;
host.bind_address = "0.0.0.0";
host.port = 7777;
host.max_peers = 64;
mk_multiplayer_desc_t desc;
mk_multiplayer_desc_init(&desc);
desc.role = MK_HOST_SERVER;
desc.hosts = &host;
desc.host_count = 1;
desc.app_protocol_id = 0x4d594741;
mk_multiplayer_t session = MK_MULTIPLAYER_INVALID;
mk_multiplayer_create(&desc, &session);

Register stable RPC method IDs, snapshot streams and client input streams before connecting or polling. Then call mk_multiplayer_poll() from the owner thread. All callbacks run inline; incoming payload views remain valid through later callbacks in the same poll and expire when the poll returns.

Requests complete exactly once and can be replied to immediately or deferred. Input sequence acknowledgements ride in snapshot headers. Snapshot sampling is based on synchronized server time and reports interpolate, hold or extrapolate state. TCP and WebSocket strengthen unreliable delivery to reliable, while latest-state snapshot slots still coalesce stale unsent updates.

The default simulation rate is 60 Hz and the default snapshot rate is 30 Hz. All timeouts and send pacing use monotonic wall-clock time, independently of how often the application polls.

Direction checks only route messages; they are not authentication or gameplay authorization. Validate the peer and requested action in every server handler.

The complete ownership, bandwidth, codec, RPC and interpolation contracts are in the repository’s docs/multiplayer.md, with runnable code in examples/86_multiplayer.