fixed bug in udp loop that could crash loop
parent
9f422d46b6
commit
63d3d7ed8e
97
src/main.rs
97
src/main.rs
|
|
@ -528,31 +528,40 @@ 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();
|
||||||
let client = clients.get(&client_id).unwrap();
|
if clients.contains_key(&client_id){
|
||||||
let mut port = client.port.write().unwrap();
|
let client = clients.get(&client_id).unwrap();
|
||||||
*port = addr.port(); //set the udp port to send data to
|
let mut port = client.port.write().unwrap();
|
||||||
s.send_to(&buf,addr).unwrap(); //echo back
|
*port = addr.port(); //set the udp port to send data to
|
||||||
|
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();
|
||||||
let client = clients.get(&client_id).unwrap();
|
if clients.contains_key(&client_id){
|
||||||
let room_option = client.room.read().unwrap();
|
let client = clients.get(&client_id).unwrap();
|
||||||
let room = room_option.as_ref().unwrap();
|
let room_option = client.room.read().unwrap();
|
||||||
let room_clients = room.clients.read().unwrap(); //we finally got to the room!
|
let room = room_option.as_ref().unwrap();
|
||||||
for (_k,v) in room_clients.iter() {
|
let room_clients = room.clients.read().unwrap(); //we finally got to the room!
|
||||||
if v.id != client_id{
|
for (_k,v) in room_clients.iter() {
|
||||||
let ip = v.ip.read().unwrap();
|
if v.id != client_id{
|
||||||
let port = v.port.read().unwrap();
|
let ip = v.ip.read().unwrap();
|
||||||
match s.send_to(&buf,SocketAddr::new(*ip, *port)) {
|
let port = v.port.read().unwrap();
|
||||||
Ok(_)=> (),
|
match s.send_to(&buf,SocketAddr::new(*ip, *port)) {
|
||||||
Err(_) => ()
|
Ok(_)=> (),
|
||||||
|
Err(_) => ()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -561,20 +570,22 @@ 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();
|
||||||
let client = clients.get(&client_id).unwrap();
|
if clients.contains_key(&client_id){
|
||||||
let room_option = client.room.read().unwrap();
|
let client = clients.get(&client_id).unwrap();
|
||||||
let room = room_option.as_ref().unwrap();
|
let room_option = client.room.read().unwrap();
|
||||||
let room_clients = room.clients.read().unwrap(); //we finally got to the room!
|
let room = room_option.as_ref().unwrap();
|
||||||
buf[0] = 3u8; //messages are always 3s, even though this came in as 4
|
let room_clients = room.clients.read().unwrap(); //we finally got to the room!
|
||||||
for (_k,v) in room_clients.iter() {
|
buf[0] = 3u8; //messages are always 3s, even though this came in as 4
|
||||||
|
for (_k,v) in room_clients.iter() {
|
||||||
|
|
||||||
|
let ip = v.ip.read().unwrap();
|
||||||
|
let port = v.port.read().unwrap();
|
||||||
|
match s.send_to(&buf,SocketAddr::new(*ip, *port)) {
|
||||||
|
Ok(_)=> (),
|
||||||
|
Err(_) => ()
|
||||||
|
}
|
||||||
|
|
||||||
let ip = v.ip.read().unwrap();
|
|
||||||
let port = v.port.read().unwrap();
|
|
||||||
match s.send_to(&buf,SocketAddr::new(*ip, *port)) {
|
|
||||||
Ok(_)=> (),
|
|
||||||
Err(_) => ()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} 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]
|
||||||
|
|
@ -587,29 +598,31 @@ 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();
|
||||||
let client = clients.get(&client_id).unwrap();
|
if clients.contains_key(&client_id){
|
||||||
let groups = client.groups.read().unwrap();
|
let client = clients.get(&client_id).unwrap();
|
||||||
if groups.contains_key(&group_name) {
|
let groups = client.groups.read().unwrap();
|
||||||
|
if groups.contains_key(&group_name) {
|
||||||
|
|
||||||
|
|
||||||
let clients = groups.get(&group_name).unwrap();
|
let clients = groups.get(&group_name).unwrap();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//we need to form a new message without the group name
|
//we need to form a new message without the group name
|
||||||
let mut message_to_send = vec![];
|
let mut message_to_send = vec![];
|
||||||
message_to_send.push(3u8);
|
message_to_send.push(3u8);
|
||||||
message_to_send.extend([buf[1],buf[2],buf[3],buf[4]]);
|
message_to_send.extend([buf[1],buf[2],buf[3],buf[4]]);
|
||||||
message_to_send.extend(message_bytes);
|
message_to_send.extend(message_bytes);
|
||||||
|
|
||||||
for v in clients.iter() {
|
for v in clients.iter() {
|
||||||
|
|
||||||
let ip = v.ip.read().unwrap();
|
let ip = v.ip.read().unwrap();
|
||||||
let port = v.port.read().unwrap();
|
let port = v.port.read().unwrap();
|
||||||
|
|
||||||
match s.send_to(&message_to_send,SocketAddr::new(*ip, *port)) {
|
match s.send_to(&message_to_send,SocketAddr::new(*ip, *port)) {
|
||||||
Ok(_)=> (),
|
Ok(_)=> (),
|
||||||
Err(_) => ()
|
Err(_) => ()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue