Skip to content

Commit

Permalink
Only verify remote checksums once
Browse files Browse the repository at this point in the history
And fix bugs with checksum interval > 32
  • Loading branch information
johanhelsing committed Oct 20, 2023
1 parent b779440 commit 94604ce
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 25 deletions.
19 changes: 6 additions & 13 deletions src/network/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ where
last_recv_time: Instant,

// debug desync
checksum_history: HashMap<Frame, u128>,
pub(crate) pending_checksums: VecDeque<(Frame, u128)>,
last_added_checksum_frame: Frame,
}

Expand Down Expand Up @@ -261,7 +261,7 @@ impl<T: Config> UdpProtocol<T> {
last_recv_time: Instant::now(),

// debug desync
checksum_history: HashMap::new(),
pending_checksums: VecDeque::new(),
last_added_checksum_frame: NULL_FRAME,
}
}
Expand Down Expand Up @@ -711,13 +711,10 @@ impl<T: Config> UdpProtocol<T> {
/// Upon receiving a `ChecksumReport`, add it to the checksum history
fn on_checksum_report(&mut self, body: &ChecksumReport) {
if self.last_added_checksum_frame < body.frame {
if self.checksum_history.len() > MAX_CHECKSUM_HISTORY_SIZE {
let oldest_frame_to_keep = self.last_added_checksum_frame;
self.checksum_history
.retain(|&frame, _| frame >= oldest_frame_to_keep as i32);
}
self.last_added_checksum_frame = body.frame;
self.checksum_history.insert(body.frame, body.checksum);
self.pending_checksums
.truncate(MAX_CHECKSUM_HISTORY_SIZE - 1);
self.pending_checksums
.push_front((body.frame, body.checksum));
}
}

Expand All @@ -729,10 +726,6 @@ impl<T: Config> UdpProtocol<T> {
}
}

pub(crate) fn checksum_history(&self) -> &HashMap<Frame, u128> {
&self.checksum_history
}

pub(crate) fn send_checksum_report(&mut self, frame_to_send: Frame, checksum: u128) {
let body = ChecksumReport {
frame: frame_to_send,
Expand Down
29 changes: 17 additions & 12 deletions src/sessions/p2p_session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -875,20 +875,25 @@ impl<T: Config> P2PSession<T> {

fn compare_local_checksums_against_peers(&mut self) {
match self.desync_detection {
DesyncDetection::On { interval } => {
if self.current_frame() % interval as i32 != 0 {
return;
}

for remote in self.player_reg.remotes.values() {
for (remote_frame, remote_checksum) in remote.checksum_history() {
if let Some(local_checksum) = self.local_checksum_history.get(remote_frame)
DesyncDetection::On { .. } => {
for remote in self.player_reg.remotes.values_mut() {
while remote.pending_checksums.len() > 0 {
// todo: clean up when/if a drain filter variant is added for VecDeque
let (remote_frame, _) = remote.pending_checksums.back().unwrap();
if *remote_frame >= self.sync_layer.last_confirmed_frame() {
break;
}
if let Some(&local_checksum) =
self.local_checksum_history.get(&remote_frame)
{
if *local_checksum != *remote_checksum {
let (remote_frame, remote_checksum) =
remote.pending_checksums.pop_back().unwrap();

if local_checksum != remote_checksum {
self.event_queue.push_back(GGRSEvent::DesyncDetected {
frame: *remote_frame,
local_checksum: *local_checksum,
remote_checksum: *remote_checksum,
frame: remote_frame,
local_checksum,
remote_checksum,
addr: remote.peer_addr(),
});
}
Expand Down

0 comments on commit 94604ce

Please sign in to comment.