diff --git a/.gitignore b/.gitignore index 9026c77..0012182 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /target .vscode +main diff --git a/src/main.rs b/src/main.rs index 2951141..0cf6b48 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,6 +16,7 @@ struct Client { } struct Room { + name: String, clients: Mutex>> } @@ -23,6 +24,11 @@ fn udp_listen(){ println!("UDP Thread Started"); } +fn read_u8(string: &mut TcpStream) -> u8 { + let mut buf = [0; 1]; + string.read_exact(&mut buf).unwrap(); + return buf[0]; +} fn read_u32(stream: &mut TcpStream) -> u32 { let mut buf:[u8;4] = [0; 4]; stream.read_exact(&mut buf).unwrap(); @@ -36,11 +42,18 @@ fn read_string(stream: &mut TcpStream) -> String { stream.read_exact(&mut stringBytes).unwrap(); return String::from_utf8(stringBytes).unwrap(); } +fn read_short_string(stream: &mut TcpStream) -> String { + let size = read_u8(stream); + println!("Size in bytes: {}",size); + let mut stringBytes = vec![0;size as usize]; + stream.read_exact(&mut stringBytes).unwrap(); + return String::from_utf8(stringBytes).unwrap(); +} fn read_login_message(stream: &mut TcpStream, client: Arc) { println!("Got login message"); - let username = read_string(stream); - let password = read_string(stream); + let username = read_short_string(stream); + let password = read_short_string(stream); println!("Got username {} and password {}",username,password); @@ -56,7 +69,7 @@ fn read_rooms_message(stream: &mut TcpStream, client: Arc){ } fn read_join_message(stream: &mut TcpStream, client: Arc){ - let room_name = read_string(stream); + let room_name = read_short_string(stream); println!("Got room message {}",room_name); @@ -92,6 +105,8 @@ fn read_join_message(stream: &mut TcpStream, client: Arc){ let mut writeBuf = vec![]; writeBuf.push(2u8); writeBuf.extend_from_slice(&(client.id).to_be_bytes()); //send everyone that the client id joined the room + writeBuf.push(room_name.as_bytes().len() as u8); + writeBuf.extend_from_slice(room_name.as_bytes()); v.sender.send(writeBuf); } @@ -101,6 +116,8 @@ fn read_join_message(stream: &mut TcpStream, client: Arc){ let mut writeBuf = vec![]; writeBuf.push(2u8); writeBuf.extend_from_slice(&(v.id).to_be_bytes()); //send everyone that the client id joined the room + writeBuf.push(room_name.as_bytes().len() as u8); + writeBuf.extend_from_slice(room_name.as_bytes()); client.sender.send(writeBuf); } } @@ -180,6 +197,28 @@ fn handle_client(mut stream: TcpStream, client_id: u32, clients_mutex: Arc