Skip to content

Commit

Permalink
improve: mpsc.send error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
luftaquila committed May 2, 2024
1 parent 84b01ad commit 81ac6c6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
8 changes: 4 additions & 4 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,20 +281,20 @@ fn prompt_display_position(displays: &mut Vec<Display>, server_conf: Vec<Display
let mut file = match fs::File::create(&path) {
Ok(file) => file,
Err(e) => {
eprintln!("[ERR] failed to create {:?}: {:?}", path_str, e);
eprintln!("[ERR] failed to create {}: {}", path_str, e);
break;
}
};

match serde_json::to_string_pretty(&displays) {
Ok(json) => {
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;
}
Expand Down
25 changes: 13 additions & 12 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -236,7 +237,7 @@ fn handle_client(
let mut buffer = vec![0u8; mem::size_of::<Cid>()];

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;
};

Expand All @@ -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;
};

Expand All @@ -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;
};

Expand All @@ -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;
};

Expand All @@ -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);
}
}

Expand Down

0 comments on commit 81ac6c6

Please sign in to comment.