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

View file

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