Skip to content

Commit

Permalink
Merge pull request #22 from HeatXD/fix-disconnection-bug
Browse files Browse the repository at this point in the history
fix disconnection bug stated in #2
  • Loading branch information
HeatXD authored Oct 28, 2024
2 parents fd133e6 + fe87a7c commit 34c1c46
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
6 changes: 6 additions & 0 deletions Examples/OnlineSession/OnlineSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,16 @@ int main(int argc, char* args[])
auto event = events[i];

printf("EV: %d\n", event->type);

if (event->type == DesyncDetected) {
auto desync = event->data.desynced;
printf("desync detected, f:%d, rh:%d, lc:%u, rc:%u\n", desync.frame, desync.remote_handle, desync.local_checksum, desync.remote_checksum);
}

if (event->type == PlayerDisconnected) {
auto disco = event->data.disconnected;
printf("disconnect detected, player: %d\n", disco.handle);
}
}

count = 0;
Expand Down
1 change: 1 addition & 0 deletions GekkoLib/include/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ namespace Gekko {
Frame last_acked_frame;
u64 last_sent_sync_message;
u64 last_received_message = -1;
u64 last_received_frame = 0;
};

struct NetInputData {
Expand Down
25 changes: 23 additions & 2 deletions GekkoLib/src/backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,10 +311,24 @@ void Gekko::MessageSystem::HandleTooFarBehindActors(bool spectator)

for (auto& player : spectator ? spectators : remotes) {
if (player->GetStatus() == Connected) {
const u32 diff = last_added - player->stats.last_acked_frame;
const u64 input_diff = now - player->stats.last_received_frame;

if (!spectator && input_diff > NetStats::DISCONNECT_TIMEOUT) {
// give them one chance to redeem themselves
if (player->stats.last_received_frame == 0) {
player->stats.last_received_frame = now;
return;
}
session_events.AddPlayerDisconnectedEvent(player->handle);
player->SetStatus(Disconnected);
player->sync_num = 0;
return;
}

const u32 ack_diff = last_added - player->stats.last_acked_frame;
const u64 msg_diff = now - player->stats.last_received_message;

if (diff > max_diff || msg_diff > NetStats::DISCONNECT_TIMEOUT) {
if (ack_diff > max_diff || msg_diff > NetStats::DISCONNECT_TIMEOUT) {
session_events.AddPlayerDisconnectedEvent(player->handle);
player->SetStatus(Disconnected);
player->sync_num = 0;
Expand Down Expand Up @@ -538,6 +552,13 @@ void Gekko::MessageSystem::OnInputs(NetAddress& addr, NetPacket& pkt)
net_input->input.start_frame = body->start_frame;
net_input->input.total_size = (u16)net_input->input.inputs.size();

for (auto handle : net_input->handles) {
auto player = GetPlayerByHandle(handle);
if (player) {
player->stats.last_received_frame = TimeSinceEpoch();
}
}

_received_inputs.push(std::move(net_input));
}

Expand Down

0 comments on commit 34c1c46

Please sign in to comment.