Don't immediately send heartbeat
All checks were successful
CI / Formatting (push) Successful in 1m11s

This commit is contained in:
Michael Bradley 2025-07-05 18:09:46 -04:00
parent 591cfee715
commit e58629c2f1
Signed by: MichaelBradley
SSH key fingerprint: SHA256:o/aaeYtRubILK7OYYjYP12DmU7BsPUhKji1AgaQ+ge4
5 changed files with 50 additions and 46 deletions

View file

@ -4,6 +4,7 @@ use bevy::prelude::*;
use uuid::Uuid;
use super::{
packet::{InboundPacket, OutboundPacket, Packet},
peer::{Peer, PeerChangeEvent, PeerMap, PeerReceiveTiming, PeerSendTiming},
queues::{NetworkReceive, NetworkSend},
};
@ -65,11 +66,9 @@ pub fn heartbeat(
mut outbound: EventWriter<OutboundPacket>,
) {
for (peer, last) in peers {
if let Some(previous) = last.timestamp() {
// Allow for 2 consecutive missed heartbeats without timing out
if previous + TIMEOUT / 3 > time.elapsed() {
continue;
}
// Allow for 2 consecutive missed heartbeats without timing out
if last.time() + TIMEOUT / 3 > time.elapsed() {
continue;
}
outbound.write(OutboundPacket(Packet::new(Vec::new(), peer.uuid)));
}
@ -81,7 +80,7 @@ pub fn timeouts(
mut delete: EventWriter<PeerChangeEvent>,
) {
for (peer, last) in peers {
if let Some(previous) = last.timestamp() {
if let Some(previous) = last.time() {
if previous + TIMEOUT < time.elapsed() {
warn!("Peer {} timed out", peer.uuid);
delete.write(PeerChangeEvent::new(peer.uuid, None));
@ -100,21 +99,3 @@ impl Config {
Self { id: Uuid::new_v4() }
}
}
#[derive(Debug)]
pub struct Packet {
pub message: Vec<u8>,
pub peer: Uuid,
}
impl Packet {
pub fn new(message: Vec<u8>, peer: Uuid) -> Self {
Self { peer, message }
}
}
#[derive(Debug, Event)]
pub struct OutboundPacket(pub Packet);
#[derive(Debug, Event)]
pub struct InboundPacket(pub Packet);