From b37edb0222d726835253171eed40ae192d1bdcf7 Mon Sep 17 00:00:00 2001 From: Kyle Johnsen Date: Tue, 22 Feb 2022 08:24:34 -0500 Subject: [PATCH] no more magic numbers --- config.txt | 3 ++- src/main.rs | 13 +++++++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/config.txt b/config.txt index 1abccfb..0fe8d74 100644 --- a/config.txt +++ b/config.txt @@ -1,4 +1,5 @@ { "port":80, -"tcp_timeout":30 +"tcp_timeout":30, +"tcp_send_buffer":100000 } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 6335316..b1f5a5e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -69,7 +69,8 @@ struct Room { #[derive(Serialize, Deserialize)] struct Config { port: u16, - tcp_timeout: u64 + tcp_timeout: u64, + tcp_send_buffer: usize } @@ -615,14 +616,14 @@ fn client_write_thread(mut stream: TcpStream, rx: Receiver> ) { } -fn handle_client(stream: TcpStream, client_id: u32, clients_mutex: Arc>>>, rooms_mutex: Arc>>>,tcp_timeout: u64){ +fn handle_client(stream: TcpStream, client_id: u32, clients_mutex: Arc>>>, rooms_mutex: Arc>>>,tcp_timeout: u64,tcp_send_buffer:usize){ stream.set_nodelay(true).unwrap(); stream.set_read_timeout(Some(time::Duration::new(tcp_timeout,0))).unwrap(); stream.set_write_timeout(Some(time::Duration::new(tcp_timeout,0))).unwrap(); println!("{}: Accepted new connection, assigned client id {}",Local::now().format("%Y-%m-%d %H:%M:%S"),client_id); - let (tx, rx) = mpsc::sync_channel(10000); //todo: address this magic number + let (tx, rx) = mpsc::sync_channel(tcp_send_buffer); //the server is very fast. However, if the clients throttle the sending, this buffer may overflow, and when it does, data is lost //create a new client structure and add it to the list of clients let client = Arc::new(Client{ id: client_id, @@ -676,7 +677,7 @@ fn handle_client(stream: TcpStream, client_id: u32, clients_mutex: Arc>>>, room_mutex: Arc>>>,port:u16,tcp_timeout:u64){ +fn tcp_listen(client_mutex: Arc>>>, room_mutex: Arc>>>,port:u16,tcp_timeout:u64,tcp_send_buffer:usize){ println!("{}: Started TCP Listener",Local::now().format("%Y-%m-%d %H:%M:%S")); let listener = TcpListener::bind(format!("0.0.0.0:{}",port)).expect("could not bind port"); @@ -685,7 +686,7 @@ fn tcp_listen(client_mutex: Arc>>>, room_mutex: for stream in listener.incoming() { let client_mutex = Arc::clone(&client_mutex); let room_mutex = Arc::clone(&room_mutex); - thread::spawn(move || {handle_client(stream.unwrap(), next_client_id, client_mutex, room_mutex,tcp_timeout)}); + thread::spawn(move || {handle_client(stream.unwrap(), next_client_id, client_mutex, room_mutex,tcp_timeout,tcp_send_buffer)}); next_client_id+=1; } println!("{}: Ended TCP Listener",Local::now().format("%Y-%m-%d %H:%M:%S")); @@ -839,7 +840,7 @@ fn main() { let udp_rooms = Arc::clone(&room_mutex); let udp_handle = thread::spawn(move ||{udp_listen(udp_clients, udp_rooms, config.port);}); //start the TCP thread - tcp_listen(client_mutex, room_mutex,config.port,config.tcp_timeout); + tcp_listen(client_mutex, room_mutex,config.port,config.tcp_timeout,config.tcp_send_buffer); udp_handle.join().unwrap(); println!("{}: VelNet Ended", Local::now().format("%Y-%m-%d %H:%M:%S")); }