kubi/kubi-server/src/server.rs

47 lines
1.3 KiB
Rust
Raw Normal View History

2023-02-11 17:37:24 -06:00
use shipyard::{AllStoragesView, Unique, UniqueView, UniqueViewMut};
use kubi_shared::networking::messages::{ClientToServerMessage, ServerToClientMessage};
2023-02-12 18:53:55 -06:00
use std::time::Duration;
2023-02-11 17:37:24 -06:00
use crate::config::ConfigTable;
#[derive(Unique)]
#[repr(transparent)]
2023-02-13 21:27:27 -06:00
pub struct UdpServer(pub Server<ServerToClientMessage, ClientToServerMessage>);
#[derive(Unique, Default)]
pub struct ServerEvents(pub Vec<ServerEvent<ClientToServerMessage>>);
2023-02-11 17:37:24 -06:00
pub fn bind_server(
storages: AllStoragesView,
) {
log::info!("Creating server...");
let config = storages.borrow::<UniqueView<ConfigTable>>().unwrap();
let server: Server<ServerToClientMessage, ClientToServerMessage> = Server::bind(
config.server.address,
2023-02-12 18:53:55 -06:00
ServerConfig {
max_clients: config.server.max_clients,
client_timeout: Duration::from_millis(config.server.timeout_ms),
..Default::default()
}
2023-02-11 17:37:24 -06:00
).unwrap();
storages.add_unique(UdpServer(server));
2023-02-13 21:27:27 -06:00
storages.add_unique(ServerEvents::default());
2023-02-11 17:37:24 -06:00
}
pub fn update_server(
mut server: UniqueViewMut<UdpServer>
) {
if let Err(error) = server.0.update() {
log::error!("Server error: {error:?}")
}
}
2023-02-13 21:27:27 -06:00
pub fn update_server_events(
mut server: UniqueViewMut<UdpServer>,
mut events: UniqueViewMut<ServerEvents>,
) {
//drop current events
events.0.clear();
//fetch new ones
events.0.extend(server.0.process_events().rev());
}