added further refinements to reduce locking unecessarily
parent
031db08eb0
commit
d5cfc471e2
23
src/main.rs
23
src/main.rs
|
|
@ -364,7 +364,8 @@ fn read_join_message(stream: &mut TcpStream, client: &Arc<Client>){
|
|||
|
||||
//join room_name
|
||||
{
|
||||
let mut rooms = client.rooms_mutex.write().unwrap();
|
||||
{
|
||||
let rooms = client.rooms_mutex.read().unwrap();
|
||||
if !rooms.contains_key(&extended_room_name) { //new room, must create it
|
||||
let map: HashMap<u32, Arc<Client>> = HashMap::new();
|
||||
let r = Arc::new(Room {
|
||||
|
|
@ -372,26 +373,29 @@ fn read_join_message(stream: &mut TcpStream, client: &Arc<Client>){
|
|||
clients: RwLock::new(map),
|
||||
master_client: Arc::new(RwLock::new(client.clone())) //client is the master, since they joined first
|
||||
});
|
||||
let mut rooms = client.rooms_mutex.write().unwrap();
|
||||
rooms.insert(String::from(&extended_room_name),r);
|
||||
println!("New room {} created",&extended_room_name);
|
||||
}
|
||||
|
||||
}
|
||||
//the room is guaranteed to exist now
|
||||
{
|
||||
|
||||
{ //actually add the client to the room (need a write lock)
|
||||
let rooms = client.rooms_mutex.read().unwrap();
|
||||
let mut clients = rooms[&extended_room_name].clients.write().unwrap();
|
||||
clients.insert(client.id,client.clone());
|
||||
}
|
||||
{
|
||||
println!("Client {} joined {}",client.id,&extended_room_name);
|
||||
{
|
||||
|
||||
let rooms = client.rooms_mutex.read().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
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
{
|
||||
let rooms = client.rooms_mutex.read().unwrap();
|
||||
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)
|
||||
for (_k,v) in clients.iter() {
|
||||
if v.id != client.id {
|
||||
|
|
@ -405,16 +409,15 @@ fn read_join_message(stream: &mut TcpStream, client: &Arc<Client>){
|
|||
ids_in_room.push(v.id);
|
||||
|
||||
}
|
||||
|
||||
|
||||
send_you_joined_message(client, ids_in_room, &short_room_name);
|
||||
}
|
||||
|
||||
|
||||
let room = client.room.read().unwrap();
|
||||
//tell the client who the master is
|
||||
let master_client = room.as_ref().unwrap().master_client.read().unwrap();
|
||||
send_client_master_message(client, master_client.id);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue