75 for (
auto& remote :
_msg.remotes) {
82 for (
u32 i = 0; i <
_msg.locals.size(); i++) {
83 if (
_msg.locals[i]->handle == player) {
84 _sync.SetLocalDelay(player, delay);
97 std::unique_ptr<NetAddress> address;
100 address = std::make_unique<NetAddress>(addr->
data, addr->
size);
110 _msg.ReclaimDisconnectedSpectators();
112 if (
_msg.spectators.size() >=
_config.max_spectators) {
118 for (; new_handle < end_handle; new_handle++) {
119 const bool in_use = std::any_of(
120 _msg.spectators.begin(),
121 _msg.spectators.end(),
122 [new_handle](
const std::unique_ptr<Player>& player) {
123 return player->handle == new_handle;
132 auto spectator = std::make_unique<Player>(new_handle, type, address.get());
133 spectator->requires_spectator_state =
_started;
134 spectator->spectator_state_acked = !
_started;
135 _msg.spectators.push_back(std::move(spectator));
147 _msg.locals.push_back(std::make_unique<Player>(new_handle, type, address.get()));
151 if (addr ==
nullptr) {
155 _msg.remotes.push_back(std::make_unique<Player>(new_handle, type, address.get()));
165 if (!
_msg.DisconnectActor(actor)) {
180 _msg.SetDisconnectTimeout(timeout);
185 u8* inp = (
u8*)input;
187 for (
u32 i = 0; i <
_msg.locals.size(); i++) {
188 if (
_msg.locals[i]->handle == player) {
189 _sync.AddLocalInput(player, inp);
198 _msg.session_events.Reset();
246 _sync.IncrementFrame();
259 *count = (
i32)
_msg.session_events.GetRecentEvents().size();
260 return _msg.session_events.GetRecentEvents().data();
271 for (
auto& remote :
_msg.remotes) {
273 const f32 adv = remote->adv_history.GetAverageAdvantage();
274 ahead = found ? std::max(ahead, adv) : adv;
283 std::vector<std::unique_ptr<Player>>* current = &
_msg.remotes;
285 for (
u32 i = 0; i < 2; i++)
288 current = &
_msg.spectators;
291 for (
auto& actor : *current) {
292 if (actor->handle == player) {
293 actor->stats.UpdateBandwidth();
294 stats->
kb_sent = actor->stats.kb_sent_per_sec;
295 stats->
kb_received = actor->stats.kb_received_per_sec;
296 stats->
last_ping = actor->stats.LastRTT();
297 stats->
jitter = actor->stats.CalculateJitter();
298 stats->
avg_ping = actor->stats.CalculateAvgRTT();
325 return _replay.StartRecording(
_config,
_sync.GetCurrentFrame(), save_initial_state, disable_compression);
332 return _replay.StopRecording(length);
339 return _replay.PeekRecording(length);
353 return _replay.SetChecksumInterval(interval);
358 return _replay.SetUserData(data, length);
390 const Frame incorrect =
_sync.GetMinIncorrectFrame();
392 up_to = std::min(up_to, incorrect - 1);
400 const Frame current =
_sync.GetCurrentFrame();
402 const Frame incorrect =
_sync.GetMinIncorrectFrame();
404 Frame stored = confirmed;
407 stored = std::min(stored, incorrect - 1);
415 auto saved =
_storage.GetState(stored);
417 if (saved->frame == stored && saved->state_len > 0) {
418 _replay.RecordState(saved->state.get(), saved->state_len, stored);
439 auto entry = std::make_unique<StateEntry>();
440 entry->state = std::make_unique<u8[]>(
_config.state_size);
441 entry->state_len =
_config.state_size;
457 const Frame current =
_sync.GetCurrentFrame();
462 const Frame frame_to_save = std::min(current - 1, confirmed_frame);
464 _sync.SetCurrentFrame(sync_frame);
466 _sync.IncrementFrame();
468 for (
Frame frame = sync_frame + 1; frame < current; frame++) {
470 if (frame == frame_to_save) {
475 _sync.IncrementFrame();
479 assert(
_sync.GetCurrentFrame() == current);
484 if (!
_config.desync_detection) {
505 const Frame current =
_sync.GetCurrentFrame();
506 confirmed = (current -
_config.max_input_prediction_window) - 1;
516 auto sav =
_storage.GetState(confirmed);
518 assert(sav->frame == confirmed);
520 checksum = sav->checksum;
525 _msg.local_health[confirmed] = checksum;
527 _msg.SendSessionHealth(confirmed, checksum);
529 for (
auto iter =
_msg.local_health.begin();
530 iter !=
_msg.local_health.end(); ) {
531 if (iter->first < (confirmed - 100)) {
532 iter =
_msg.local_health.erase(iter);
553 const Frame current =
_sync.GetCurrentFrame();
565 _msg.SendNetworkHealth();
570 if (!
_config.desync_detection) {
574 for (
auto iter =
_msg.local_health.begin();
575 iter !=
_msg.local_health.end(); ) {
577 for (
auto& player :
_msg.remotes) {
578 if (player->session_health.count(iter->first)) {
579 if (player->session_health[iter->first] != iter->second) {
580 _msg.session_events.AddDesyncDetectedEvent(
584 player->session_health[iter->first]
587 player->session_health.erase(iter->first);
597 const Frame current =
_sync.GetCurrentFrame();
599 for (
auto& player :
_msg.remotes) {
604 const Handle handle = player->handle;
605 const Frame disc_frame = player->disconnect_frame;
607 auto& input_q =
_msg.GetNetPlayerQueue(handle);
608 const Frame last_added =
_msg.GetLastAddedInputFrom(handle);
609 const Frame oldest = last_added - (
Frame)input_q.size() + 1;
613 if (player->applied_disconnect_frame != INT32_MAX &&
614 player->applied_disconnect_frame < disc_frame) {
615 const Frame received =
_sync.GetLastReceivedFrom(handle);
616 const Frame raised_up_to = std::min(disc_frame, received);
617 for (
Frame frame = player->applied_disconnect_frame + 1; frame <= raised_up_to; frame++) {
618 if (frame >= oldest && frame <= last_added) {
619 _sync.OverwriteInput(handle, input_q[frame - oldest].get(), frame);
623 player->applied_disconnect_frame = disc_frame;
627 const Frame last_recv =
_sync.GetLastReceivedFrom(handle) + 1;
629 for (
Frame frame = last_recv; frame <= current; frame++) {
630 if (frame <= disc_frame && frame >= oldest && frame <= last_added) {
631 _sync.AddRemoteInput(handle, input_q[frame - oldest].get(), frame);
642 const Frame current =
_msg.GetLastAddedInput(
true) + 1;
645 std::unique_ptr<u8[]> inputs;
646 for (
Frame frame = current; frame <= confirmed; frame++) {
647 if (!
_sync.GetSpectatorInputs(inputs, frame)) {
650 _msg.AddSpectatorInput(frame, inputs.get());
660 const Frame oldest_input =
_msg.GetOldestSpectatorInput();
662 for (
auto& spectator :
_msg.spectators) {
663 if (spectator->GetStatus() !=
Connected ||
664 !spectator->requires_spectator_state || spectator->spectator_state_acked) {
668 const bool state_too_old = spectator->spectator_state_configured &&
669 spectator->spectator_state_frame + 1 < oldest_input;
670 if (spectator->spectator_state_configured && !state_too_old) {
676 const Frame incorrect =
_sync.GetMinIncorrectFrame();
678 newest_correct = std::min(newest_correct, incorrect - 1);
684 : std::max((
Frame)-1, state_frame - (
Frame)
_config.max_input_prediction_window - 1);
687 for (
Frame frame = state_frame; frame >= oldest_state; frame--) {
688 auto candidate =
_storage.GetState(frame);
689 if (candidate->frame == frame && candidate->state_len > 0 &&
690 candidate->state_len <=
_config.state_size) {
706 if (!state || state_frame + 1 < oldest_input ||
707 (spectator->spectator_state_configured &&
708 state_frame <= spectator->spectator_state_frame)) {
712 _msg.SetSpectatorState(
727 const Frame oldest_input =
_msg.GetOldestSpectatorInput();
728 bool state_needed =
false;
729 for (
auto& spectator :
_msg.spectators) {
730 if (spectator->GetStatus() !=
Connected ||
731 !spectator->requires_spectator_state || spectator->spectator_state_acked) {
735 if (!spectator->spectator_state_configured ||
736 spectator->spectator_state_frame + 1 < oldest_input) {
742 const Frame current =
_sync.GetCurrentFrame();
744 if (!state_needed || state_frame + 1 < oldest_input ||
750 if (state_frame == current - 1) {
759 if (sync_frame >= state_frame) {
763 auto saved =
_storage.GetState(sync_frame);
764 if (saved->frame != sync_frame || saved->state_len == 0) {
768 _sync.SetCurrentFrame(sync_frame);
770 _sync.IncrementFrame();
772 for (
Frame frame = sync_frame + 1; frame < current; frame++) {
774 if (frame == state_frame) {
779 _sync.IncrementFrame();
782 assert(
_sync.GetCurrentFrame() == current);
789 _sync.SetCurrentFrame(current - 1);
791 _sync.IncrementFrame();
798 current =
_sync.GetCurrentFrame();
799 const Frame min =
_sync.GetMinIncorrectFrame();
807 _sync.SetCurrentFrame(sync_frame);
809 _sync.IncrementFrame();
811 for (
Frame frame = sync_frame + 1; frame < current; frame++) {
813 if (!
_config.limited_saving || frame == frame_to_save) {
818 _sync.IncrementFrame();
821 const u32 resimulated = (
u32)(current - sync_frame - 1);
828 _sync.ClearIncorrectFramesUpTo(current);
831 assert(
_sync.GetCurrentFrame() == current);
843 auto data =
_host->receive_data(&length);
849 _msg.CheckStatusActors();
873 if (!
_msg.CheckStatusActors()) {
878 _msg.session_events.AddSessionStartedEvent();
890 for (
auto& remote :
_msg.remotes) {
891 if (remote->GetStatus() !=
Connected)
continue;
894 auto handle = remote->handle;
895 const Frame last_recv =
_sync.GetLastReceivedFrom(handle) + 1;
896 const Frame last_added =
_msg.GetLastAddedInputFrom(handle);
898 assert(last_added - last_recv <= 128);
900 auto& input_q =
_msg.GetNetPlayerQueue(handle);
901 const Frame min_frame = last_added - (
i32)input_q.size() + 1;
902 const Frame current_frame =
_sync.GetCurrentFrame();
904 for (
int i = last_recv; i <= last_added; i++) {
905 if (i >= min_frame) {
906 int current_idx = i - min_frame;
907 u8* input = input_q[current_idx].get();
908 _sync.AddRemoteInput(handle, input, i);
910 const i8 local_adv = (
i8)(current_frame - i + local_delay);
911 _msg.SendInputAck(handle, i, local_adv);
921 const Frame current =
_msg.GetLastAddedInput(
false) + 1;
924 auto input = std::make_unique<u8[]>(
_config.input_size);
925 for (
Frame frame = current; frame <= current + delay; frame++) {
926 for (
auto& player :
_msg.locals) {
927 if (!
_sync.GetLocalInput(player->handle, input, frame)) {
930 _msg.AddInput(frame, player->handle, input.get());
933 if (frame == current) {
934 const Frame current_frame =
_sync.GetCurrentFrame();
935 for (
auto& remote :
_msg.remotes) {
937 const i8 local_adv = (
i8)(current_frame -
_sync.GetLastReceivedFrom(remote->handle) + (
Frame)delay);
938 remote->adv_history.SetLocalAdvantage(local_adv);
939 remote->adv_history.Update(frame);
950 for (
auto& player :
_msg.locals) {
951 min = std::min(
_sync.GetLocalDelay(player->handle), min);
958 return _msg.remotes.empty() && !
_msg.locals.empty();
963 return _config.max_input_prediction_window == 0;
982 return diff >
_config.max_input_prediction_window;
988 return std::min(
_msg.GetDisconnectHoldFrame(),
_sync.GetMinReceivedFrame());
995 const Frame hold =
_msg.GetDisconnectHoldFrame();
997 if (hold == INT32_MAX) {
1029 _sync.SetRunaheadMode(
true);
1035 _sync.IncrementFrame();
1037 _sync.SetRunaheadMode(
false);
i32 AddActor(GekkoPlayerType type, GekkoNetAddress *addr) override
GameEventSystem _game_events
void HandleReceivedInputs()
void SetInputPredictionWindow(u8 window) override
void SetRunahead(u8 runahead) override
void NetworkStats(i32 player, GekkoNetworkStats *stats) override
void SetLocalDelay(i32 player, u8 delay) override
std::unique_ptr< u8[]> _disconnected_input
bool IsLockstepActive() const
void SessionIntegrityCheck()
void SetDisconnectTimeout(u32 timeout) override
void AddLocalInput(i32 player, void *input) override
void SendSpectatorInputs()
RollbackCounters _rollback_stats
bool StartRecording(bool save_initial_state, bool disable_compression) override
static const Frame HEALTH_CHECK_INTERVAL
Frame GetConfirmedFrame()
void HandleSavingConfirmedFrame()
void RecordConfirmedChecksums()
f32 FramesAhead() override
GekkoGameEvent ** UpdateSession(i32 *count) override
Frame _last_checksum_capture
void CaptureHealthState(Frame frame)
void RollbackStats(GekkoRollbackStats *stats) override
bool ShouldStallAdvance()
void Init(GekkoConfig *config) override
void NetworkPoll() override
void AddDisconnectedPlayerInputs()
void CaptureChecksumState(Frame frame)
Frame _last_sent_healthcheck
GekkoSessionEvent ** Events(i32 *count) override
Frame _runahead_start_frame
StateEntry _spectator_state
u8 _input_prediction_window
void CaptureSpectatorState()
bool DisconnectActor(i32 actor) override
void SendSessionHealthCheck()
void SkipUnreachableHealthFrame()
std::vector< std::unique_ptr< StateEntry > > _checksum_states
void SetNetAdapter(GekkoNetAdapter *adapter) override
void SendNetworkHealthCheck()
bool RecordChecksums(u32 interval) override
const u8 * StopRecording(u32 &length) override
bool SetReplayUserData(const u8 *data, u32 length) override
void PrepareSpectatorStates()
void RecordInitialState()
const u8 * PeekRecording(u32 &length) override
struct GekkoConfig GekkoConfig
unsigned int total_stalls
std::unique_ptr< u8[]> state