added timeout

main
Kyle Johnsen 2022-02-21 18:24:08 -05:00
parent c2a30171e3
commit de78812fbb
1 changed files with 3 additions and 1 deletions

View File

@ -11,6 +11,7 @@ use std::sync::mpsc::{SyncSender,Receiver};
use chrono::Local; use chrono::Local;
use std::fs; use std::fs;
use std::fmt; use std::fmt;
use std::time;
use serde::{Serialize, Deserialize}; use serde::{Serialize, Deserialize};
enum ToClientTCPMessageType { enum ToClientTCPMessageType {
LoggedIn = 0, LoggedIn = 0,
@ -568,7 +569,7 @@ fn client_read_thread(mut stream: TcpStream, mut client: Arc<Client>) {
let mut read_buf:[u8;1] = [0; 1]; let mut read_buf:[u8;1] = [0; 1];
//messages come through as a 1 byte type identifier, that can be one of 0 (login) 1 (get rooms), 2 (join/leave room) 3 (send message to room), 4 (send message to room including me), 5 (send message to group), 6 (establish group) //messages come through as a 1 byte type identifier, that can be one of 0 (login) 1 (get rooms), 2 (join/leave room) 3 (send message to room), 4 (send message to room including me), 5 (send message to group), 6 (establish group)
loop { loop {
//read exactly 1 byte //read exactly 1 byte
stream.read_exact(&mut read_buf).unwrap(); stream.read_exact(&mut read_buf).unwrap();
//println!("Got a message {}",read_buf[0]); //println!("Got a message {}",read_buf[0]);
@ -617,6 +618,7 @@ 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>>>>){
stream.set_nodelay(true).unwrap(); stream.set_nodelay(true).unwrap();
stream.set_read_timeout(Some(time::Duration::new(10,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);