Skip to content

Commit

Permalink
Fix error handling bug
Browse files Browse the repository at this point in the history
  • Loading branch information
goodhoko committed Apr 21, 2023
1 parent e9b00bc commit 5a37564
Showing 1 changed file with 22 additions and 17 deletions.
39 changes: 22 additions & 17 deletions crates/composer/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use composer::api::Event;
use composer::api::DEFAULT_SERVER_ADDRESS;
use eyre::Result;
use rodio::{source::Source, Decoder, OutputStream};
use rodio::{source::Source, Decoder, OutputStream, OutputStreamHandle};
use std::fs::File;
use std::io::BufReader;
use std::net::UdpSocket;
Expand All @@ -15,23 +15,28 @@ fn main() -> Result<()> {
let (_stream, stream_handle) = OutputStream::try_default()?;

loop {
if let Err(err) = {
// Size up to max normal network packet size should be enough
let mut buf = [0; 1500];
let (number_of_bytes, _) = socket.recv_from(&mut buf)?;

let event: Event = bincode::deserialize(&buf)?;
println!("Received an event ({number_of_bytes} bytes): {:?}", event);

// FIXME: do the decoding and file reading outside the loop
let file = BufReader::new(File::open("src/sound_samples/click.wav")?);
let source = Decoder::new(file)?;
stream_handle.play_raw(source.convert_samples())?;

Ok::<(), eyre::Error>(())
} {
eprintln!("Could not process data-gram: {:?}. Ignoring and continuing.", err);
if let Err(err) = handle_datagram(&socket, &stream_handle) {
eprintln!(
"Could not process datagram. Ignoring and continuing. {:?}",
err
);
continue;
}
}
}

fn handle_datagram(socket: &UdpSocket, output_stream: &OutputStreamHandle) -> Result<()> {
// Size up to max normal network packet size
let mut buf = [0; 1500];
let (number_of_bytes, _) = socket.recv_from(&mut buf)?;

let event: Event = bincode::deserialize(&buf)?;
println!("Received an event ({number_of_bytes} bytes): {:?}", event);

// FIXME: do the decoding and file reading outside the loop
let file = BufReader::new(File::open("src/sound_samples/click.wav")?);
let source = Decoder::new(file)?;
output_stream.play_raw(source.convert_samples())?;

Ok(())
}

0 comments on commit 5a37564

Please sign in to comment.