fixed bug in udp loop that could crash loop

main
Kyle Johnsen 2022-01-24 19:24:02 -05:00
parent 9f422d46b6
commit 63d3d7ed8e
1 changed files with 61 additions and 48 deletions

View File

@ -528,20 +528,28 @@ fn udp_listen(client_mutex: Arc<RwLock<HashMap<u32, Arc<Client>>>>, _room_mutex:
let client_id_bytes = [buf[1],buf[2],buf[3],buf[4]]; let client_id_bytes = [buf[1],buf[2],buf[3],buf[4]];
let client_id = u32::from_be_bytes(client_id_bytes); let client_id = u32::from_be_bytes(client_id_bytes);
if t == 0 { //1 byte, 0. Nothing else. This is just to establish the udp port, Echos back the same thing sent if t == 0 { //1 byte, 0. Nothing else. This is just to establish the udp port, Echos back the same thing sent
//connect message, respond back //connect message, respond back
{ {
let clients = client_mutex.read().unwrap(); let clients = client_mutex.read().unwrap();
if clients.contains_key(&client_id){
let client = clients.get(&client_id).unwrap(); let client = clients.get(&client_id).unwrap();
let mut port = client.port.write().unwrap(); let mut port = client.port.write().unwrap();
*port = addr.port(); //set the udp port to send data to *port = addr.port(); //set the udp port to send data to
s.send_to(&buf,addr).unwrap(); //echo back match s.send_to(&buf,addr) {
Ok(_)=>(),
Err(_)=>()
}
}
} }
} else if t == 3 { //[3:u8][from:i32][contents:u8array] note that it must fit into the packet of 1024 bytes } else if t == 3 { //[3:u8][from:i32][contents:u8array] note that it must fit into the packet of 1024 bytes
{ {
let clients = client_mutex.read().unwrap(); let clients = client_mutex.read().unwrap();
if clients.contains_key(&client_id){
let client = clients.get(&client_id).unwrap(); let client = clients.get(&client_id).unwrap();
let room_option = client.room.read().unwrap(); let room_option = client.room.read().unwrap();
let room = room_option.as_ref().unwrap(); let room = room_option.as_ref().unwrap();
@ -557,10 +565,12 @@ fn udp_listen(client_mutex: Arc<RwLock<HashMap<u32, Arc<Client>>>>, _room_mutex:
} }
} }
} }
}
} else if t == 4 { //see above } else if t == 4 { //see above
{ {
let clients = client_mutex.read().unwrap(); let clients = client_mutex.read().unwrap();
if clients.contains_key(&client_id){
let client = clients.get(&client_id).unwrap(); let client = clients.get(&client_id).unwrap();
let room_option = client.room.read().unwrap(); let room_option = client.room.read().unwrap();
let room = room_option.as_ref().unwrap(); let room = room_option.as_ref().unwrap();
@ -577,6 +587,7 @@ fn udp_listen(client_mutex: Arc<RwLock<HashMap<u32, Arc<Client>>>>, _room_mutex:
} }
} }
}
} else if t == 5 { //[5:byte][from:i32][group.length():u8][message:u8array] } else if t == 5 { //[5:byte][from:i32][group.length():u8][message:u8array]
//this one is a little different, because we don't send the group in the message, so we have to formulate another message (like a 3 message) //this one is a little different, because we don't send the group in the message, so we have to formulate another message (like a 3 message)
//send a message to a group //send a message to a group
@ -587,6 +598,7 @@ fn udp_listen(client_mutex: Arc<RwLock<HashMap<u32, Arc<Client>>>>, _room_mutex:
let (group_name_bytes, message_bytes) = message_vec.split_at(group_name_size as usize); let (group_name_bytes, message_bytes) = message_vec.split_at(group_name_size as usize);
let group_name = String::from_utf8(group_name_bytes.to_vec()).unwrap(); let group_name = String::from_utf8(group_name_bytes.to_vec()).unwrap();
let clients = client_mutex.read().unwrap(); let clients = client_mutex.read().unwrap();
if clients.contains_key(&client_id){
let client = clients.get(&client_id).unwrap(); let client = clients.get(&client_id).unwrap();
let groups = client.groups.read().unwrap(); let groups = client.groups.read().unwrap();
if groups.contains_key(&group_name) { if groups.contains_key(&group_name) {
@ -614,6 +626,7 @@ fn udp_listen(client_mutex: Arc<RwLock<HashMap<u32, Arc<Client>>>>, _room_mutex:
} }
} }
} }
}
} }