-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlib.rs
143 lines (128 loc) · 5.5 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
extern crate clap;
use std::io::Read;
use std::io::Write;
use clap::{Arg, App};
pub struct Settings {
pub non_blocking: bool,
pub warm_up_count: u64,
pub msg_count: u64,
pub msg_size: u64,
pub sleep_time: u64,
pub ponger_addr: String,
pub pinger_addr: String,
pub tcp: bool,
pub udp: bool,
}
pub fn parse_settings() -> Settings {
let matches = App::new("Ping/Pong")
.version("1.0")
.about("Latency testing")
.arg(Arg::with_name("non_blocking")
.long("--poll")
.short("p")
.help(""))
.arg(Arg::with_name("tcp")
.long("--tcp")
.short("t")
.required_unless("udp")
.help(""))
.arg(Arg::with_name("udp")
.long("--udp")
.short("u")
.required_unless("tcp")
.help(""))
.arg(Arg::with_name("warmup_messages")
.long("--warmup-messages")
.short("w")
.takes_value(true)
.help(""))
.arg(Arg::with_name("messages")
.long("--messages")
.short("m")
.takes_value(true)
.validator(|val| {
if val.parse::<u64>().unwrap() == 0 {
return Err("Need to send at least one message".to_string());
}
return Ok(());
})
.help(""))
.arg(Arg::with_name("message_size")
.long("--message-size")
.short("s")
.takes_value(true)
.validator(|val| {
if val.parse::<u64>().unwrap() > 65000 {
return Err("Messages bigger than 65k are not supported yet".to_string());
}
return Ok(());
})
.help(""))
.arg(Arg::with_name("sleep_time")
.long("--sleep-time")
.takes_value(true)
.help(""))
.arg(Arg::with_name("ponger")
.long("--ponger-addr")
.short("o")
.takes_value(true)
.help(""))
.arg(Arg::with_name("pinger")
.long("--pinger-addr")
.short("i")
.takes_value(true)
.help(""))
.get_matches();
return Settings{
non_blocking: matches.is_present("non_blocking"),
warm_up_count: matches.value_of("warmup_messages").unwrap_or("1000").parse::<u64>().unwrap(),
msg_count: matches.value_of("messages").unwrap_or("1000").parse::<u64>().unwrap(),
msg_size: matches.value_of("message_size").unwrap_or("64").parse::<u64>().unwrap(),
sleep_time: matches.value_of("sleep_time").unwrap_or("0").parse::<u64>().unwrap(),
ponger_addr: matches.value_of("remote").unwrap_or("localhost:10000").to_string(),
pinger_addr: matches.value_of("local").unwrap_or("localhost:10001").to_string(),
tcp: matches.is_present("tcp"),
udp: matches.is_present("udp"),
};
}
pub trait Sender {
fn send_data(&mut self, &[u8]) -> std::io::Result<usize>;
fn recv_data(&mut self, &mut [u8]) -> std::io::Result<usize>;
fn set_busy(&self, busy: bool) -> std::io::Result<()>;
}
impl Sender for std::net::TcpStream {
fn send_data(&mut self, buf: &[u8]) -> std::io::Result<usize> {
return self.write(buf);
}
fn recv_data(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
return self.read(buf);
}
fn set_busy(&self, busy: bool) -> std::io::Result<()> {
self.set_nodelay(busy)?;
self.set_nonblocking(busy)?;
return Ok(());
}
}
impl Sender for std::net::UdpSocket {
fn send_data(&mut self, buf: &[u8]) -> std::io::Result<usize> {
return self.send(buf);
}
fn recv_data(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
return self.recv(buf);
}
fn set_busy(&self, busy: bool) -> std::io::Result<()> {
self.set_nonblocking(busy)?;
return Ok(());
}
}
pub fn read_busy_until_some<Socket: Sender>(sock: &mut Socket, mut buf: &mut [u8]) -> std::io::Result<usize> {
loop {
return match sock.recv_data(&mut buf) {
Ok(bytes_read) => Ok(bytes_read),
Err(ref err) if err.kind() == std::io::ErrorKind::WouldBlock => continue,
Err(ref err) if err.kind() == std::io::ErrorKind::ConnectionReset => Ok(0),
Err(err) => Err(err),
};
}
}