Implement very basic and bad and specific netcode
All checks were successful
CI / Formatting (push) Successful in 1m14s

The game can now retrieve its seed from another game on the network
This commit is contained in:
Michael Bradley 2025-05-25 00:54:16 -04:00
parent 50ef78f7aa
commit dad37262a5
Signed by: MichaelBradley
SSH key fingerprint: SHA256:o/aaeYtRubILK7OYYjYP12DmU7BsPUhKji1AgaQ+ge4
7 changed files with 116 additions and 5 deletions

View file

@ -8,6 +8,7 @@ use rand::random;
pub struct Seed(u64);
impl Seed {
/// Use a random integer as the seed
pub fn random() -> Self {
Self(random())
}
@ -30,3 +31,15 @@ impl From<Seed> for [u8; 8] {
value.0.to_le_bytes()
}
}
impl From<u64> for Seed {
fn from(value: u64) -> Self {
Seed(value)
}
}
impl From<Seed> for u64 {
fn from(value: Seed) -> Self {
value.0
}
}

View file

@ -14,6 +14,8 @@ use game::{
setup::{check_for_seed, setup_balls, setup_from_seed, setup_player, setup_ui, setup_walls},
};
mod net;
/// The initial configuration passed to the game's setup functions.
/// Also functions as a Bevy plugin to pass the configuration into the app.
#[derive(Parser)]
@ -63,6 +65,7 @@ impl Plugin for AppSettings {
PhysicsPlugins::default().with_length_unit(50.0),
#[cfg(feature = "dev")]
dev::dev_tools,
net::NetIOPlugin::new(self.port, self.source.connect),
))
.init_state::<AppState>()
.add_systems(Startup, setup_ui)
@ -80,7 +83,7 @@ impl Plugin for AppSettings {
.add_systems(
Update,
(
(move_player, move_camera.after(move_player), zoom_camera)
((move_player, move_camera).chain(), zoom_camera)
.run_if(in_state(AppState::InGame)),
quit.run_if(input_pressed(KeyCode::KeyQ)),
),
@ -89,8 +92,7 @@ impl Plugin for AppSettings {
if let Some(ref seed) = self.source.seed {
app.insert_resource(seed.clone());
} else if let Some(ref peer) = self.source.connect {
info!("Got peer: {peer}");
todo!("Handle connecting to peer and retrieving seed");
info!("Will retrieve seed from peer => {peer}");
} else {
app.insert_resource(Seed::random());
}

94
src/net.rs Normal file
View file

@ -0,0 +1,94 @@
use std::{
net::{Ipv6Addr, SocketAddr, UdpSocket},
thread,
};
use bevy::prelude::*;
use crossbeam_channel::{Receiver, Sender, unbounded};
use crate::game::seed::Seed;
#[derive(Resource)]
pub struct NetworkSend(Sender<(u64, SocketAddr)>);
#[derive(Resource)]
pub struct NetworkReceive(Receiver<(u64, SocketAddr)>);
fn handle_network_io(
receive: Res<NetworkReceive>,
send: Res<NetworkSend>,
seed: Option<Res<Seed>>,
mut commands: Commands,
) -> Result {
let Ok((message, address)) = receive.0.try_recv() else {
return Ok(());
};
if let Some(value) = seed {
send.0.try_send((value.clone().into(), address))?;
} else {
commands.insert_resource::<Seed>(message.into());
}
Ok(())
}
pub struct NetIOPlugin {
listen: u16,
peer: Option<SocketAddr>,
}
impl NetIOPlugin {
pub fn new(listen: u16, peer: Option<SocketAddr>) -> Self {
Self { listen, peer }
}
}
impl Plugin for NetIOPlugin {
fn build(&self, app: &mut App) {
app.add_systems(FixedUpdate, handle_network_io);
let (send, receive) = match UdpSocket::bind((Ipv6Addr::LOCALHOST, self.listen)) {
Ok(socket) => {
socket.set_read_timeout(None).unwrap();
socket.set_write_timeout(None).unwrap();
let (send_outbound, receive_outbound) = unbounded::<(u64, SocketAddr)>();
let send_socket = socket.try_clone().unwrap();
thread::spawn(move || {
loop {
match receive_outbound.recv() {
Ok((message, address)) => send_socket
.send_to(&message.to_le_bytes(), address)
.unwrap(),
Err(err) => {
error!("{err}");
break;
}
};
}
});
let (send_inbound, receive_inbound) = unbounded::<(u64, SocketAddr)>();
thread::spawn(move || {
loop {
let mut message = [0u8; 8];
let (len, address) = socket.recv_from(&mut message).unwrap();
info!("Received {len} bytes");
send_inbound
.try_send((u64::from_le_bytes(message), address))
.unwrap();
}
});
(send_outbound, receive_inbound)
}
Err(err) => {
error!("Could not bind socket: {err}");
todo!("bounded(0) is apparently meaningful so find another solution")
}
};
if let Some(socket) = self.peer {
send.try_send((0, socket)).unwrap();
}
app.insert_resource(NetworkSend(send));
app.insert_resource(NetworkReceive(receive));
}
}