Allow multiple connections

This commit is contained in:
Silvio Braendle 2025-01-06 15:19:57 +01:00
parent 1986341fdc
commit 2b8a8a7c84
No known key found for this signature in database
GPG key ID: 553A5A37BA69D8AE
2 changed files with 27 additions and 20 deletions

View file

@ -2,6 +2,7 @@ use crate::protocol::Message;
use crate::sound_scheduler::SoundScheduler;
use std::io::Read;
use std::net::{TcpListener, TcpStream};
use std::thread;
pub struct ClientHandler;
@ -24,14 +25,14 @@ impl ClientHandler {
let listener = TcpListener::bind("127.0.0.1:3000").expect("Could not bind to address");
for stream in listener.incoming() {
match stream {
thread::spawn(move || match stream {
Ok(stream) => {
ClientHandler::handle_client(stream);
}
Err(e) => {
eprintln!("Error: {}", e);
}
}
});
}
}
}

View file

@ -1,17 +1,22 @@
use crate::protocol::PlaySound;
use crate::sound_player::SoundPlayer;
use std::thread::sleep;
use std::thread::{self, sleep};
use std::time::{Duration, SystemTime, UNIX_EPOCH};
pub struct SoundScheduler;
impl SoundScheduler {
pub fn handle_scheduled_sound(play_msg: PlaySound) {
thread::spawn(move || {
let PlaySound {
sound_data,
timestamp,
} = play_msg;
println!("Scheduled sound: len={:?} at {:?}", sound_data.len(), timestamp);
println!(
"Scheduled sound: len={:?} at {:?}",
sound_data.len(),
timestamp
);
let now_micros = SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("Could not get time")
@ -24,5 +29,6 @@ impl SoundScheduler {
sleep(Duration::from_micros(wait as u64));
println!("Playing sound...");
SoundPlayer::play_sound(sound_data);
});
}
}