addressed race condition

main
Kyle Johnsen 2022-02-08 08:59:33 -05:00
parent a263c0d028
commit 031db08eb0
1 changed files with 12 additions and 7 deletions

View File

@ -264,7 +264,7 @@ fn send_client_left_message(to: &Arc<Client>, from: u32, room: &str){
fn client_leave_room(client: &Arc<Client>, send_to_client: bool){ fn client_leave_room(client: &Arc<Client>, send_to_client: bool){
//first remove the client from the room they are in //first remove the client from the room they are in
let mut room = client.room.write().unwrap(); let mut room = client.room.write().unwrap(); //I need to get the room, because I'll be modifying the clients in it
if room.is_some(){ if room.is_some(){
{ {
@ -378,14 +378,19 @@ fn read_join_message(stream: &mut TcpStream, client: &Arc<Client>){
//the room is guaranteed to exist now //the room is guaranteed to exist now
{ {
{ //actually add the client to the room (need a write lock)
let mut clients = rooms[&extended_room_name].clients.write().unwrap(); let mut clients = rooms[&extended_room_name].clients.write().unwrap();
clients.insert(client.id,client.clone()); clients.insert(client.id,client.clone());
}
{
println!("Client {} joined {}",client.id,&extended_room_name); println!("Client {} joined {}",client.id,&extended_room_name);
{ {
let mut room = client.room.write().unwrap(); let mut room = client.room.write().unwrap();
*room = Some(rooms.get(&extended_room_name).unwrap().clone()); //we create an option and assign it back to the room *room = Some(rooms.get(&extended_room_name).unwrap().clone()); //we create an option and assign it back to the room
} }
}
let clients = rooms[&extended_room_name].clients.read().unwrap(); //only need a read lock now
//send a join message to everyone in the room (except the client) //send a join message to everyone in the room (except the client)
for (_k,v) in clients.iter() { for (_k,v) in clients.iter() {