GekkoNet 1.0
C/C++ Peer to Peer Game Networking SDK
Loading...
Searching...
No Matches
gekkonet.h
Go to the documentation of this file.
1/*
2BSD 2-Clause License
3
4Copyright (c) 2024-2026, Jamie Meyer
5
6Redistribution and use in source and binary forms, with or without
7modification, are permitted provided that the following conditions are met:
8
91. Redistributions of source code must retain the above copyright notice, this
10 list of conditions and the following disclaimer.
11
122. Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
15
16THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*/
27#pragma once
28
29#ifdef __cplusplus
30extern "C" {
31#else
32#include <stdbool.h>
33#endif
34
35#ifdef _WIN32
36#ifdef GEKKONET_STATIC
37 // Static library - no import/export needed
38#define GEKKONET_API
39#else
40#ifdef GEKKONET_DLL_EXPORT
41 // Building the DLL
42#define GEKKONET_API __declspec(dllexport)
43#else
44 // Using the DLL
45#define GEKKONET_API __declspec(dllimport)
46#endif
47#endif
48#else
49 // Non-Windows platforms don't need special export macros
50#define GEKKONET_API
51#endif
52
53// GekkoNet is mostly self contained memory wise using internal memory buffers.
54// Technically the user shouldnt need to free or create any memory (look at the examples).
55// The only case the user needs to create memory is when slotting in their own GekkoNetAdapter
57
58typedef enum GekkoSessionType {
59 GekkoGameSession, // session for an active player.
60 GekkoStressSession, // session to test your local simulation for state desyncs.
61 GekkoSpectateSession, // session for spectators watching an active player.
62 GekkoReplaySession, // session which plays back a recorded replay.
64
65typedef struct GekkoConfig {
66 unsigned char num_players;
67 unsigned char max_spectators;
69 unsigned int spectator_delay;
70 unsigned int input_size;
71 unsigned int state_size;
74 unsigned int check_distance;
76
82
83typedef struct GekkoNetAddress {
84 void* data;
85 unsigned int size;
87
93
94typedef struct GekkoNetAdapter {
95 // send data to another peer
96 void (*send_data)(GekkoNetAddress* addr, const char* data, int length);
97 // receive all packets accumulated between the last frame and now
98 // the results within the array will be freed after use using the free_data function
99 // the array itself won't be touched
100 GekkoNetResult** (*receive_data)(int* length);
101 // free data function so gekkonet can cleanup data that the user created.
102 void (*free_data)(void* data_ptr);
104
111
112typedef struct GekkoGameEvent {
114
116 // events
118 int frame;
119 unsigned int input_len;
120 unsigned char* inputs;
124 struct GekkoSave {
125 int frame;
127 unsigned int* checksum;
128 unsigned int* state_len;
129 unsigned char* state;
131 struct GekkoLoad {
132 int frame;
133 unsigned int state_len;
134 unsigned char* state;
138
151
180
181typedef struct GekkoNetworkStats {
182 float kb_sent;
184 unsigned short last_ping;
185 float avg_ping;
186 float jitter;
188
189typedef struct GekkoRollbackStats {
190 unsigned int total_count;
191 unsigned int last_frames;
193 unsigned int max_frames;
194 unsigned int total_stalls;
196
197// Public Facing API
198
199// creates a session of the given type. the handle has to be empty.
200GEKKONET_API bool gekko_create(GekkoSession** session, GekkoSessionType session_type);
201
202// destroys the session and clears the handle.
204
205// applies the config to the session, call this before adding any actors.
206GEKKONET_API void gekko_start(GekkoSession* session, GekkoConfig* config);
207
208// sets the adapter the session sends and receives its packets with.
210
211// adds a player or spectator and returns its handle, or -1 when it doesnt fit the session.
212// remote actors require an address, local ones dont.
213// adding a spectator after the session starts also requires a nonzero state_size.
215
216// disconnects an actor without destroying the session.
217// disconnecting a remote actor drops every actor which shares its address.
218// disconnecting a local actor means leaving the session and drops every remote actor and spectator.
219GEKKONET_API bool gekko_disconnect_actor(GekkoSession* session, int actor);
220
221// sets the time in ms without received packets before an actor is disconnected automatically.
222// a timeout of 0 disables automatic disconnecting. defaults to 5000ms.
223GEKKONET_API void gekko_set_disconnect_timeout(GekkoSession* session, unsigned int timeout);
224
225// holds a local players input back by the given amount of frames before the
226// session uses it, trading input lag for less mispredictions.
227GEKKONET_API void gekko_set_local_delay(GekkoSession* session, int player, unsigned char delay);
228
229// simulates the given amount of frames ahead every update and rewinds them
230// on the next one, which hides local input delay.
231GEKKONET_API void gekko_set_runahead(GekkoSession* session, unsigned char runahead);
232
233// sets how many frames the session may predict ahead of its remote players.
234// the window is clamped to the configs max_input_prediction_window, which also
235// decides how many states the session keeps around for rolling back.
236// a window of 0 stops predicting but still corrects earlier mispredictions.
237GEKKONET_API void gekko_set_input_prediction_window(GekkoSession* session, unsigned char window);
238
239// hands the input of a local player to the session, once per player per frame.
240GEKKONET_API void gekko_add_local_input(GekkoSession* session, int player, void* input);
241
242// advances the session and returns the events the game has to handle in order.
244
245// returns the events the last update raised, like actors connecting or a replay finishing.
247
248// the amount of frames this session runs ahead of its slowest remote player, input delays included.
250
251// spectator sessions: received frames not played yet. 0 for other sessions.
253
254// copies the network stats gathered for a remote actor or spectator.
255GEKKONET_API void gekko_network_stats(GekkoSession* session, int player, GekkoNetworkStats* stats);
256
257// copies the rollback stats of the session. total_count is the number of rollbacks so far and the frames are the ones resimulated per rollback.
258// total_stalls is the number of updates which could not advance because inputs were missing.
260
261// sends and receives packets without advancing the session.
263
264// starts recording the confirmed inputs of a session. replay sessions cannot record.
265// when save_initial_state is set the session stores the gamestate the recording starts at,
266// issuing a save event whenever it doesnt hold one already, so playback can start from there.
267// the recorded inputs are packed unless disable_compression is set.
268GEKKONET_API bool gekko_start_recording(GekkoSession* session, bool save_initial_state, bool disable_compression);
269
270// stops the recording and returns the serialized replay.
271// the returned memory is owned by the session and stays valid
272// until the session records again or gets destroyed.
273GEKKONET_API const unsigned char* gekko_stop_recording(GekkoSession* session, unsigned int* length);
274
275// returns the replay recorded so far without stopping the recording, for example to keep it
276// on disk while the session runs. the memory is owned by the session and stays valid
277// until the next call to gekko_peek_recording or gekko_stop_recording.
278GEKKONET_API const unsigned char* gekko_peek_recording(GekkoSession* session, unsigned int* length);
279
280// stores the checksums the game returns for its saved frames in the current recording,
281// for every frame divisible by interval (1 records every saved frame, 0 none).
282// only frames whose inputs are confirmed are recorded. spectator sessions issue a save event
283// for each such frame to get its checksum. call after gekko_start_recording.
284// a replay session raises GekkoReplayDesync at the first frame whose checksum differs.
285GEKKONET_API bool gekko_record_checksums(GekkoSession* session, unsigned int interval);
286
287// stores a copy of application data in the current recording, like a build id or the
288// local player. a later call replaces it. call after gekko_start_recording.
289GEKKONET_API bool gekko_set_replay_user_data(GekkoSession* session, const void* data, unsigned int length);
290
291// loads a serialized replay into a replay session.
292// the config is stored within the replay so calling gekko_start is not needed.
293// a replay session saves every frame that has a recorded checksum and compares the
294// checksum the game returns with it.
295GEKKONET_API bool gekko_load_replay(GekkoSession* session, const unsigned char* replay_data, unsigned int length);
296
297// returns the application data of the replay loaded into a replay session,
298// or null when it has none. the memory is owned by the session.
299GEKKONET_API const unsigned char* gekko_replay_user_data(GekkoSession* session, unsigned int* length);
300
301#ifndef GEKKONET_NO_ASIO
302
303// creates the built in UDP adapter bound to the given port.
304// calling this again closes the socket the previous call opened.
305// returns null when the socket can not be opened.
307
308// closes the socket the built in adapter uses.
310
311#endif // GEKKONET_NO_ASIO
312
313#ifdef __cplusplus
314}
315#endif
GEKKONET_API void gekko_set_local_delay(GekkoSession *session, int player, unsigned char delay)
Definition gekkonet.cpp:74
GEKKONET_API GekkoNetAdapter * gekko_default_adapter(unsigned short port)
Definition gekkonet.cpp:288
GEKKONET_API void gekko_add_local_input(GekkoSession *session, int player, void *input)
Definition gekkonet.cpp:89
GEKKONET_API const unsigned char * gekko_peek_recording(GekkoSession *session, unsigned int *length)
Definition gekkonet.cpp:147
GEKKONET_API bool gekko_default_adapter_destroy()
Definition gekkonet.cpp:322
GekkoSessionType
Definition gekkonet.h:58
@ GekkoGameSession
Definition gekkonet.h:59
@ GekkoSpectateSession
Definition gekkonet.h:61
@ GekkoStressSession
Definition gekkonet.h:60
@ GekkoReplaySession
Definition gekkonet.h:62
GEKKONET_API bool gekko_load_replay(GekkoSession *session, const unsigned char *replay_data, unsigned int length)
Definition gekkonet.cpp:170
GEKKONET_API void gekko_set_disconnect_timeout(GekkoSession *session, unsigned int timeout)
Definition gekkonet.cpp:69
GEKKONET_API bool gekko_create(GekkoSession **session, GekkoSessionType session_type)
Definition gekkonet.cpp:8
GEKKONET_API bool gekko_record_checksums(GekkoSession *session, unsigned int interval)
Definition gekkonet.cpp:160
GEKKONET_API void gekko_start(GekkoSession *session, GekkoConfig *config)
Definition gekkonet.cpp:49
GEKKONET_API const unsigned char * gekko_replay_user_data(GekkoSession *session, unsigned int *length)
Definition gekkonet.cpp:175
GekkoSessionEventType
Definition gekkonet.h:139
@ GekkoPlayerDisconnected
Definition gekkonet.h:143
@ GekkoPlayerSyncing
Definition gekkonet.h:141
@ GekkoPlayerConnected
Definition gekkonet.h:142
@ GekkoReplayFinished
Definition gekkonet.h:148
@ GekkoEmptySessionEvent
Definition gekkonet.h:140
@ GekkoReplayDesync
Definition gekkonet.h:149
@ GekkoDesyncDetected
Definition gekkonet.h:147
@ GekkoSpectatorPaused
Definition gekkonet.h:145
@ GekkoSpectatorUnpaused
Definition gekkonet.h:146
@ GekkoSessionStarted
Definition gekkonet.h:144
GEKKONET_API void gekko_network_poll(GekkoSession *session)
Definition gekkonet.cpp:124
GEKKONET_API GekkoGameEvent ** gekko_update_session(GekkoSession *session, int *count)
Definition gekkonet.cpp:94
GEKKONET_API void gekko_network_stats(GekkoSession *session, int player, GekkoNetworkStats *stats)
Definition gekkonet.cpp:114
GekkoPlayerType
Definition gekkonet.h:77
@ GekkoSpectator
Definition gekkonet.h:80
@ GekkoLocalPlayer
Definition gekkonet.h:78
@ GekkoRemotePlayer
Definition gekkonet.h:79
GEKKONET_API bool gekko_set_replay_user_data(GekkoSession *session, const void *data, unsigned int length)
Definition gekkonet.cpp:165
#define GEKKONET_API
Definition gekkonet.h:50
GEKKONET_API void gekko_rollback_stats(GekkoSession *session, GekkoRollbackStats *stats)
Definition gekkonet.cpp:119
GEKKONET_API float gekko_frames_ahead(GekkoSession *session)
Definition gekkonet.cpp:104
GEKKONET_API int gekko_spectator_buffered_frames(GekkoSession *session)
Definition gekkonet.cpp:109
GEKKONET_API bool gekko_start_recording(GekkoSession *session, bool save_initial_state, bool disable_compression)
Definition gekkonet.cpp:129
GEKKONET_API int gekko_add_actor(GekkoSession *session, GekkoPlayerType player_type, GekkoNetAddress *addr)
Definition gekkonet.cpp:59
GEKKONET_API bool gekko_destroy(GekkoSession **session)
Definition gekkonet.cpp:38
GEKKONET_API void gekko_set_runahead(GekkoSession *session, unsigned char runahead)
Definition gekkonet.cpp:79
GEKKONET_API void gekko_set_input_prediction_window(GekkoSession *session, unsigned char window)
Definition gekkonet.cpp:84
GekkoGameEventType
Definition gekkonet.h:105
@ GekkoLoadEvent
Definition gekkonet.h:109
@ GekkoSaveEvent
Definition gekkonet.h:108
@ GekkoEmptyGameEvent
Definition gekkonet.h:106
@ GekkoAdvanceEvent
Definition gekkonet.h:107
GEKKONET_API bool gekko_disconnect_actor(GekkoSession *session, int actor)
Definition gekkonet.cpp:64
GEKKONET_API const unsigned char * gekko_stop_recording(GekkoSession *session, unsigned int *length)
Definition gekkonet.cpp:134
GEKKONET_API void gekko_net_adapter_set(GekkoSession *session, GekkoNetAdapter *adapter)
Definition gekkonet.cpp:54
GEKKONET_API GekkoSessionEvent ** gekko_session_events(GekkoSession *session, int *count)
Definition gekkonet.cpp:99
unsigned char max_spectators
Definition gekkonet.h:67
unsigned char num_players
Definition gekkonet.h:66
unsigned char max_input_prediction_window
Definition gekkonet.h:68
unsigned int input_size
Definition gekkonet.h:70
unsigned int state_size
Definition gekkonet.h:71
unsigned int spectator_delay
Definition gekkonet.h:69
bool limited_saving
Definition gekkonet.h:72
unsigned int check_distance
Definition gekkonet.h:74
bool desync_detection
Definition gekkonet.h:73
GekkoGameEventType type
Definition gekkonet.h:113
union GekkoGameEvent::GekkoEventData data
void(* free_data)(void *data_ptr)
Definition gekkonet.h:102
void(* send_data)(GekkoNetAddress *addr, const char *data, int length)
Definition gekkonet.h:96
unsigned int size
Definition gekkonet.h:85
GekkoNetAddress addr
Definition gekkonet.h:89
unsigned int data_len
Definition gekkonet.h:90
void * data
Definition gekkonet.h:91
unsigned short last_ping
Definition gekkonet.h:184
unsigned int max_frames
Definition gekkonet.h:193
unsigned int total_count
Definition gekkonet.h:190
unsigned int last_frames
Definition gekkonet.h:191
unsigned int total_stalls
Definition gekkonet.h:194
GekkoSessionEventType type
Definition gekkonet.h:153
union GekkoSessionEvent::GekkoSessionData data
struct GekkoGameEvent::GekkoEventData::GekkoLoad load
struct GekkoGameEvent::GekkoEventData::GekkoAdvance adv
struct GekkoGameEvent::GekkoEventData::GekkoSave save
struct GekkoSessionEvent::GekkoSessionData::GekkoDesynced desynced
struct GekkoSessionEvent::GekkoSessionData::GekkoDisconnected disconnected
struct GekkoSessionEvent::GekkoSessionData::GekkoReplayDesynced replay_desynced
struct GekkoSessionEvent::GekkoSessionData::GekkoSyncing syncing
struct GekkoSessionEvent::GekkoSessionData::GekkoConnected connected