use serde::{Deserialize, Serialize};
use std::net::IpAddr;
use std::time::Duration;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum Protocol {
Tcp,
Udp,
}
impl Protocol {
pub const fn as_str(self) -> &'static str {
match self {
Protocol::Tcp => "Tcp",
Protocol::Udp => "Udp",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum Mode {
Server,
Client,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Config {
pub mode: Mode,
pub protocol: Protocol,
pub port: u16,
pub server_addr: Option<String>,
pub bind_addr: Option<IpAddr>,
pub duration: Duration,
pub bandwidth: Option<u64>,
pub buffer_size: usize,
pub parallel: usize,
pub reverse: bool,
pub json: bool,
pub interval: Duration,
}
impl Default for Config {
fn default() -> Self {
Self {
mode: Mode::Client,
protocol: Protocol::Tcp,
port: 5201,
server_addr: None,
bind_addr: None,
duration: Duration::from_secs(10),
bandwidth: None,
buffer_size: 128 * 1024, parallel: 1,
reverse: false,
json: false,
interval: Duration::from_secs(1),
}
}
}
impl Config {
pub fn new() -> Self {
Self::default()
}
pub fn server(port: u16) -> Self {
Self {
mode: Mode::Server,
port,
..Default::default()
}
}
pub fn client(server_addr: String, port: u16) -> Self {
Self {
mode: Mode::Client,
server_addr: Some(server_addr),
port,
..Default::default()
}
}
pub fn with_protocol(mut self, protocol: Protocol) -> Self {
self.protocol = protocol;
self
}
pub fn with_duration(mut self, duration: Duration) -> Self {
self.duration = duration;
self
}
pub fn with_bandwidth(mut self, bandwidth: u64) -> Self {
self.bandwidth = Some(bandwidth);
self
}
pub fn with_buffer_size(mut self, size: usize) -> Self {
self.buffer_size = size;
self
}
pub fn with_parallel(mut self, parallel: usize) -> Self {
self.parallel = parallel;
self
}
pub fn with_reverse(mut self, reverse: bool) -> Self {
self.reverse = reverse;
self
}
pub fn with_json(mut self, json: bool) -> Self {
self.json = json;
self
}
pub fn with_interval(mut self, interval: Duration) -> Self {
self.interval = interval;
self
}
}