chore: use ?
This commit is contained in:
29
src/main.rs
29
src/main.rs
@@ -43,12 +43,13 @@ lazy_static! {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_text_message(handle_context: &mut HandleContext, tx: &Tx, addr: SocketAddr, msg: String) {
|
fn handle_text_message(handle_context: &mut HandleContext, tx: &Tx, addr: SocketAddr, msg: String) -> XResult<()> {
|
||||||
// process all registered handles
|
// process all registered handles
|
||||||
if msg.starts_with('/') {
|
if msg.starts_with('/') {
|
||||||
for handle in &*TEXT_MESSAGE_HANDLES {
|
for handle in &*TEXT_MESSAGE_HANDLES {
|
||||||
if handle.is_matches(handle_context, tx, addr, &msg) {
|
if handle.is_matches(handle_context, tx, addr, &msg) {
|
||||||
return handle.handle(handle_context, tx, addr, &msg);
|
handle.handle(handle_context, tx, addr, &msg);
|
||||||
|
return Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -58,7 +59,7 @@ fn handle_text_message(handle_context: &mut HandleContext, tx: &Tx, addr: Socket
|
|||||||
Err(e) => {
|
Err(e) => {
|
||||||
warning!("Parse message: from: {:?} - {:?}, failed: {}", handle_context.room_id, handle_context.client_id, e);
|
warning!("Parse message: from: {:?} - {:?}, failed: {}", handle_context.room_id, handle_context.client_id, e);
|
||||||
RoomMessageDown::create_error_reply(format!("Message parse failed: {}, message: {}", e, msg)).send(&tx);
|
RoomMessageDown::create_error_reply(format!("Message parse failed: {}, message: {}", e, msg)).send(&tx);
|
||||||
return;
|
return Ok(());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
match room_message.r#type {
|
match room_message.r#type {
|
||||||
@@ -66,7 +67,7 @@ fn handle_text_message(handle_context: &mut HandleContext, tx: &Tx, addr: Socket
|
|||||||
if let (Some(room_id), Some(client_id)) = (&handle_context.room_id, &handle_context.client_id) {
|
if let (Some(room_id), Some(client_id)) = (&handle_context.room_id, &handle_context.client_id) {
|
||||||
warning!("Client is already in room: {:?} - {:?}", room_id, client_id);
|
warning!("Client is already in room: {:?} - {:?}", room_id, client_id);
|
||||||
RoomMessageDown::create_error_reply("Client is already in room").send(tx);
|
RoomMessageDown::create_error_reply("Client is already in room").send(tx);
|
||||||
return;
|
return Ok(());
|
||||||
}
|
}
|
||||||
if let (Some(msg_room_id), Some(msg_client_id)) = (room_message.room_id, room_message.client_id) {
|
if let (Some(msg_room_id), Some(msg_client_id)) = (room_message.room_id, room_message.client_id) {
|
||||||
let mut room_map = handle_context.room_map.lock().unwrap();
|
let mut room_map = handle_context.room_map.lock().unwrap();
|
||||||
@@ -95,7 +96,7 @@ fn handle_text_message(handle_context: &mut HandleContext, tx: &Tx, addr: Socket
|
|||||||
client_map.insert(msg_client_id.clone(), addr);
|
client_map.insert(msg_client_id.clone(), addr);
|
||||||
|
|
||||||
let m = RoomMessageDown::create_peer_enter(msg_client_id.clone());
|
let m = RoomMessageDown::create_peer_enter(msg_client_id.clone());
|
||||||
if let Ok(mm) = serde_json::to_string(&m) {
|
let mm = serde_json::to_string(&m)?;
|
||||||
for (client_id, client_addr) in client_map {
|
for (client_id, client_addr) in client_map {
|
||||||
if client_id != &msg_client_id {
|
if client_id != &msg_client_id {
|
||||||
if let Some(client_tx) = handle_context.peer_map.lock().unwrap().get(client_addr) {
|
if let Some(client_tx) = handle_context.peer_map.lock().unwrap().get(client_addr) {
|
||||||
@@ -103,7 +104,7 @@ fn handle_text_message(handle_context: &mut HandleContext, tx: &Tx, addr: Socket
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
RoomMessageDown::create_success_reply(format!("Client entered: {:?}", addr)).send(tx);
|
RoomMessageDown::create_success_reply(format!("Client entered: {:?}", addr)).send(tx);
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -163,8 +164,7 @@ fn handle_text_message(handle_context: &mut HandleContext, tx: &Tx, addr: Socket
|
|||||||
client_ids.push(client_id.clone());
|
client_ids.push(client_id.clone());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let client_ids_data = serde_json::to_string(&client_ids);
|
let client_ids_data = serde_json::to_string(&client_ids)?;
|
||||||
if let Ok(client_ids_data) = client_ids_data {
|
|
||||||
let m = RoomMessageDown {
|
let m = RoomMessageDown {
|
||||||
r#type: RoomMessageDownType::PeerList,
|
r#type: RoomMessageDownType::PeerList,
|
||||||
reply_code: Some(200),
|
reply_code: Some(200),
|
||||||
@@ -173,7 +173,6 @@ fn handle_text_message(handle_context: &mut HandleContext, tx: &Tx, addr: Socket
|
|||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
m.send(&tx);
|
m.send(&tx);
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
client_not_in_room(&tx, addr);
|
client_not_in_room(&tx, addr);
|
||||||
}
|
}
|
||||||
@@ -192,7 +191,7 @@ fn handle_text_message(handle_context: &mut HandleContext, tx: &Tx, addr: Socket
|
|||||||
peer_id: Some(client_id.clone()),
|
peer_id: Some(client_id.clone()),
|
||||||
data,
|
data,
|
||||||
};
|
};
|
||||||
if let Ok(mm) = serde_json::to_string(&m) {
|
let mm = serde_json::to_string(&m)?;
|
||||||
if let Some(client_map) = client_map {
|
if let Some(client_map) = client_map {
|
||||||
let mut sent_messages = 0;
|
let mut sent_messages = 0;
|
||||||
for (peer_client_id, peer_client_addr) in client_map {
|
for (peer_client_id, peer_client_addr) in client_map {
|
||||||
@@ -205,7 +204,6 @@ fn handle_text_message(handle_context: &mut HandleContext, tx: &Tx, addr: Socket
|
|||||||
}
|
}
|
||||||
RoomMessageDown::create_success_reply(format!("Send message to {} peers", sent_messages)).send(&tx);
|
RoomMessageDown::create_success_reply(format!("Send message to {} peers", sent_messages)).send(&tx);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
client_not_in_room(&tx, addr);
|
client_not_in_room(&tx, addr);
|
||||||
}
|
}
|
||||||
@@ -225,7 +223,7 @@ fn handle_text_message(handle_context: &mut HandleContext, tx: &Tx, addr: Socket
|
|||||||
peer_id: Some(client_id.clone()),
|
peer_id: Some(client_id.clone()),
|
||||||
data,
|
data,
|
||||||
};
|
};
|
||||||
if let Ok(mm) = serde_json::to_string(&m) {
|
let mm = serde_json::to_string(&m)?;
|
||||||
if let Some(client_map) = client_map {
|
if let Some(client_map) = client_map {
|
||||||
if let Some(peer_client_addr) = client_map.get(peer_id) {
|
if let Some(peer_client_addr) = client_map.get(peer_id) {
|
||||||
if let Some(peer_tx) = handle_context.peer_map.lock().unwrap().get(peer_client_addr) {
|
if let Some(peer_tx) = handle_context.peer_map.lock().unwrap().get(peer_client_addr) {
|
||||||
@@ -237,12 +235,12 @@ fn handle_text_message(handle_context: &mut HandleContext, tx: &Tx, addr: Socket
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
client_not_in_room(&tx, addr);
|
client_not_in_room(&tx, addr);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handle_connection(handle_context: HandleContext, raw_stream: TcpStream, addr: SocketAddr) {
|
async fn handle_connection(handle_context: HandleContext, raw_stream: TcpStream, addr: SocketAddr) {
|
||||||
@@ -280,7 +278,10 @@ async fn inner_handle_connection(
|
|||||||
},
|
},
|
||||||
Message::Text(msg) => {
|
Message::Text(msg) => {
|
||||||
if !msg.is_empty() {
|
if !msg.is_empty() {
|
||||||
handle_text_message(&mut handle_context, &tx, addr, msg);
|
if let Err(e) = handle_text_message(&mut handle_context, &tx, addr, msg) {
|
||||||
|
failure!("Error in process text message: {}", e);
|
||||||
|
RoomMessageDown::create_error_reply(format!("Error in process text message: {}", e));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user