kubi/kubi-server/src/main.rs

58 lines
1.4 KiB
Rust
Raw Normal View History

2023-02-10 19:36:58 +00:00
use shipyard::{World, AllStoragesView, Unique, Workload, IntoWorkload, UniqueView, UniqueViewMut};
2023-02-10 19:26:03 +00:00
use kubi_udp::server::{Server, ServerConfig};
use kubi_shared::networking::messages::{ClientToServerMessage, ServerToClientMessage};
2023-02-10 21:11:18 +00:00
use std::{thread, time::Duration, net::SocketAddr};
#[derive(Unique)]
#[repr(transparent)]
pub struct ServerAddr(SocketAddr);
2023-02-10 19:26:03 +00:00
2023-02-10 19:36:58 +00:00
#[derive(Unique)]
#[repr(transparent)]
pub struct UdpServer(Server<ServerToClientMessage, ClientToServerMessage>);
fn bind_server(
storages: AllStoragesView,
) {
2023-02-10 21:11:18 +00:00
log::info!("Creating server...");
let addr = storages.borrow::<UniqueView<SocketAddr>>().expect("No server addr found");
2023-02-10 19:36:58 +00:00
let server: Server<ServerToClientMessage, ClientToServerMessage> = Server::bind(
2023-02-10 19:26:03 +00:00
"0.0.0.0:1234".parse().unwrap(),
ServerConfig::default()
).unwrap();
2023-02-10 19:36:58 +00:00
storages.add_unique(UdpServer(server));
}
fn update_server(
mut server: UniqueViewMut<UdpServer>
) {
if let Err(error) = server.0.update() {
2023-02-10 21:05:10 +00:00
log::error!("Server error: {error:?}")
2023-02-10 19:36:58 +00:00
}
}
fn initialize() -> Workload {
(
bind_server,
).into_workload()
}
fn update() -> Workload {
(
update_server,
).into_workload()
}
fn main() {
2023-02-10 19:44:34 +00:00
kubi_logging::init();
2023-02-10 19:36:58 +00:00
let world = World::new();
world.add_workload(initialize);
world.add_workload(update);
world.run_workload(initialize).unwrap();
2023-02-10 21:11:18 +00:00
log::info!("The server is now running");
2023-02-10 19:26:03 +00:00
loop {
2023-02-10 19:36:58 +00:00
world.run_workload(update).unwrap();
2023-02-10 21:05:10 +00:00
thread::sleep(Duration::from_millis(16));
2023-02-10 19:26:03 +00:00
}
2023-01-30 00:46:22 +00:00
}