Convert to UDP
This commit is contained in:
parent
d0e86603ae
commit
a6a08ad768
2 changed files with 22 additions and 33 deletions
|
|
@ -1,42 +1,30 @@
|
||||||
use crate::protocol::Message;
|
use crate::protocol::Message;
|
||||||
use crate::sound_scheduler::SoundScheduler;
|
use crate::sound_scheduler::SoundScheduler;
|
||||||
use std::net::{TcpListener, TcpStream};
|
use std::net::UdpSocket;
|
||||||
use std::thread;
|
|
||||||
|
|
||||||
pub struct ClientHandler;
|
pub struct ClientHandler;
|
||||||
|
|
||||||
impl ClientHandler {
|
impl ClientHandler {
|
||||||
fn handle_client(mut stream: TcpStream) {
|
fn handle_message(msg: Message) {
|
||||||
println!("Handling client: {:?}", stream);
|
match msg {
|
||||||
loop {
|
Message::PlaySound(play_sound) => {
|
||||||
match bincode::deserialize_from::<&mut TcpStream, Message>(&mut stream) {
|
SoundScheduler::handle_scheduled_sound(play_sound);
|
||||||
Ok(message) => {
|
|
||||||
match message {
|
|
||||||
Message::PlaySound(play_sound) => {
|
|
||||||
SoundScheduler::handle_scheduled_sound(play_sound);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
eprintln!("Error deserializing message: {}", e);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn listen() {
|
pub fn listen() {
|
||||||
let listener = TcpListener::bind("0.0.0.0:3000").expect("Could not bind to address");
|
let socket = UdpSocket::bind("0.0.0.0:3000").expect("Could not bind to address");
|
||||||
|
|
||||||
for stream in listener.incoming() {
|
println!("Listening on {}", socket.local_addr().unwrap());
|
||||||
thread::spawn(move || match stream {
|
|
||||||
Ok(stream) => {
|
loop {
|
||||||
ClientHandler::handle_client(stream);
|
let mut data = [0; 1_048_576];
|
||||||
}
|
let (amt, src) = socket.recv_from(&mut data).expect("Didn't receive data");
|
||||||
Err(e) => {
|
println!("Received {} bytes from {}", amt, src);
|
||||||
eprintln!("Error: {}", e);
|
let data = &mut data[..amt];
|
||||||
}
|
let msg = bincode::deserialize::<Message>(data).expect("Failed to deserialize");
|
||||||
});
|
ClientHandler::handle_message(msg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
use std::io::BufReader;
|
use std::fs::File;
|
||||||
use std::net::TcpStream;
|
use std::io::{BufReader, Read};
|
||||||
|
use std::net::UdpSocket;
|
||||||
use std::thread;
|
use std::thread;
|
||||||
use std::time::{Duration, SystemTime};
|
use std::time::{Duration, SystemTime};
|
||||||
use std::{fs::File, io::Write};
|
|
||||||
|
|
||||||
use rodio::{Decoder, Source};
|
use rodio::{Decoder, Source};
|
||||||
|
|
||||||
|
|
@ -26,7 +26,7 @@ impl Dispatcher {
|
||||||
let channels = source.channels();
|
let channels = source.channels();
|
||||||
let sample_rate = source.sample_rate();
|
let sample_rate = source.sample_rate();
|
||||||
let duration = source.total_duration().unwrap();
|
let duration = source.total_duration().unwrap();
|
||||||
let chunk_len = Duration::from_secs(1);
|
let chunk_len = Duration::from_millis(200);
|
||||||
|
|
||||||
let mut offset = Duration::from_secs(0);
|
let mut offset = Duration::from_secs(0);
|
||||||
|
|
||||||
|
|
@ -38,7 +38,8 @@ impl Dispatcher {
|
||||||
let source = source.clone();
|
let source = source.clone();
|
||||||
|
|
||||||
thread::spawn(move || {
|
thread::spawn(move || {
|
||||||
let mut sock = TcpStream::connect(addr).expect("Failed to connect");
|
let sock = UdpSocket::bind("0.0.0.0:0").expect("Failed to bind");
|
||||||
|
sock.connect(addr.clone()).expect("Failed to connect");
|
||||||
|
|
||||||
while offset < duration {
|
while offset < duration {
|
||||||
let sound_data = source
|
let sound_data = source
|
||||||
|
|
@ -57,7 +58,7 @@ impl Dispatcher {
|
||||||
});
|
});
|
||||||
|
|
||||||
let buf = bincode::serialize(&msg).expect("Failed to serialize");
|
let buf = bincode::serialize(&msg).expect("Failed to serialize");
|
||||||
sock.write_all(&buf).unwrap();
|
sock.send(&buf).expect("Failed to send");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue