diff --git a/config.txt b/config.txt index eeb9f87..1abccfb 100644 --- a/config.txt +++ b/config.txt @@ -1,3 +1,4 @@ { -"port":80 +"port":80, +"tcp_timeout":30 } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 4db0271..fd90870 100644 --- a/src/main.rs +++ b/src/main.rs @@ -69,7 +69,8 @@ struct Room { } #[derive(Serialize, Deserialize)] struct Config { - port: u16 + port: u16, + tcp_timeout: u64 } @@ -615,10 +616,10 @@ fn client_write_thread(mut stream: TcpStream, rx: Receiver> ) { } -fn handle_client(stream: TcpStream, client_id: u32, clients_mutex: Arc>>>, rooms_mutex: Arc>>>){ +fn handle_client(stream: TcpStream, client_id: u32, clients_mutex: Arc>>>, rooms_mutex: Arc>>>,tcp_timeout: u64){ stream.set_nodelay(true).unwrap(); - stream.set_read_timeout(Some(time::Duration::new(10,0))).unwrap(); + stream.set_read_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); @@ -675,7 +676,7 @@ fn handle_client(stream: TcpStream, client_id: u32, clients_mutex: Arc>>>, room_mutex: Arc>>>,port:u16){ +fn tcp_listen(client_mutex: Arc>>>, room_mutex: Arc>>>,port:u16,tcp_timeout:u64){ 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"); @@ -684,7 +685,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)}); + thread::spawn(move || {handle_client(stream.unwrap(), next_client_id, client_mutex, room_mutex,tcp_timeout)}); next_client_id+=1; } println!("{}: Ended TCP Listener",Local::now().format("%Y-%m-%d %H:%M:%S")); @@ -838,7 +839,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); + tcp_listen(client_mutex, room_mutex,config.port,config.tcp_timeout); udp_handle.join().unwrap(); println!("{}: VelNet Ended", Local::now().format("%Y-%m-%d %H:%M:%S")); }