added config timeout, and set default as 30
parent
de78812fbb
commit
fd02ce4ede
|
|
@ -1,3 +1,4 @@
|
||||||
{
|
{
|
||||||
"port":80
|
"port":80,
|
||||||
|
"tcp_timeout":30
|
||||||
}
|
}
|
||||||
13
src/main.rs
13
src/main.rs
|
|
@ -69,7 +69,8 @@ struct Room {
|
||||||
}
|
}
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
struct Config {
|
struct Config {
|
||||||
port: u16
|
port: u16,
|
||||||
|
tcp_timeout: u64
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -615,10 +616,10 @@ fn client_write_thread(mut stream: TcpStream, rx: Receiver<Vec<u8>> ) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fn handle_client(stream: TcpStream, client_id: u32, clients_mutex: Arc<RwLock<HashMap<u32,Arc<Client>>>>, rooms_mutex: Arc<RwLock<HashMap<String,Arc<Room>>>>){
|
fn handle_client(stream: TcpStream, client_id: u32, clients_mutex: Arc<RwLock<HashMap<u32,Arc<Client>>>>, rooms_mutex: Arc<RwLock<HashMap<String,Arc<Room>>>>,tcp_timeout: u64){
|
||||||
|
|
||||||
stream.set_nodelay(true).unwrap();
|
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);
|
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);
|
let (tx, rx) = mpsc::sync_channel(10000);
|
||||||
|
|
@ -675,7 +676,7 @@ fn handle_client(stream: TcpStream, client_id: u32, clients_mutex: Arc<RwLock<Ha
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn tcp_listen(client_mutex: Arc<RwLock<HashMap<u32, Arc<Client>>>>, room_mutex: Arc<RwLock<HashMap<String,Arc<Room>>>>,port:u16){
|
fn tcp_listen(client_mutex: Arc<RwLock<HashMap<u32, Arc<Client>>>>, room_mutex: Arc<RwLock<HashMap<String,Arc<Room>>>>,port:u16,tcp_timeout:u64){
|
||||||
println!("{}: Started TCP Listener",Local::now().format("%Y-%m-%d %H:%M:%S"));
|
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");
|
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<RwLock<HashMap<u32, Arc<Client>>>>, room_mutex:
|
||||||
for stream in listener.incoming() {
|
for stream in listener.incoming() {
|
||||||
let client_mutex = Arc::clone(&client_mutex);
|
let client_mutex = Arc::clone(&client_mutex);
|
||||||
let room_mutex = Arc::clone(&room_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;
|
next_client_id+=1;
|
||||||
}
|
}
|
||||||
println!("{}: Ended TCP Listener",Local::now().format("%Y-%m-%d %H:%M:%S"));
|
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_rooms = Arc::clone(&room_mutex);
|
||||||
let udp_handle = thread::spawn(move ||{udp_listen(udp_clients, udp_rooms, config.port);});
|
let udp_handle = thread::spawn(move ||{udp_listen(udp_clients, udp_rooms, config.port);});
|
||||||
//start the TCP thread
|
//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();
|
udp_handle.join().unwrap();
|
||||||
println!("{}: VelNet Ended", Local::now().format("%Y-%m-%d %H:%M:%S"));
|
println!("{}: VelNet Ended", Local::now().format("%Y-%m-%d %H:%M:%S"));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue