From e1b9f2b9694b59dfe2b35eed99ad57866452b3a9 Mon Sep 17 00:00:00 2001 From: Luuk van Oijen Date: Wed, 8 Nov 2023 12:52:34 +0100 Subject: [PATCH] clients joining should no longer block acception runtime --- src/server/mod.rs | 47 ++++++++++++++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 17 deletions(-) diff --git a/src/server/mod.rs b/src/server/mod.rs index 7efb521..e92e577 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -3,7 +3,7 @@ use std::sync::{Arc, Mutex, atomic::{AtomicBool, Ordering}}; use std::time::Instant; use tokio::net::{TcpListener, UdpSocket}; -use tokio::task::JoinHandle; +use tokio::task::{JoinHandle, JoinSet}; use num_enum::IntoPrimitive; @@ -70,32 +70,45 @@ impl Server { let clients_incoming_ref = Arc::clone(&clients_incoming); debug!("Client acception runtime starting..."); let connect_runtime_handle = tokio::spawn(async move { + let mut set = JoinSet::new(); loop { match tcp_listener_ref.accept().await { Ok((mut socket, addr)) => { info!("New client connected: {:?}", addr); - socket.set_nodelay(true); + let cfg_ref = config_ref.clone(); + let ci_ref = clients_incoming_ref.clone(); - let mut client = Client::new(socket); - match client.authenticate(&config_ref).await { - Ok(_) => { - let mut lock = clients_incoming_ref - .lock() - .map_err(|e| error!("{:?}", e)) - .expect("Failed to acquire lock on mutex!"); - lock.push(client); - drop(lock); - }, - Err(e) => { - error!("Authentication error occured, kicking player..."); - error!("{:?}", e); - client.kick("Failed to authenticate player!").await; + set.spawn(async move { + socket.set_nodelay(true); + + let mut client = Client::new(socket); + match client.authenticate(&cfg_ref).await { + Ok(_) => { + let mut lock = ci_ref + .lock() + .map_err(|e| error!("{:?}", e)) + .expect("Failed to acquire lock on mutex!"); + lock.push(client); + drop(lock); + }, + Err(e) => { + error!("Authentication error occured, kicking player..."); + error!("{:?}", e); + client.kick("Failed to authenticate player!").await; + } } - } + }); } Err(e) => error!("Failed to accept incoming connection: {:?}", e), } + + if set.is_empty() == false { + tokio::select!( + _ = set.join_next() => {}, + _ = tokio::time::sleep(tokio::time::Duration::from_secs(1)) => {}, + ) + } } }); debug!("Client acception runtime started!");