Skip to content

Commit b33c1f2

Browse files
committed
make rperf as a lib
1 parent bdc9b10 commit b33c1f2

File tree

12 files changed

+30
-25
lines changed

12 files changed

+30
-25
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ readme = "README.md"
1414
chrono = "0.4"
1515
clap = { version = "4.4", features = ["derive", "wrap_help"] }
1616
core_affinity = "0.8"
17-
ctrlc2 = "3.5"
17+
ctrlc2 = { version = "3.5", features = ["termination"] }
1818
env_logger = "0.10"
1919
log = { version = "0.4", features = ["std"] }
2020
mio = { version = "0.8", features = ["log", "os-poll", "net"] }

src/client.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use crate::{
2727
results::{IntervalResultBox, IntervalResultKind, TcpTestResults, TestResults, UdpTestResults},
2828
},
2929
stream::{tcp, udp, TestStream},
30+
BoxResult,
3031
};
3132
use mio::net::TcpStream;
3233
use std::{
@@ -40,8 +41,6 @@ use std::{
4041
time::{Duration, SystemTime, UNIX_EPOCH},
4142
};
4243

43-
type BoxResult<T> = Result<T, Box<dyn std::error::Error + Send + Sync + 'static>>;
44-
4544
/// when false, the system is shutting down
4645
static ALIVE: AtomicBool = AtomicBool::new(true);
4746

src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
pub mod args;
2+
pub mod client;
3+
pub(crate) mod protocol;
4+
pub mod server;
5+
pub(crate) mod stream;
6+
pub(crate) mod utils;
7+
8+
pub(crate) type BoxResult<T> = Result<T, Box<dyn std::error::Error + Send + Sync + 'static>>;

src/main.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,7 @@
1818
* along with rperf. If not, see <https://www.gnu.org/licenses/>.
1919
*/
2020

21-
mod args;
22-
mod client;
23-
mod protocol;
24-
mod server;
25-
mod stream;
26-
mod utils;
21+
use rperf::{args, client, server};
2722

2823
fn main() {
2924
use clap::Parser;
@@ -37,7 +32,7 @@ fn main() {
3732

3833
if args.server {
3934
log::debug!("registering SIGINT handler...");
40-
ctrlc2::set_handler(move || {
35+
let exiting = ctrlc2::set_handler(move || {
4136
if server::kill() {
4237
log::warn!("shutdown requested; please allow a moment for any in-progress tests to stop");
4338
} else {
@@ -50,6 +45,7 @@ fn main() {
5045

5146
log::debug!("beginning normal operation...");
5247
let service = server::serve(&args);
48+
exiting.join().expect("unable to join SIGINT handler thread");
5349
if service.is_err() {
5450
log::error!("unable to run server: {}", service.unwrap_err());
5551
std::process::exit(4);

src/protocol/communication.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use std::time::Duration;
2424
use mio::net::TcpStream;
2525
use mio::{Events, Interest, Poll, Token};
2626

27-
type BoxResult<T> = Result<T, Box<dyn std::error::Error + Send + Sync + 'static>>;
27+
use crate::BoxResult;
2828

2929
/// how long to wait for keepalive events
3030
// the communications channels typically exchange data every second, so 2s is reasonable to avoid excess noise

src/protocol/messaging.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* along with rperf. If not, see <https://www.gnu.org/licenses/>.
1919
*/
2020

21-
type BoxResult<T> = Result<T, Box<dyn std::error::Error + Send + Sync + 'static>>;
21+
use crate::BoxResult;
2222

2323
/// prepares a message used to tell the server to begin operations
2424
pub fn prepare_begin() -> serde_json::Value {

src/protocol/results.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,11 @@
1818
* along with rperf. If not, see <https://www.gnu.org/licenses/>.
1919
*/
2020

21+
use crate::BoxResult;
22+
use serde::{Deserialize, Serialize};
2123
use std::collections::{HashMap, HashSet};
2224
use std::time::{SystemTime, UNIX_EPOCH};
2325

24-
use serde::{Deserialize, Serialize};
25-
26-
type BoxResult<T> = Result<T, Box<dyn std::error::Error + Send + Sync + 'static>>;
27-
2826
/* This module contains structures used to represent and collect the results of tests.
2927
* Since everything is basically just a data-container with representation methods,
3028
* it isn't extensively documented.

src/server.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ use crate::protocol::communication::{receive, send, KEEPALIVE_DURATION};
3434
use crate::protocol::messaging::{prepare_connect, prepare_connect_ready};
3535
use crate::protocol::results::ServerDoneResult;
3636
use crate::stream::{tcp, udp, TestStream};
37-
38-
type BoxResult<T> = Result<T, Box<dyn std::error::Error + Send + Sync + 'static>>;
37+
use crate::BoxResult;
3938

4039
const POLL_TIMEOUT: Duration = Duration::from_millis(500);
4140

@@ -335,7 +334,14 @@ pub fn serve(args: &Args) -> BoxResult<()> {
335334
let mut events = Events::with_capacity(32);
336335

337336
while is_alive() {
338-
poll.poll(&mut events, Some(POLL_TIMEOUT))?;
337+
if let Err(err) = poll.poll(&mut events, Some(POLL_TIMEOUT)) {
338+
if err.kind() == std::io::ErrorKind::Interrupted {
339+
log::debug!("Poll interrupted: \"{err}\", ignored, continue polling");
340+
continue;
341+
}
342+
log::error!("Poll error: {}", err);
343+
break;
344+
}
339345
for event in events.iter() {
340346
event.token();
341347
loop {

src/stream/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
pub mod tcp;
2222
pub mod udp;
2323

24-
type BoxResult<T> = Result<T, Box<dyn std::error::Error + Send + Sync + 'static>>;
24+
use crate::BoxResult;
2525

2626
pub const INTERVAL: std::time::Duration = std::time::Duration::from_secs(1);
2727

src/stream/tcp.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,10 @@
2121
use nix::sys::socket::{setsockopt, sockopt::RcvBuf, sockopt::SndBuf};
2222

2323
use crate::protocol::results::{get_unix_timestamp, IntervalResult, TcpReceiveResult, TcpSendResult};
24+
use crate::BoxResult;
2425

2526
use super::{parse_port_spec, TestStream, INTERVAL};
2627

27-
type BoxResult<T> = Result<T, Box<dyn std::error::Error + Send + Sync + 'static>>;
28-
2928
pub const TEST_HEADER_SIZE: usize = 16;
3029

3130
#[derive(Clone)]

0 commit comments

Comments
 (0)