From 81ac6c6f183c9fb487de57feb1dfd5efef3957ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=98=A4=EB=B3=91=EC=A4=80?= Date: Fri, 3 May 2024 02:17:07 +0900 Subject: [PATCH] improve: mpsc.send error handling --- src/client.rs | 8 ++++---- src/server.rs | 25 +++++++++++++------------ 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/client.rs b/src/client.rs index a8b4e09..b48d306 100644 --- a/src/client.rs +++ b/src/client.rs @@ -281,7 +281,7 @@ fn prompt_display_position(displays: &mut Vec, server_conf: Vec file, Err(e) => { - eprintln!("[ERR] failed to create {:?}: {:?}", path_str, e); + eprintln!("[ERR] failed to create {}: {}", path_str, e); break; } }; @@ -289,12 +289,12 @@ fn prompt_display_position(displays: &mut Vec, server_conf: Vec { if let Err(e) = file.write_all(json.as_bytes()) { - eprintln!("[ERR] failed to write to {:?}: {:?}", path_str, e); + eprintln!("[ERR] failed to write to {}: {}", path_str, e); } - println!("[INF] config saved at {:?}", path_str); + println!("[INF] config saved at {}", path_str); } - Err(e) => eprintln!("[ERR] failed to save config into file: {:?}", e), + Err(e) => eprintln!("[ERR] failed to save config into file: {}", e), } break; } diff --git a/src/server.rs b/src/server.rs index 5dc9a8f..b363086 100644 --- a/src/server.rs +++ b/src/server.rs @@ -189,20 +189,21 @@ impl Server { let (x, y) = warp_point.unwrap(); // transmit warp point - tx.send(Message { + if let Err(e) = tx.send(Message { disp: *cur_id, action: Action::Warp, x, y, - }) - .unwrap(); + }) { + eprintln!("[ERR] mpsc tx failed: {}", e); + } // warp // TODO: winit })); if let Err(e) = hook { - eprintln!("[ERR] event hook failed: {:?}", e); + eprintln!("[ERR] event hook failed: {}", e); } } } @@ -236,7 +237,7 @@ fn handle_client( let mut buffer = vec![0u8; mem::size_of::()]; if let Err(e) = tcp_read(&mut stream, &mut buffer) { - eprintln!("[ERR] client {:?} handshake failed: {:?}", ip, e); + eprintln!("[ERR] client {} handshake failed: {}", ip, e); continue; }; @@ -245,14 +246,14 @@ fn handle_client( // reject unknown client if !authorized.contains(&cid) { if let Err(e) = tcp_write(&mut stream, 0) { - eprintln!("[ERR] client {:?} handshake failed: {:?}", ip, e); + eprintln!("[ERR] client {} handshake failed: {}", ip, e); continue; }; } // transmit display counts to client if let Err(e) = tcp_write(&mut stream, displays.read().unwrap().len() as u32) { - eprintln!("[ERR] client {:?} handshake failed: {:?}", ip, e); + eprintln!("[ERR] client {} handshake failed: {}", ip, e); continue; }; @@ -261,14 +262,14 @@ fn handle_client( let disp = displays.read().unwrap(); if let Err(e) = tcp_write(&mut stream, disp.clone()) { - eprintln!("[ERR] client {:?} handshake failed: {:?}", ip, e); + eprintln!("[ERR] client {} handshake failed: {}", ip, e); continue; }; } // receive display attach request if let Err(e) = tcp_read(&mut stream, &mut buffer) { - eprintln!("[ERR] client {:?} handshake failed: {:?}", ip, e); + eprintln!("[ERR] client {} handshake failed: {}", ip, e); continue; }; @@ -278,14 +279,14 @@ fn handle_client( let new = match create_warpzones_hashmap(&mut displays, &mut client_disp) { Ok(new) => new, Err(e) => { - eprintln!("[ERR] invalid request from client {:?} : {:?}", ip, e); + eprintln!("[ERR] invalid request from client {} : {}", ip, e); continue; } }; // transmit ack if let Err(e) = tcp_write(&mut stream, HandshakeStatus::HandshakeOk as i32) { - eprintln!("[ERR] client {:?} handshake failed: {:?}", ip, e); + eprintln!("[ERR] client {} handshake failed: {}", ip, e); continue; }; @@ -299,7 +300,7 @@ fn handle_client( clients.write().unwrap().insert(cid, client); disp_ids.write().unwrap().client.extend(new); - println!("[INF] client {:?} connected!", ip); + println!("[INF] client {} connected!", ip); } }