ecs server

This commit is contained in:
griffi-gh 2023-02-10 20:36:58 +01:00
parent 28a82cf69e
commit 055e6c3600
3 changed files with 41 additions and 7 deletions

View file

@ -6,3 +6,4 @@ edition = "2021"
[dependencies] [dependencies]
kubi-shared = { path = "../kubi-shared" } kubi-shared = { path = "../kubi-shared" }
kubi-udp = { path = "../kubi-udp" } kubi-udp = { path = "../kubi-udp" }
shipyard = "0.6"

View file

@ -1,14 +1,47 @@
use shipyard::{World, AllStoragesView, Unique, Workload, IntoWorkload, UniqueView, UniqueViewMut};
use kubi_udp::server::{Server, ServerConfig}; use kubi_udp::server::{Server, ServerConfig};
use kubi_shared::networking::messages::{ClientToServerMessage, ServerToClientMessage}; use kubi_shared::networking::messages::{ClientToServerMessage, ServerToClientMessage};
fn main() { #[derive(Unique)]
let mut server: Server<ServerToClientMessage, ClientToServerMessage> = Server::bind( #[repr(transparent)]
pub struct UdpServer(Server<ServerToClientMessage, ClientToServerMessage>);
fn bind_server(
storages: AllStoragesView,
) {
let server: Server<ServerToClientMessage, ClientToServerMessage> = Server::bind(
"0.0.0.0:1234".parse().unwrap(), "0.0.0.0:1234".parse().unwrap(),
ServerConfig::default() ServerConfig::default()
).unwrap(); ).unwrap();
storages.add_unique(UdpServer(server));
}
fn update_server(
mut server: UniqueViewMut<UdpServer>
) {
if let Err(error) = server.0.update() {
println!("Server error: {error:?}")
}
}
fn initialize() -> Workload {
(
bind_server,
).into_workload()
}
fn update() -> Workload {
(
update_server,
).into_workload()
}
fn main() {
let world = World::new();
world.add_workload(initialize);
world.add_workload(update);
world.run_workload(initialize).unwrap();
loop { loop {
if let Err(or) = server.update() { world.run_workload(update).unwrap();
println!("Server error: {or:?}")
}
} }
} }

View file

@ -48,7 +48,7 @@ pub struct Server<S, R> where S: Encode + Decode, R: Encode + Decode {
clients: HashMap<ClientId, ConnectedClient, BuildNoHashHasher<u8>>, clients: HashMap<ClientId, ConnectedClient, BuildNoHashHasher<u8>>,
config: ServerConfig, config: ServerConfig,
event_queue: VecDeque<ServerEvent<R>>, event_queue: VecDeque<ServerEvent<R>>,
_s: PhantomData<*const S>, _s: PhantomData<S>,
} }
impl<S, R> Server<S, R> where S: Encode + Decode, R: Encode + Decode { impl<S, R> Server<S, R> where S: Encode + Decode, R: Encode + Decode {
fn send_to_addr(&self, addr: SocketAddr, packet: IdServerPacket<S>) -> Result<()> { fn send_to_addr(&self, addr: SocketAddr, packet: IdServerPacket<S>) -> Result<()> {