2023-02-11 17:37:24 -06:00
|
|
|
use shipyard::{AllStoragesView, Unique, UniqueView, UniqueViewMut};
|
|
|
|
use kubi_udp::server::{Server, ServerConfig};
|
|
|
|
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)]
|
|
|
|
pub struct UdpServer(Server<ServerToClientMessage, ClientToServerMessage>);
|
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn update_server(
|
|
|
|
mut server: UniqueViewMut<UdpServer>
|
|
|
|
) {
|
|
|
|
if let Err(error) = server.0.update() {
|
|
|
|
log::error!("Server error: {error:?}")
|
|
|
|
}
|
|
|
|
}
|