More complete Peer distribution
All checks were successful
CI / Formatting (push) Successful in 45s

Slightly better implementation of peers, still need to create a more generic system for deciding which components to distribute where and then use that for Peers.
This commit is contained in:
Michael Bradley 2025-10-13 01:06:31 -04:00
parent e013fb427a
commit 53fe3333f0
Signed by: MichaelBradley
SSH key fingerprint: SHA256:BKO2eI2LPsCbQS3n3i5SdwZTAIV3F1lHezR07qP+Ob0
14 changed files with 438 additions and 265 deletions

View file

@ -1,65 +1,59 @@
use std::net::SocketAddr;
use bevy::prelude::*;
use uuid::Uuid;
use super::{
io::{handle_network_input, handle_network_output, heartbeat, timeout},
heartbeat::{PotentialPeerTimer, heartbeat, ping_potential_peers, timeout},
io::{Config, handle_network_input, handle_network_output},
packet::{InboundPacket, OutboundPacket},
peer::{PeerChangeEvent, PeerData, PeerMap, handle_new_peer, handle_peer_change},
peer::{
PeerChangeEvent, PeerMap, PotentialPeers, handle_new_peer, handle_peer_change,
new_peer_message,
},
queues::{NetworkReceive, NetworkSend},
socket::bind_socket,
state::NetworkState,
};
pub struct NetIOPlugin {
listen: u16,
peer: Option<SocketAddr>,
initial_peers: Vec<SocketAddr>,
}
impl NetIOPlugin {
pub fn new(listen: u16, peer: Option<SocketAddr>) -> Self {
Self { listen, peer }
pub fn maybe_peer(listen: u16, peer: Option<SocketAddr>) -> Self {
Self {
listen,
initial_peers: match peer {
Some(addr) => vec![addr],
None => Vec::new(),
},
}
}
}
impl Plugin for NetIOPlugin {
fn build(&self, app: &mut App) {
app.init_state::<NetworkState>()
.add_systems(
FixedPreUpdate,
(handle_network_input, handle_peer_change)
.chain()
.run_if(in_state(NetworkState::MultiPlayer)),
)
.add_systems(
FixedUpdate,
(heartbeat, timeout, handle_new_peer).run_if(in_state(NetworkState::MultiPlayer)),
)
.add_systems(
FixedPostUpdate,
handle_network_output.run_if(in_state(NetworkState::MultiPlayer)),
)
.add_event::<PeerChangeEvent>()
.add_event::<InboundPacket>()
.add_event::<OutboundPacket>();
app.add_systems(
FixedPreUpdate,
(handle_network_input, (handle_peer_change, new_peer_message)).chain(),
)
.add_systems(
FixedUpdate,
(heartbeat, timeout, handle_new_peer, ping_potential_peers),
)
.add_systems(FixedPostUpdate, handle_network_output)
.init_resource::<Config>()
.init_resource::<PeerMap>()
.init_resource::<PotentialPeerTimer>()
.insert_resource(PotentialPeers::new(self.initial_peers.clone()))
.add_event::<PeerChangeEvent>()
.add_event::<InboundPacket>()
.add_event::<OutboundPacket>();
match bind_socket(self.listen) {
Ok((send, receive)) => {
app.insert_state(NetworkState::MultiPlayer)
.insert_resource(NetworkSend::new(send))
app.insert_resource(NetworkSend::new(send))
.insert_resource(NetworkReceive::new(receive));
let mut peer_map = PeerMap::default();
if let Some(socket) = self.peer {
let entity = app.world_mut().spawn(PeerData {
addr: socket.into(),
me: Uuid::nil(),
});
peer_map.insert(Uuid::nil(), entity.id());
}
app.insert_resource(peer_map);
}
Err(err) => {
warn!("Failed to set up networking: {err}");