Skip to content
modkitv0.2

peer.h

#include <modkit/peer.h>19 functions · 3 structs · 5 enums · 5 typedefs · 2 macros

Transport-agnostic networking: hosts, peers, delivery channels.

An L1 "session" layer over the platform transport backends. Game code talks to an mk_host (a server OR a client endpoint) that produces mk_peers and exchanges messages on numbered channels with a chosen delivery guarantee. The same event-loop code runs on both client and server. mk_host_t* host = mk_host_create(&(mk_host_desc){ .role = MK_HOST_SERVER, .transport = MK_TRANSPORT_UDP, .bind_addr = "0.0.0.0", .port = 7777, .max_peers = 256, .channel_count = 2, .channels = { {MK_DELIVERY_UNRELIABLE_SEQUENCED}, {MK_DELIVERY_RELIABLE_ORDERED} }, .app_protocol_id = 0xCAFE0001, }); mk_peer_event ev; while (mk_host_service(host, &ev, 0)) { if (ev.type == MK_PEER_EVENT_RECEIVE) handle(ev.peer, ev.channel, ev.data, ev.len); } Unlike the raw UDP in net.h, message boundaries ARE preserved: every MK_PEER_EVENT_RECEIVE is exactly one mk_peer_send from the other side. The data pointer is owned by the host and is valid only until the next mk_host_service() call on that host (copy it if you need to keep it).

Functions

mk_host_broadcastfunction

void mk_host_broadcast(mk_host_t *host, uint8_t channel, const void *data, uint32_t len)

Perform the host broadcast operation.

ParameterTypeDescription
hostmk_host_t *Value for host.
channeluint8_tValue for channel.
dataconst void *Data buffer.
lenuint32_tValue for len.

mk_host_broadcast_exceptfunction

void mk_host_broadcast_except(mk_host_t *host, mk_peer_t *except, uint8_t channel, const void *data, uint32_t len)

Perform the host broadcast except operation.

ParameterTypeDescription
hostmk_host_t *Value for host.
exceptmk_peer_t *Value for except.
channeluint8_tValue for channel.
dataconst void *Data buffer.
lenuint32_tValue for len.

mk_host_connectfunction

mk_peer_t * mk_host_connect(mk_host_t *host, const char *remote, uint16_t port)

Client: initiate a connection.

Returns a peer in the CONNECTING state; a

ParameterTypeDescription
hostmk_host_t *Value for host.
remoteconst char *Value for remote.
portuint16_tValue for port.

Returns A borrowed pointer, or NULL when unavailable. MK_PEER_EVENT_CONNECT (or DISCONNECT on failure) follows from service().

mk_host_connect_urlfunction

mk_peer_t * mk_host_connect_url(mk_host_t *host, const char *url)

Client: connect a WebSocket host using an absolute ws:// or wss:// URL.

The URL may include a path, query, explicit port, or bracketed IPv6 address. Returns NULL for an invalid URL, non-client host, or non-WebSocket transport.

ParameterTypeDescription
hostmk_host_t *Value for host.
urlconst char *Value for URL.

Returns A borrowed pointer, or NULL when unavailable.

mk_host_createfunction

mk_host_t * mk_host_create(const mk_host_desc *desc)

Create host.

ParameterTypeDescription
descconst mk_host_desc *Configuration descriptor.

Returns A borrowed pointer, or NULL when unavailable.

mk_host_destroyfunction

void mk_host_destroy(mk_host_t *host)

Destroy the host.

ParameterTypeDescription
hostmk_host_t *Value for host.

mk_host_peer_countfunction

uint32_t mk_host_peer_count(mk_host_t *host)

Number of peers currently attached to a host.

ParameterTypeDescription
hostmk_host_t *Value for host.

Returns The resulting value.

mk_host_peer_firstfunction

mk_peer_t * mk_host_peer_first(mk_host_t *host)

First attached peer, or NULL.

ParameterTypeDescription
hostmk_host_t *Value for host.

Returns A borrowed pointer, or NULL when unavailable.

mk_host_servicefunction

int mk_host_service(mk_host_t *host, mk_peer_event *out, uint32_t timeout_ms)

Pump the network and dequeue ONE event.

Call repeatedly until it returns 0. Drives the transport event loop. timeout_ms 0 = non-blocking (clients/frame

ParameterTypeDescription
hostmk_host_t *Value for host.
outmk_peer_event *Value for out.
timeout_msuint32_tValue for timeout ms.

Returns The resulting value. loop); >0 may block up to that long waiting for I/O (headless server tick).

mk_peer_addressfunction

bool mk_peer_address(mk_peer_t *peer, char *host, size_t cap, uint16_t *port)

Perform the peer address operation.

ParameterTypeDescription
peermk_peer_t *Value for peer.
hostchar *Value for host.
capsize_tValue for cap.
portuint16_t *Value for port.

Returns True when the operation succeeds.

mk_peer_disconnectfunction

void mk_peer_disconnect(mk_peer_t *peer, mk_disconnect_reason_t reason)

Perform the peer disconnect operation.

ParameterTypeDescription
peermk_peer_t *Value for peer.
reasonmk_disconnect_reason_tValue for reason.

mk_peer_get_userfunction

uint64_t mk_peer_get_user(mk_peer_t *peer)

Get user from the peer.

ParameterTypeDescription
peermk_peer_t *Value for peer.

Returns The resulting value.

mk_peer_get_user_ptrfunction

void * mk_peer_get_user_ptr(mk_peer_t *peer)

Get user ptr from the peer.

ParameterTypeDescription
peermk_peer_t *Value for peer.

