Convert potential peer ping to a timer run condition
This commit is contained in:
parent
27b4644730
commit
1ba4b96863
2 changed files with 15 additions and 31 deletions
|
|
@ -11,11 +11,9 @@ use super::{
|
||||||
queues::NetworkSend,
|
queues::NetworkSend,
|
||||||
};
|
};
|
||||||
|
|
||||||
const PING_FREQUENCY: Duration = Duration::from_secs(3);
|
pub const PING_FREQUENCY: Duration = Duration::from_secs(1);
|
||||||
const MISSED_PINGS: u32 = 3;
|
const MISSED_PINGS: u32 = 5;
|
||||||
|
|
||||||
// TODO: Perhaps this needs a state rethink, is Single/Multiplayer actually useful vs Disconnected, Connecting, Connected?
|
|
||||||
// Would also help to state-scope some of these things, like InitialAddresses vs PeerMap
|
|
||||||
pub fn heartbeat(
|
pub fn heartbeat(
|
||||||
peers: Query<(&PeerID, &PeerSendTiming)>,
|
peers: Query<(&PeerID, &PeerSendTiming)>,
|
||||||
time: Res<Time>,
|
time: Res<Time>,
|
||||||
|
|
@ -44,34 +42,16 @@ pub fn timeout(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Resource)]
|
|
||||||
pub struct PotentialPeerTimer {
|
|
||||||
timer: Timer,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for PotentialPeerTimer {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self {
|
|
||||||
timer: Timer::new(PING_FREQUENCY, TimerMode::Repeating),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn ping_potential_peers(
|
pub fn ping_potential_peers(
|
||||||
mut timer: ResMut<PotentialPeerTimer>,
|
|
||||||
time: Res<Time>,
|
|
||||||
peers: Res<PotentialPeers>,
|
peers: Res<PotentialPeers>,
|
||||||
to_socket: Res<NetworkSend>,
|
to_socket: Res<NetworkSend>,
|
||||||
config: Res<Config>,
|
config: Res<Config>,
|
||||||
) -> Result {
|
) -> Result {
|
||||||
timer.timer.tick(time.delta());
|
for peer in &peers.addresses {
|
||||||
if timer.timer.is_finished() {
|
to_socket.send(
|
||||||
for peer in &peers.addresses {
|
format_message(&Vec::new(), PacketType::Peer, config.id),
|
||||||
to_socket.send(
|
*peer,
|
||||||
format_message(&Vec::new(), PacketType::Peer, config.id),
|
)?;
|
||||||
*peer,
|
|
||||||
)?;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,9 @@
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
|
|
||||||
use bevy::prelude::*;
|
use bevy::{prelude::*, time::common_conditions::on_timer};
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
heartbeat::{PotentialPeerTimer, heartbeat, ping_potential_peers, timeout},
|
heartbeat::{PING_FREQUENCY, heartbeat, ping_potential_peers, timeout},
|
||||||
io::{Config, handle_network_input, handle_network_output},
|
io::{Config, handle_network_input, handle_network_output},
|
||||||
packet::{InboundPacket, OutboundPacket},
|
packet::{InboundPacket, OutboundPacket},
|
||||||
peer::{
|
peer::{
|
||||||
|
|
@ -39,12 +39,16 @@ impl Plugin for NetIOPlugin {
|
||||||
)
|
)
|
||||||
.add_systems(
|
.add_systems(
|
||||||
FixedUpdate,
|
FixedUpdate,
|
||||||
(heartbeat, timeout, handle_new_peer, ping_potential_peers),
|
(
|
||||||
|
heartbeat,
|
||||||
|
timeout,
|
||||||
|
handle_new_peer,
|
||||||
|
ping_potential_peers.run_if(on_timer(PING_FREQUENCY)),
|
||||||
|
),
|
||||||
)
|
)
|
||||||
.add_systems(FixedPostUpdate, handle_network_output)
|
.add_systems(FixedPostUpdate, handle_network_output)
|
||||||
.init_resource::<Config>()
|
.init_resource::<Config>()
|
||||||
.init_resource::<PeerMap>()
|
.init_resource::<PeerMap>()
|
||||||
.init_resource::<PotentialPeerTimer>()
|
|
||||||
.insert_resource(PotentialPeers::new(self.initial_peers.clone()))
|
.insert_resource(PotentialPeers::new(self.initial_peers.clone()))
|
||||||
.add_message::<PeerChangeMessage>()
|
.add_message::<PeerChangeMessage>()
|
||||||
.add_message::<InboundPacket>()
|
.add_message::<InboundPacket>()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue