addressed race condition
parent
a263c0d028
commit
031db08eb0
17
src/main.rs
17
src/main.rs
|
|
@ -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
|
||||||
{
|
{
|
||||||
let mut clients = rooms[&extended_room_name].clients.write().unwrap();
|
{ //actually add the client to the room (need a write lock)
|
||||||
clients.insert(client.id,client.clone());
|
let mut clients = rooms[&extended_room_name].clients.write().unwrap();
|
||||||
println!("Client {} joined {}",client.id,&extended_room_name);
|
clients.insert(client.id,client.clone());
|
||||||
|
}
|
||||||
{
|
{
|
||||||
let mut room = client.room.write().unwrap();
|
println!("Client {} joined {}",client.id,&extended_room_name);
|
||||||
*room = Some(rooms.get(&extended_room_name).unwrap().clone()); //we create an option and assign it back to the room
|
{
|
||||||
|
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 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() {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue