summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStuart Stock <stuart@int08h.com>2020-09-27 20:32:40 -0500
committerStuart Stock <stuart@int08h.com>2020-09-27 20:34:02 -0500
commit7338262e70ad88734de933b09499f7b0ade81312 (patch)
tree9ce6f7ae47f17d81ba226e25d569190443f2aa75
parent3848ca8d698693eedbcc7c039c8d15dafc09d047 (diff)
downloadroughenough-7338262e70ad88734de933b09499f7b0ade81312.zip
Simplify ctrl-c check; check only in outer server loop
-rw-r--r--src/bin/roughenough-server.rs28
-rw-r--r--src/server.rs28
2 files changed, 14 insertions, 42 deletions
diff --git a/src/bin/roughenough-server.rs b/src/bin/roughenough-server.rs
index 4a3096c..bb20b21 100644
--- a/src/bin/roughenough-server.rs
+++ b/src/bin/roughenough-server.rs
@@ -25,7 +25,7 @@ extern crate log;
use std::env;
use std::process;
-use std::sync::atomic::Ordering;
+use std::sync::atomic::{Ordering, AtomicBool};
use roughenough::config;
use roughenough::config::ServerConfig;
@@ -33,32 +33,28 @@ use roughenough::roughenough_version;
use roughenough::server::Server;
use mio::Events;
-
-macro_rules! check_ctrlc {
- ($keep_running:expr) => {
- if !$keep_running.load(Ordering::Acquire) {
- warn!("Ctrl-C caught, exiting...");
- return;
- }
- };
-}
+use std::sync::Arc;
+use simple_logger::SimpleLogger;
+use log::LevelFilter;
fn polling_loop(config: Box<dyn ServerConfig>) {
let mut server = Server::new(config);
+ let keep_running = Arc::new(AtomicBool::new(true));
let cfg = server.get_config();
- display_config(&server, cfg);
- let kr = server.get_keep_running();
- let kr0 = kr.clone();
+ display_config(&server, cfg);
- ctrlc::set_handler(move || kr.store(false, Ordering::Release))
+ let kr_clone = keep_running.clone();
+ ctrlc::set_handler(move || kr_clone.store(false, Ordering::Release))
.expect("failed setting Ctrl-C handler");
let mut events = Events::with_capacity(1024);
loop {
- check_ctrlc!(kr0);
- if server.process_events(&mut events) {
+ server.process_events(&mut events);
+
+ if !keep_running.load(Ordering::Acquire) {
+ warn!("Ctrl-C caught, exiting...");
return;
}
}
diff --git a/src/server.rs b/src/server.rs
index 91845de..3a216d1 100644
--- a/src/server.rs
+++ b/src/server.rs
@@ -21,8 +21,6 @@ use std::io::ErrorKind;
use std::io::Write;
use std::net::{IpAddr, Shutdown, SocketAddr};
use std::process;
-use std::sync::atomic::{AtomicBool, Ordering};
-use std::sync::Arc;
use std::time::{Duration, SystemTime};
use byteorder::{LittleEndian, WriteBytesExt};
@@ -41,15 +39,6 @@ use crate::merkle::MerkleTree;
use crate::stats::{AggregatedStats, ClientStatEntry, PerClientStats, ServerStats};
use crate::{Error, RtMessage, Tag, MIN_REQUEST_LENGTH};
-macro_rules! check_ctrlc {
- ($keep_running:expr) => {
- if !$keep_running.load(Ordering::Acquire) {
- warn!("Ctrl-C caught, exiting...");
- return true;
- }
- };
-}
-
// mio event registrations
const EVT_MESSAGE: Token = Token(0);
const EVT_STATUS_UPDATE: Token = Token(1);
@@ -75,7 +64,6 @@ pub struct Server {
socket: UdpSocket,
health_listener: Option<TcpListener>,
- keep_running: Arc<AtomicBool>,
poll_duration: Option<Duration>,
grease: Grease,
timer: Timer<()>,
@@ -116,8 +104,6 @@ impl Server {
long_term_key.make_cert(&online_key).encode().unwrap()
};
- let keep_running = Arc::new(AtomicBool::new(true));
-
let sock_addr = config.udp_socket_addr().expect("udp sock addr");
let socket = UdpSocket::bind(&sock_addr).expect("failed to bind to socket");
@@ -166,7 +152,6 @@ impl Server {
socket,
health_listener,
- keep_running,
poll_duration,
grease,
timer,
@@ -199,11 +184,6 @@ impl Server {
self.config.as_ref()
}
- /// Returns a reference counted pointer the this server's `keep_running` value.
- pub fn get_keep_running(&self) -> Arc<AtomicBool> {
- self.keep_running.clone()
- }
-
#[cfg(fuzzing)]
pub fn send_to_self(&mut self, data: &[u8]) {
let res = self
@@ -213,10 +193,9 @@ impl Server {
}
/// The main processing function for incoming connections. This method should be
- /// called repeatedly in a loop to process requests. It returns 'true' when the
- /// server has shutdown (due to keep_running being set to 'false').
+ /// called repeatedly in a loop to process requests.
///
- pub fn process_events(&mut self, events: &mut Events) -> bool {
+ pub fn process_events(&mut self, events: &mut Events) {
self.poll
.poll(events, self.poll_duration)
.expect("server event poll failed; cannot recover");
@@ -224,8 +203,6 @@ impl Server {
for msg in events.iter() {
match msg.token() {
EVT_MESSAGE => loop {
- check_ctrlc!(self.keep_running);
-
self.merkle.reset();
self.requests.clear();
@@ -246,7 +223,6 @@ impl Server {
_ => unreachable!(),
}
}
- false
}
// Read and process client requests from socket into `self.requests` and `self.merkle` until