okay, so RWlocks are not so simple

main
Kyle Johnsen 2022-01-19 16:06:53 -05:00
parent 2f1c216d62
commit 1caf16ef44
1 changed files with 36 additions and 7 deletions

View File

@ -114,6 +114,7 @@ fn client_leave_room(client: &Arc<Client>, send_to_client: bool){
if room.is_some(){
{
println!("Client in room, leaving");
let room = room.as_ref().unwrap();
//may have to choose a new master
@ -167,7 +168,10 @@ fn client_leave_room(client: &Arc<Client>, send_to_client: bool){
}
}
} else{
println!("Client not in room");
}
*room = Option::None;
@ -175,17 +179,32 @@ fn client_leave_room(client: &Arc<Client>, send_to_client: bool){
fn read_join_message(stream: &mut TcpStream, client: &Arc<Client>){
//byte,shortstring
let room_name = read_short_string(stream);
let mut room_name = read_short_string(stream);
room_name = room_name.trim().to_string();
println!("Got room message {}",room_name);
//if the client is in a room, leave it
let mut room = client.room.write().unwrap();
//if the client is in a room, leave it
let mut leave_room = false;
{
let mut room = client.room.read().unwrap(); //must release this mutex before calling into a function that uses it
if room.as_ref().is_some(){
println!("Leaving the current room ");
leave_room = true;
}
}
if leave_room {
client_leave_room(client, true);
}
if room_name.trim() == "" || room_name == "-1" {
println!("Empty room, leaving");
return;
}
//join room_name
{
let mut rooms = client.rooms_mutex.write().unwrap();
@ -205,7 +224,11 @@ fn read_join_message(stream: &mut TcpStream, client: &Arc<Client>){
let mut clients = rooms[&room_name].clients.write().unwrap();
clients.insert(client.id,client.clone());
println!("Client {} joined {}",client.id,&room_name);
{
let mut room = client.room.write().unwrap();
*room = Some(rooms.get(&room_name).unwrap().clone()); //we create an option and assign it back to the room
}
//send a join message to everyone in the room
for (_k,v) in clients.iter() {
send_client_join_message(v, client.id, &room_name);
@ -217,6 +240,8 @@ fn read_join_message(stream: &mut TcpStream, client: &Arc<Client>){
send_client_join_message(&client, v.id, &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);
@ -242,7 +267,11 @@ fn send_room_message(sender: &Arc<Client>, message: &Vec<u8>, include_sender: bo
if !include_sender && v.id == sender.id {
continue;
}
v.sender.send(write_buf.clone()).unwrap();
match v.sender.send(write_buf.clone()){
Ok(_) => (),
Err(x) => println!("Error sending to client {}: {}",v.id,x)
} //this sometimes fails.
}
}
}