GekkoNet 1.0
C/C++ Peer to Peer Game Networking SDK
Loading...
Searching...
No Matches
stress_session.cpp
Go to the documentation of this file.
2#include <cassert>
3#include <cstring>
4
5
12
14{
15 std::memcpy(&_config, config, sizeof(GekkoConfig));
16
17 // check distance
18 _check_distance = _config.check_distance;
19
20 // setup input buffer for the players
21 _sync.Init(_config.num_players, _config.input_size);
22
23 // setup game event system
24 _game_events.Init(_config.input_size * _config.num_players);
25
26 // setup state storage
27 _storage.Init(_check_distance, _config.state_size, false);
28
29 // setup checksum history for comparisons
30 _checksum_history.clear();
31}
32
34{
35 for (u32 i = 0; i < _locals.size(); i++) {
36 if (_locals[i].handle == player) {
37 _sync.SetLocalDelay(player, delay);
38 }
39 }
40}
41
43{
44 if (type != GekkoLocalPlayer) return -1;
45
46 u32 new_handle = (u32)_locals.size();
47 std::unique_ptr<NetAddress> address;
48
49 if (addr) {
50 address = std::make_unique<NetAddress>(addr->data, addr->size);
51 }
52
53 _locals.push_back(Player(new_handle, type, address.get()));
54
55 return new_handle;
56}
57
59{
60 u8* inp = (u8*)input;
61
62 for (u32 i = 0; i < _locals.size(); i++) {
63 if (_locals[i].handle == player) {
64 _sync.AddLocalInput(player, inp);
65 break;
66 }
67 }
68}
69
71{
72 _game_events.Clear();
73 _session_events.Reset();
74 _game_events.Reset();
75
76 // store the frames that went by for the replay
78
79 Frame current = _sync.GetCurrentFrame();
80 if (_check_distance > 0 && current > _check_distance) {
81 // once whe have gone far enough forward start rollback back and comparing checksums
82 for (Frame check_frame = current - _check_distance; check_frame < current; check_frame++) {
83 CheckForDesyncs(check_frame);
84 }
85 // rollback according to check distance
87 }
88
89 if (current - 1 == GameInput::NULL_FRAME - 1) {
90 // initial gamestate save
91 _game_events.AddSaveEvent(_sync, _storage);
92 }
93
94 // advance the session forward
95 if (_game_events.AddAdvanceEvent(_sync, false)) {
96 _game_events.AddSaveEvent(_sync, _storage);
97 _sync.IncrementFrame();
98 }
99
100 *count = _game_events.Count();
101 return _game_events.Data();
102}
103
105{
106 *count = (i32)_session_events.GetRecentEvents().size();
107 return _session_events.GetRecentEvents().data();
108}
109
110bool Gekko::StressSession::StartRecording(bool save_initial_state, bool disable_compression)
111{
112 return _replay.StartRecording(_config, _sync.GetCurrentFrame(), save_initial_state, disable_compression);
113}
114
116{
118
119 return _replay.StopRecording(length);
120}
121
123{
125
126 return _replay.PeekRecording(length);
127}
128
130{
131 // the game handled the events of the last update by now, keep what they confirmed.
132 if (_replay.IsRecording() && !_replay.NeedsState()) {
133 _replay.RecordInputs(_sync);
134 _replay.RecordChecksums(_storage, _sync.GetCurrentFrame() - 1);
135 }
136}
137
139{
140 return _replay.SetChecksumInterval(interval);
141}
142
144{
145 return _replay.SetUserData(data, length);
146}
147
149{
150 if (!_replay.IsRecording()) {
151 return;
152 }
153
154 if (_replay.NeedsState()) {
155 _game_events.AddStateSaveEvent(_sync.GetCurrentFrame() - 1, _replay.PendingState());
156 }
157
158 _replay.RecordInputs(_sync);
159
160 // a local session has no unconfirmed frames, every frame it saved is final.
161 _replay.RecordChecksums(_storage, _sync.GetCurrentFrame() - 1);
162}
163
165{
166 const Frame current = _sync.GetCurrentFrame();
167 const Frame past = current - _check_distance;
168
169 // load the sync frame
170 _sync.SetCurrentFrame(past);
171 _game_events.AddLoadEvent(_sync, _storage);
172 _sync.IncrementFrame();
173
174 for (Frame frame = past + 1; frame < current; frame++) {
175 _game_events.AddAdvanceEvent(_sync, true);
176 _game_events.AddSaveEvent(_sync, _storage);
177 _sync.IncrementFrame();
178 }
179
180 // make sure that we are back where we started.
181 assert(_sync.GetCurrentFrame() == current);
182}
183
185{
186 const Frame oldest = check_frame - _check_distance;
187
188 // clear old checksum frames
189 _checksum_history.erase(_checksum_history.begin(), _checksum_history.lower_bound(oldest));
190
191 const StateEntry* state = _storage.GetState(check_frame);
192 // check if there is a valid entry in the state storage
193 if (state->frame == check_frame) {
194 if (_checksum_history.count(check_frame)) {
195 // found in the history! compare checksums and send event if incorrect.
196 u32 recorded_checksum = _checksum_history[check_frame];
197 if (state->checksum != recorded_checksum) {
198 _session_events.AddDesyncDetectedEvent(check_frame, 0, state->checksum, recorded_checksum);
199 }
200 } else {
201 // not in the history? then add it.
202 _checksum_history.emplace(check_frame, state->checksum);
203 }
204 }
205}
bool StartRecording(bool save_initial_state, bool disable_compression) override
void Init(GekkoConfig *config) override
std::map< Frame, u32 > _checksum_history
const u8 * StopRecording(u32 &length) override
GekkoSessionEvent ** Events(i32 *count) override
GekkoGameEvent ** UpdateSession(i32 *count) override
i32 AddActor(GekkoPlayerType type, GekkoNetAddress *addr) override
SessionEventSystem _session_events
GameEventSystem _game_events
std::vector< Player > _locals
bool RecordChecksums(u32 interval) override
const u8 * PeekRecording(u32 &length) override
bool SetReplayUserData(const u8 *data, u32 length) override
void SetLocalDelay(i32 player, u8 delay) override
void CheckForDesyncs(Frame check_frame)
void AddLocalInput(i32 player, void *input) override
int32_t i32
Definition gekko_types.h:12
int32_t Frame
Definition gekko_types.h:18
uint8_t u8
Definition gekko_types.h:5
uint32_t u32
Definition gekko_types.h:7
struct GekkoConfig GekkoConfig
GekkoPlayerType
Definition gekkonet.h:77
@ GekkoLocalPlayer
Definition gekkonet.h:78
unsigned int size
Definition gekkonet.h:85
static const i32 NULL_FRAME
Definition input.h:21