Use Vec<u8>s for sending data between threads

This commit is contained in:
Michael Bradley 2025-05-25 17:12:05 -04:00
parent dd57bc30e1
commit ba7737671e
Signed by: MichaelBradley
SSH key fingerprint: SHA256:o/aaeYtRubILK7OYYjYP12DmU7BsPUhKji1AgaQ+ge4
5 changed files with 47 additions and 16 deletions

View file

@ -14,7 +14,7 @@ fn configure_socket(socket: &UdpSocket) -> io::Result<()> {
Ok(())
}
type NetworkMessage = (u64, SocketAddr);
type NetworkMessage = (Vec<u8>, SocketAddr);
fn start_network_thread<M: Send + 'static>(
network_loop: fn(M, UdpSocket) -> Result,
@ -34,16 +34,24 @@ fn start_network_thread<M: Send + 'static>(
fn network_send_loop(messages: Receiver<NetworkMessage>, socket: UdpSocket) -> Result {
loop {
let (message, address) = messages.recv()?;
socket.send_to(&message.to_le_bytes(), address)?;
debug!("Sending {} bytes to {}", message.len(), address);
let sent = socket.send_to(message.as_slice(), address)?;
if message.len() != sent {
error!(
"Network thread: Tried to send {} bytes but only sent {}",
message.len(),
sent
);
}
}
}
fn network_receive_loop(messages: Sender<NetworkMessage>, socket: UdpSocket) -> Result {
loop {
let mut message = [0u8; 8];
let mut message = [0u8; 1024]; // 1 KiB seems like it would be enough, TBD though
let (len, address) = socket.recv_from(&mut message)?;
info!("Network thread: Received {len} bytes");
messages.try_send((u64::from_le_bytes(message), address))?;
debug!("Network thread: Received {len} bytes from {address}");
messages.try_send((message[..len].into(), address))?;
}
}
@ -73,9 +81,9 @@ fn handle_network_io(
return Ok(());
};
if let Some(value) = seed {
send.0.try_send((value.clone().into(), address))?;
send.0.try_send(((*value).into(), address))?;
} else {
commands.insert_resource::<Seed>(message.into());
commands.insert_resource::<Seed>(message.try_into()?);
}
Ok(())
}
@ -108,7 +116,7 @@ impl Plugin for NetIOPlugin {
match setup_socket(self.listen) {
Ok((send, receive)) => {
if let Some(socket) = self.peer {
if let Err(err) = send.try_send((0, socket)) {
if let Err(err) = send.try_send((Vec::new(), socket)) {
warn!("Failed to send to peer: {err}");
return;
}