From 1725c5a7c9c09c09a1ae9b55a5444eb143c04add Mon Sep 17 00:00:00 2001 From: griffi-gh Date: Wed, 8 Mar 2023 20:26:51 +0100 Subject: [PATCH] upgrade some server stuff to uflow --- kubi/README.md => README.md | 0 kubi-server/Cargo.toml | 2 +- kubi-server/src/main.rs | 12 ++++--- kubi-server/src/server.rs | 63 +++++++++++++++++++++++-------------- 4 files changed, 47 insertions(+), 30 deletions(-) rename kubi/README.md => README.md (100%) diff --git a/kubi/README.md b/README.md similarity index 100% rename from kubi/README.md rename to README.md diff --git a/kubi-server/Cargo.toml b/kubi-server/Cargo.toml index 6c6b67d..6fb5e3d 100644 --- a/kubi-server/Cargo.toml +++ b/kubi-server/Cargo.toml @@ -8,7 +8,7 @@ publish = false kubi-shared = { path = "../kubi-shared" } kubi-logging = { path = "../kubi-logging" } log = "*" -shipyard = { git = "https://github.com/leudz/shipyard", rev = "eb189f66" } +shipyard = { git = "https://github.com/leudz/shipyard", rev = "eb189f66", features = ["thread_local"] } serde = { version = "1.0", default-features = false, features = ["alloc", "derive"] } toml = "0.7" glam = { version = "0.23", features = ["debug-glam-assert", "fast-math"] } diff --git a/kubi-server/src/main.rs b/kubi-server/src/main.rs index fc24b57..a99dbf0 100644 --- a/kubi-server/src/main.rs +++ b/kubi-server/src/main.rs @@ -9,7 +9,7 @@ pub(crate) mod world; pub(crate) mod auth; use config::read_config; -use server::{bind_server, update_server, update_server_events}; +use server::{bind_server, update_server, log_server_errors}; use auth::authenticate_players; use world::{update_world, init_world}; @@ -24,10 +24,12 @@ fn initialize() -> Workload { fn update() -> Workload { ( update_server, - update_server_events, - authenticate_players, - update_world, - ).into_workload() + ( + log_server_errors, + authenticate_players, + update_world, + ).into_workload() + ).into_sequential_workload() } fn main() { diff --git a/kubi-server/src/server.rs b/kubi-server/src/server.rs index b862a31..8b84264 100644 --- a/kubi-server/src/server.rs +++ b/kubi-server/src/server.rs @@ -1,47 +1,62 @@ -use shipyard::{AllStoragesView, Unique, UniqueView, UniqueViewMut}; -use kubi_shared::networking::messages::{ClientToServerMessage, ServerToClientMessage}; -use uflow::server::Server; -use std::time::Duration; +use shipyard::{AllStoragesView, Unique, UniqueView, UniqueViewMut, NonSendSync}; +use uflow::{server::{Server, Event as ServerEvent, Config as ServerConfig}, EndpointConfig}; use crate::config::ConfigTable; #[derive(Unique)] #[repr(transparent)] -pub struct UdpServer(pub Server); +pub struct UdpServer(pub Server); #[derive(Unique, Default)] -pub struct ServerEvents(pub Vec>); +pub struct ServerEvents(pub Vec); + +pub trait IsMessageOfType { + ///Checks if postcard-encoded message has a type + fn is_message_of_type(&self) -> bool; +} +impl IsMessageOfType for ServerEvent { + fn is_message_of_type(&self) -> bool { + let ServerEvent::Receive(_, data) = &self else { return false }; + if data.len() == 0 { return false } + data[0] == T + } +} pub fn bind_server( storages: AllStoragesView, ) { log::info!("Creating server..."); let config = storages.borrow::>().unwrap(); - let server: Server = Server::bind( + let server = Server::bind( config.server.address, - ServerConfig { - max_clients: config.server.max_clients, - client_timeout: Duration::from_millis(config.server.timeout_ms), + ServerConfig { + max_total_connections: config.server.max_clients, + max_active_connections: config.server.max_clients, + enable_handshake_errors: true, + endpoint_config: EndpointConfig { + active_timeout_ms: config.server.timeout_ms, + ..Default::default() + }, ..Default::default() } - ).unwrap(); - storages.add_unique(UdpServer(server)); + ).expect("Failed to create the server"); + storages.add_unique_non_send_sync(UdpServer(server)); storages.add_unique(ServerEvents::default()); } pub fn update_server( - mut server: UniqueViewMut -) { - if let Err(error) = server.0.update() { - log::error!("Server error: {error:?}") - } -} - -pub fn update_server_events( - mut server: UniqueViewMut, + mut server: NonSendSync>, mut events: UniqueViewMut, ) { - //drop current events events.0.clear(); - //fetch new ones - events.0.extend(server.0.process_events().rev()); + events.0.extend(server.0.step()); +} + +pub fn log_server_errors( + events: UniqueView, +) { + for event in &events.0 { + if let ServerEvent::Error(addr, error) = event { + log::error!("Server error addr: {addr} error: {error:?}"); + } + } }