moved setting up the control c to a function

This commit is contained in:
Nickiel12 2021-12-18 18:34:14 -08:00
parent e9c546f559
commit 9c1c035026

View file

@ -15,22 +15,29 @@ fn main() {
let (from_socket_tx, from_socket_rx) = mpsc::channel::<String>(); let (from_socket_tx, from_socket_rx) = mpsc::channel::<String>();
let (mut listener_can_run_flag, listener_join_handle) = Socket::handle_connections(socket_listener, from_socket_tx); let (mut listener_can_run_flag, listener_join_handle) = Socket::handle_connections(socket_listener, from_socket_tx);
let (mut control_c_flag_tx, control_c_called_flag_rx) = sync_flag::new_syncflag(false); let (control_c_flag_tx, control_c_called_flag_rx) = sync_flag::new_syncflag(false);
ctrlc::set_handler(move || { setup_control_c(control_c_flag_tx);
control_c_flag_tx.set(true);
}).expect("control C handler failed!");
//until control_c is caught, check the queue of incoming
//requests from the socket handler.
while !control_c_called_flag_rx.get() { while !control_c_called_flag_rx.get() {
match from_socket_rx.recv_timeout(Duration::from_millis(100)) { match from_socket_rx.recv_timeout(Duration::from_millis(100)) {
Ok(message) => { Ok(message) => {
println!("{}", message); println!("{}", message);
}, },
Err(_) => {continue}, Err(_) => {continue},
} }
} }
println!("ding"); //Close the listener thread
listener_can_run_flag.set(false); listener_can_run_flag.set(false);
listener_join_handle.join().unwrap(); listener_join_handle.join().unwrap();
} }
fn setup_control_c(mut control_c_flag_tx: sync_flag::SyncFlagTx) {
ctrlc::set_handler(move || {
control_c_flag_tx.set(true);
}).expect("control C handler failed!");
}