diff --git a/neqo-http3-server/src/main.rs b/neqo-http3-server/src/main.rs index 80b373cbaa..73ee8f9ad1 100644 --- a/neqo-http3-server/src/main.rs +++ b/neqo-http3-server/src/main.rs @@ -238,42 +238,24 @@ fn process( } } -#[allow(clippy::cognitive_complexity)] -fn main() -> Result<(), io::Error> { - let mut args = Args::from_args(); - assert!(!args.key.is_empty(), "Need at least one key"); - - init_db(args.db.clone()); - - if args.qns_mode { - match env::var("TESTCASE") { - Ok(s) if s == "http3" => {} - Ok(s) if s == "handshake" || s == "transfer" || s == "retry" => { - args.use_old_http = true; - args.alpn = "hq-29".into(); - } - - Ok(_) => exit(127), - Err(_) => exit(1), - } - - if let Ok(qlogdir) = env::var("QLOGDIR") { - args.qlog_dir = Some(PathBuf::from(qlogdir)); - } - } - +/// Init Poll for all hosts. Returns the Poll, sockets, and a map of the +/// socketaddrs to instances of the HttpServer handling that addr. +#[allow(clippy::type_complexity)] +fn init_poll( + hosts: &[SocketAddr], + args: &Args, +) -> Result< + ( + Poll, + Vec, + HashMap, Option)>, + ), + io::Error, +> { let poll = Poll::new()?; - let hosts = args.host_socket_addrs(); - if hosts.is_empty() { - eprintln!("No valid hosts defined"); - exit(1); - } - let mut sockets = Vec::new(); let mut servers = HashMap::new(); - let mut timer = Builder::default().build::(); - poll.register(&timer, TIMER_TOKEN, Ready::readable(), PollOpt::edge())?; for (i, host) in hosts.iter().enumerate() { let socket = match UdpSocket::bind(&host) { @@ -356,6 +338,43 @@ fn main() -> Result<(), io::Error> { ); } + Ok((poll, sockets, servers)) +} + +fn main() -> Result<(), io::Error> { + let mut args = Args::from_args(); + assert!(!args.key.is_empty(), "Need at least one key"); + + init_db(args.db.clone()); + + if args.qns_mode { + match env::var("TESTCASE") { + Ok(s) if s == "http3" => {} + Ok(s) if s == "handshake" || s == "transfer" || s == "retry" => { + args.use_old_http = true; + args.alpn = "hq-29".into(); + } + + Ok(_) => exit(127), + Err(_) => exit(1), + } + + if let Ok(qlogdir) = env::var("QLOGDIR") { + args.qlog_dir = Some(PathBuf::from(qlogdir)); + } + } + + let hosts = args.host_socket_addrs(); + if hosts.is_empty() { + eprintln!("No valid hosts defined"); + exit(1); + } + + let (poll, mut sockets, mut servers) = init_poll(&hosts, &args)?; + + let mut timer = Builder::default().build::(); + poll.register(&timer, TIMER_TOKEN, Ready::readable(), PollOpt::edge())?; + let buf = &mut [0u8; 2048]; let mut events = Events::with_capacity(1024);