aquatic: move some fields from Config into new NetworkConfig

This commit is contained in:
Joakim Frostegård 2020-04-08 21:27:12 +02:00
parent fa60eddcc4
commit ca9d15c7a9
2 changed files with 26 additions and 12 deletions

View file

@ -3,17 +3,22 @@ use std::net::SocketAddr;
#[derive(Clone)]
pub struct Config {
pub address: SocketAddr,
pub num_threads: usize,
pub recv_buffer_size: usize,
pub poll_event_capacity: usize,
pub max_scrape_torrents: u8,
pub max_response_peers: usize,
pub network: NetworkConfig,
pub statistics: StatisticsConfig,
pub cleaning: CleaningConfig,
}
#[derive(Clone)]
pub struct NetworkConfig {
pub address: SocketAddr,
pub recv_buffer_size: usize,
pub poll_event_capacity: usize,
}
#[derive(Clone)]
pub struct StatisticsConfig {
@ -32,12 +37,10 @@ pub struct CleaningConfig {
impl Default for Config {
fn default() -> Self {
Self {
address: SocketAddr::from(([127, 0, 0, 1], 3000)),
num_threads: 4,
poll_event_capacity: 4096,
recv_buffer_size: 4096 * 128,
max_scrape_torrents: 255,
max_response_peers: 255,
network: NetworkConfig::default(),
statistics: StatisticsConfig::default(),
cleaning: CleaningConfig::default(),
}
@ -45,6 +48,17 @@ impl Default for Config {
}
impl Default for NetworkConfig {
fn default() -> Self {
Self {
address: SocketAddr::from(([127, 0, 0, 1], 3000)),
poll_event_capacity: 4096,
recv_buffer_size: 4096 * 128,
}
}
}
impl Default for StatisticsConfig {
fn default() -> Self {
Self {

View file

@ -31,7 +31,7 @@ pub fn run_event_loop(
.register(&mut socket, Token(token_num), interests)
.unwrap();
let mut events = Events::with_capacity(config.poll_event_capacity);
let mut events = Events::with_capacity(config.network.poll_event_capacity);
let mut connect_requests: Vec<(ConnectRequest, SocketAddr)> = Vec::with_capacity(1024);
let mut announce_requests: Vec<(AnnounceRequest, SocketAddr)> = Vec::with_capacity(1024);
@ -72,7 +72,7 @@ pub fn run_event_loop(
fn create_socket(config: &Config) -> ::std::net::UdpSocket {
let mut builder = &{
if config.address.is_ipv4(){
if config.network.address.is_ipv4(){
UdpBuilder::new_v4().expect("socket: build")
} else {
UdpBuilder::new_v6().expect("socket: build")
@ -82,16 +82,16 @@ fn create_socket(config: &Config) -> ::std::net::UdpSocket {
builder = builder.reuse_port(true)
.expect("socket: set reuse port");
let socket = builder.bind(&config.address)
.expect(&format!("socket: bind to {}", &config.address));
let socket = builder.bind(&config.network.address)
.expect(&format!("socket: bind to {}", &config.network.address));
socket.set_nonblocking(true)
.expect("socket: set nonblocking");
if let Err(err) = socket.set_recv_buffer_size(config.recv_buffer_size){
if let Err(err) = socket.set_recv_buffer_size(config.network.recv_buffer_size){
eprintln!(
"socket: failed setting recv buffer to {}: {:?}",
config.recv_buffer_size,
config.network.recv_buffer_size,
err
);
}