mirror of
https://github.com/griffi-gh/kubi.git
synced 2024-11-21 14:28:43 -06:00
upgrade some server stuff to uflow
This commit is contained in:
parent
34a50c62b0
commit
56dd1a3bda
|
@ -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"] }
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -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<ServerToClientMessage, ClientToServerMessage>);
|
||||
pub struct UdpServer(pub Server);
|
||||
|
||||
#[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(
|
||||
storages: AllStoragesView,
|
||||
) {
|
||||
log::info!("Creating server...");
|
||||
let config = storages.borrow::<UniqueView<ConfigTable>>().unwrap();
|
||||
let server: Server<ServerToClientMessage, ClientToServerMessage> = 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),
|
||||
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<UdpServer>
|
||||
) {
|
||||
if let Err(error) = server.0.update() {
|
||||
log::error!("Server error: {error:?}")
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update_server_events(
|
||||
mut server: UniqueViewMut<UdpServer>,
|
||||
mut server: NonSendSync<UniqueViewMut<UdpServer>>,
|
||||
mut events: UniqueViewMut<ServerEvents>,
|
||||
) {
|
||||
//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<ServerEvents>,
|
||||
) {
|
||||
for event in &events.0 {
|
||||
if let ServerEvent::Error(addr, error) = event {
|
||||
log::error!("Server error addr: {addr} error: {error:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue