clients joining should no longer block acception runtime

This commit is contained in:
Luuk van Oijen committed 2023-11-08 12:52:34 +01:00
1 parent 3fedcf5bd5
commit e1b9f2b969
1 file changed
+30 -17
+30 -17
View File
@@ -3,7 +3,7 @@ use std::sync::{Arc, Mutex, atomic::{AtomicBool, Ordering}};
use std::time::Instant; use std::time::Instant;
use tokio::net::{TcpListener, UdpSocket}; use tokio::net::{TcpListener, UdpSocket};
use tokio::task::JoinHandle; use tokio::task::{JoinHandle, JoinSet};
use num_enum::IntoPrimitive; use num_enum::IntoPrimitive;
@@ -70,32 +70,45 @@ impl Server {
let clients_incoming_ref = Arc::clone(&clients_incoming); let clients_incoming_ref = Arc::clone(&clients_incoming);
debug!("Client acception runtime starting..."); debug!("Client acception runtime starting...");
let connect_runtime_handle = tokio::spawn(async move { let connect_runtime_handle = tokio::spawn(async move {
let mut set = JoinSet::new();
loop { loop {
match tcp_listener_ref.accept().await { match tcp_listener_ref.accept().await {
Ok((mut socket, addr)) => { Ok((mut socket, addr)) => {
info!("New client connected: {:?}", 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); set.spawn(async move {
match client.authenticate(&config_ref).await { socket.set_nodelay(true);
Ok(_) => {
let mut lock = clients_incoming_ref let mut client = Client::new(socket);
.lock() match client.authenticate(&cfg_ref).await {
.map_err(|e| error!("{:?}", e)) Ok(_) => {
.expect("Failed to acquire lock on mutex!"); let mut lock = ci_ref
lock.push(client); .lock()
drop(lock); .map_err(|e| error!("{:?}", e))
}, .expect("Failed to acquire lock on mutex!");
Err(e) => { lock.push(client);
error!("Authentication error occured, kicking player..."); drop(lock);
error!("{:?}", e); },
client.kick("Failed to authenticate player!").await; 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), 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!"); debug!("Client acception runtime started!");