upgrade some server stuff to uflow

This commit is contained in:
griffi-gh 2023-03-08 20:26:51 +01:00
parent 34a50c62b0
commit 56dd1a3bda
4 changed files with 47 additions and 30 deletions

View file

@ -8,7 +8,7 @@ publish = false
kubi-shared = { path = "../kubi-shared" } kubi-shared = { path = "../kubi-shared" }
kubi-logging = { path = "../kubi-logging" } kubi-logging = { path = "../kubi-logging" }
log = "*" 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"] } serde = { version = "1.0", default-features = false, features = ["alloc", "derive"] }
toml = "0.7" toml = "0.7"
glam = { version = "0.23", features = ["debug-glam-assert", "fast-math"] } glam = { version = "0.23", features = ["debug-glam-assert", "fast-math"] }

View file

@ -9,7 +9,7 @@ pub(crate) mod world;
pub(crate) mod auth; pub(crate) mod auth;
use config::read_config; 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 auth::authenticate_players;
use world::{update_world, init_world}; use world::{update_world, init_world};
@ -24,10 +24,12 @@ fn initialize() -> Workload {
fn update() -> Workload { fn update() -> Workload {
( (
update_server, update_server,
update_server_events, (
authenticate_players, log_server_errors,
update_world, authenticate_players,
).into_workload() update_world,
).into_workload()
).into_sequential_workload()
} }
fn main() { fn main() {

View file

@ -1,47 +1,62 @@
use shipyard::{AllStoragesView, Unique, UniqueView, UniqueViewMut}; use shipyard::{AllStoragesView, Unique, UniqueView, UniqueViewMut, NonSendSync};
use kubi_shared::networking::messages::{ClientToServerMessage, ServerToClientMessage}; use uflow::{server::{Server, Event as ServerEvent, Config as ServerConfig}, EndpointConfig};
use uflow::server::Server;
use std::time::Duration;
use crate::config::ConfigTable; use crate::config::ConfigTable;
#[derive(Unique)] #[derive(Unique)]
#[repr(transparent)] #[repr(transparent)]
pub struct UdpServer(pub Server<ServerToClientMessage, ClientToServerMessage>); pub struct UdpServer(pub Server);
#[derive(Unique, Default)] #[derive(Unique, Default)]
pub struct ServerEvents(pub Vec<ServerEvent<ClientToServerMessage>>); pub struct ServerEvents(pub Vec<ServerEvent>);
pub trait IsMessageOfType {
///Checks if postcard-encoded message has a type
fn is_message_of_type<const T: u8>(&self) -> bool;
}
impl IsMessageOfType for ServerEvent {
fn is_message_of_type<const T: u8>(&self) -> bool {
let ServerEvent::Receive(_, data) = &self else { return false };
if data.len() == 0 { return false }
data[0] == T
}
}
pub fn bind_server( pub fn bind_server(
storages: AllStoragesView, storages: AllStoragesView,
) { ) {
log::info!("Creating server..."); log::info!("Creating server...");
let config = storages.borrow::<UniqueView<ConfigTable>>().unwrap(); let config = storages.borrow::<UniqueView<ConfigTable>>().unwrap();
let server: Server<ServerToClientMessage, ClientToServerMessage> = Server::bind( let server = Server::bind(
config.server.address, config.server.address,
ServerConfig { ServerConfig {
max_clients: config.server.max_clients, max_total_connections: config.server.max_clients,
client_timeout: Duration::from_millis(config.server.timeout_ms), 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() ..Default::default()
} }
).unwrap(); ).expect("Failed to create the server");
storages.add_unique(UdpServer(server)); storages.add_unique_non_send_sync(UdpServer(server));
storages.add_unique(ServerEvents::default()); storages.add_unique(ServerEvents::default());
} }
pub fn update_server( pub fn update_server(
mut server: UniqueViewMut<UdpServer> mut server: NonSendSync<UniqueViewMut<UdpServer>>,
) {
if let Err(error) = server.0.update() {
log::error!("Server error: {error:?}")
}
}
pub fn update_server_events(
mut server: UniqueViewMut<UdpServer>,
mut events: UniqueViewMut<ServerEvents>, mut events: UniqueViewMut<ServerEvents>,
) { ) {
//drop current events
events.0.clear(); events.0.clear();
//fetch new ones events.0.extend(server.0.step());
events.0.extend(server.0.process_events().rev()); }
pub fn log_server_errors(
events: UniqueView<ServerEvents>,
) {
for event in &events.0 {
if let ServerEvent::Error(addr, error) = event {
log::error!("Server error addr: {addr} error: {error:?}");
}
}
} }