feat: optimize code

This commit is contained in:
2023-11-05 16:18:42 +08:00
parent e8fcd412c5
commit a996f13deb

View File

@@ -128,11 +128,6 @@ async fn inner_handle_connection(
Ok(())
}
fn client_not_in_room(tx: &Tx, addr: SocketAddr) {
information!("Client is not in room: {}", addr);
RoomMessageDown::create_error_reply(&None, "Client is not in room").send(tx);
}
fn client_exit(handle_context: &HandleContext, room_id: &Option<String>, client_id: &Option<String>) {
let room_map = &handle_context.room_map;
if let (Some(room_id), Some(client_id)) = (room_id, client_id) {
@@ -184,7 +179,13 @@ fn handle_text_message(handle_context: &mut HandleContext, tx: &Tx, addr: Socket
RoomMessageDown::create_error_reply(&room_message.message_id, "Client is already in room").send(tx);
return Ok(());
}
if let (Some(msg_room_id), Some(msg_client_id)) = (room_message.room_id, room_message.client_id) {
let (msg_room_id, msg_client_id) = match (room_message.room_id, room_message.client_id) {
(Some(msg_room_id), Some(msg_client_id)) => (msg_room_id, msg_client_id),
_ => {
RoomMessageDown::create_error_reply(&room_message.message_id, "Room id and client id must both assigned").send(tx);
return Ok(());
}
};
let mut room_map = handle_context.room_map.lock().unwrap();
match room_map.get_mut(&msg_room_id) {
Some(client_map) => {
@@ -195,10 +196,7 @@ fn handle_text_message(handle_context: &mut HandleContext, tx: &Tx, addr: Socket
RoomMessageDown::create_success_reply(
&room_message.message_id, "Duplicate enter to room").send(tx);
} else {
information!(
"Replace client: {:?} - {:?}, from {:?} -> {:?}",
msg_room_id, msg_client_id, peer_addr, addr
);
information!("Replace client: {:?} - {:?}, from {:?} -> {:?}", msg_room_id, msg_client_id, peer_addr, addr);
let client_replaced_message = format!("Client replaced {:?} -> {:?}", peer_addr, addr);
if let Some(tx) = handle_context.peer_map.lock().unwrap().remove(peer_addr) {
tx.send_close();
@@ -235,14 +233,15 @@ fn handle_text_message(handle_context: &mut HandleContext, tx: &Tx, addr: Socket
&room_message.message_id, format!("Client create room: {:?}", addr)).send(tx);
}
}
// Room created
handle_context.room_id = Some(msg_room_id);
handle_context.client_id = Some(msg_client_id);
} else {
RoomMessageDown::create_error_reply(&room_message.message_id, "Room id and client id must both assigned").send(tx);
}
}
RoomMessageType::Exit => {
if let (Some(room_id), Some(client_id)) = (&handle_context.room_id, &handle_context.client_id) {
let (room_id, client_id) = match (&handle_context.room_id, &handle_context.client_id) {
(Some(room_id), Some(client_id)) => (room_id, client_id),
_ => return client_not_in_room(tx, addr),
};
let mut room_map = handle_context.room_map.lock().unwrap();
if let Some(client_map) = room_map.get_mut(room_id) {
client_map.remove(client_id);
@@ -251,12 +250,12 @@ fn handle_text_message(handle_context: &mut HandleContext, tx: &Tx, addr: Socket
} else {
warning!("Not in room: {:?} - {:?}", room_id, client_id);
}
} else {
client_not_in_room(tx, addr);
}
}
RoomMessageType::Destroy => {
if let (Some(room_id), Some(client_id)) = (&handle_context.room_id, &handle_context.client_id) {
let (room_id, client_id) = match (&handle_context.room_id, &handle_context.client_id) {
(Some(room_id), Some(client_id)) => (room_id, client_id),
_ => return client_not_in_room(tx, addr),
};
information!("Destroy room: {:?} - {:?}", room_id, client_id);
let mut room_map = handle_context.room_map.lock().unwrap();
let client_map = room_map.remove(room_id);
@@ -267,12 +266,12 @@ fn handle_text_message(handle_context: &mut HandleContext, tx: &Tx, addr: Socket
}
}
}
} else {
client_not_in_room(tx, addr);
}
}
RoomMessageType::ListPeers => {
if let (Some(room_id), Some(client_id)) = (&handle_context.room_id, &handle_context.client_id) {
let (room_id, client_id) = match (&handle_context.room_id, &handle_context.client_id) {
(Some(room_id), Some(client_id)) => (room_id, client_id),
_ => return client_not_in_room(tx, addr),
};
information!("List room peers: {:?} - {:?}", room_id, client_id);
let room_map = handle_context.room_map.lock().unwrap();
let client_map = room_map.get(room_id);
@@ -281,12 +280,12 @@ fn handle_text_message(handle_context: &mut HandleContext, tx: &Tx, addr: Socket
.unwrap_or_else(Vec::new);
let client_ids_data = serde_json::to_string(&client_ids)?;
RoomMessageDown::create_peerlist_message(&room_message.message_id, Some(client_ids_data)).send(tx);
} else {
client_not_in_room(tx, addr);
}
}
RoomMessageType::Broadcast => {
if let (Some(room_id), Some(client_id)) = (&handle_context.room_id, &handle_context.client_id) {
let (room_id, client_id) = match (&handle_context.room_id, &handle_context.client_id) {
(Some(room_id), Some(client_id)) => (room_id, client_id),
_ => return client_not_in_room(tx, addr),
};
information!("Broadcast room message: {:?} - {:?}", room_id, client_id);
let room_map = handle_context.room_map.lock().unwrap();
let client_map = room_map.get(room_id);
@@ -307,12 +306,12 @@ fn handle_text_message(handle_context: &mut HandleContext, tx: &Tx, addr: Socket
RoomMessageDown::create_success_reply(
&room_message.message_id, format!("Send message to {} peers", sent_messages)).send(tx);
}
} else {
client_not_in_room(tx, addr);
}
}
RoomMessageType::Peer => {
if let (Some(room_id), Some(client_id)) = (&handle_context.room_id, &handle_context.client_id) {
let (room_id, client_id) = match (&handle_context.room_id, &handle_context.client_id) {
(Some(room_id), Some(client_id)) => (room_id, client_id),
_ => return client_not_in_room(tx, addr),
};
information!("Send peer message: {:?} - {:?} -> {:?}", room_id, client_id, room_message.peer_id);
if let Some(peer_id) = room_message.peer_id.as_ref() {
let room_map = handle_context.room_map.lock().unwrap();
@@ -333,10 +332,13 @@ fn handle_text_message(handle_context: &mut HandleContext, tx: &Tx, addr: Socket
}
}
}
} else {
client_not_in_room(tx, addr);
}
}
}
Ok(())
}
fn client_not_in_room(tx: &Tx, addr: SocketAddr) -> XResult<()> {
information!("Client is not in room: {}", addr);
RoomMessageDown::create_error_reply(&None, "Client is not in room").send(tx);
Ok(())
}