Returns A borrowed pointer, or NULL when unavailable.

mk_peer_hostfunction

mk_host_t * mk_peer_host(mk_peer_t *peer)

Perform the peer host operation.

ParameterTypeDescription
peermk_peer_t *Value for peer.

Returns A borrowed pointer, or NULL when unavailable.

mk_peer_nextfunction

mk_peer_t * mk_peer_next(mk_peer_t *peer)

Next peer in the host's list, or NULL at the end.

The iteration is invalidated by mk_host_service(), which is when

ParameterTypeDescription
peermk_peer_t *Value for peer.

Returns A borrowed pointer, or NULL when unavailable. disconnected peers are retired; re-walk it rather than holding pointers.

mk_peer_rtt_msfunction

uint32_t mk_peer_rtt_ms(mk_peer_t *peer)

0 if unknown

ParameterTypeDescription
peermk_peer_t *Value for peer.

Returns The resulting value. 0 if unknown

mk_peer_sendfunction

bool mk_peer_send(mk_peer_t *peer, uint8_t channel, const void *data, uint32_t len)

Send peer.

ParameterTypeDescription
peermk_peer_t *Value for peer.
channeluint8_tValue for channel.
dataconst void *Data buffer.
lenuint32_tValue for len.

Returns True when the operation succeeds.

mk_peer_set_userfunction

void mk_peer_set_user(mk_peer_t *peer, uint64_t user)

Set user on the peer.

ParameterTypeDescription
peermk_peer_t *Value for peer.
useruint64_tCaller-provided context.

mk_peer_set_user_ptrfunction

void mk_peer_set_user_ptr(mk_peer_t *peer, void *ptr)

A second, pointer-sized slot of per-peer application state.

One uint64 is

ParameterTypeDescription
peermk_peer_t *Value for peer.
ptrvoid *Value for ptr. not enough once a peer carries both a platform identity and an app object.

Structs

mk_channel_descstruct

Data for channel desc.

FieldTypeDescription
deliverymk_delivery_tThe delivery.
kcp_sndwnduint16_tUDP/KCP only; 0 = backend default.
kcp_rcvwnduint16_tThe kcp rcvwnd.

mk_host_descstruct

Data for host desc.

FieldTypeDescription
rolemk_host_role_tThe role.
transportmk_transport_tThe transport.
bind_addrconst char *server bind (NULL/"0.0.0.0"); client unused
portuint16_tThe port.
max_peersuint32_tserver capacity; 0 -> 1
channel_countuint8_tThe channel count.
channelsmk_channel_descThe channels.
app_protocol_iduint32_trejects clients with a different id

mk_peer_eventstruct

Data for peer event.

FieldTypeDescription
typemk_peer_event_type_tThe type.
peermk_peer_t *who the event is about
channeluint8_tRECEIVE: which channel.
reasonmk_disconnect_reason_tDISCONNECT only.
dataconst void *RECEIVE: one message; valid until next service().
lenuint32_tThe len.

Enums

mk_delivery_tenum

Values for delivery.

ValueDescription
MK_DELIVERY_UNRELIABLEraw datagram; may drop/reorder
MK_DELIVERY_UNRELIABLE_SEQUENCEDdrops stale; never out-of-order
MK_DELIVERY_RELIABLE_ORDEREDTCP-like: all messages, in order.

mk_disconnect_reason_tenum

Values for disconnect reason.

ValueDescription
MK_DISCONNECT_NORMALSelects normal.
MK_DISCONNECT_TIMEOUTSelects timeout.
MK_DISCONNECT_PROTOCOL_MISMATCHSelects protocol mismatch.
MK_DISCONNECT_SERVER_FULLSelects server full.
MK_DISCONNECT_TRANSPORT_ERRORSelects transport error.

mk_host_role_tenum

Values for host role.

ValueDescription
MK_HOST_SERVERAccept incoming peers.
MK_HOST_CLIENTInitiate an outgoing peer connection.

mk_peer_event_type_tenum

Values for peer event type.

ValueDescription
MK_PEER_EVENT_NONESelects none.
MK_PEER_EVENT_CONNECTSelects connect.
MK_PEER_EVENT_DISCONNECTSelects disconnect.
MK_PEER_EVENT_RECEIVESelects receive.

mk_transport_tenum

Values for transport.

ValueDescription
MK_TRANSPORT_UDPnative UDP; per-channel raw or KCP (reliable)
MK_TRANSPORT_TCPnative TCP; reliable-ordered only
MK_TRANSPORT_WSnative WebSocket (libhv)
MK_TRANSPORT_WS_WEBbrowser WebSocket via emscripten (client only)
MK_TRANSPORT_AUTOWS_WEB on web, else UDP.

Typedefs

mk_channel_desctypedef

typedef struct mk_channel_desc mk_channel_desc

Data for channel desc.

mk_host_desctypedef

typedef struct mk_host_desc mk_host_desc

Data for host desc.

mk_host_ttypedef

typedef struct mk_host mk_host_t

opaque endpoint (server or client)

mk_peer_eventtypedef

typedef struct mk_peer_event mk_peer_event

Data for peer event.

mk_peer_ttypedef

typedef struct mk_peer mk_peer_t

opaque connection, owned by its host

Macros

MK_MAX_CHANNELSdefine

MK_MAX_CHANNELS

Maximum supported channels.

MK_NET_PROTOCOL_VERSIONdefine

MK_NET_PROTOCOL_VERSION

Wire protocol version, exchanged in the handshake.

Bump on any framing or handshake change; peers with a different version are rejected.