diff --git a/src/bin/src/main.rs b/src/bin/src/main.rs index 16f1d5d..4950b8b 100644 --- a/src/bin/src/main.rs +++ b/src/bin/src/main.rs @@ -1,14 +1,13 @@ // Security or something like that #![forbid(unsafe_code)] - #![feature(slice_as_chunks)] use ferrumc_ecs::Universe; -use ferrumc_net::ServerState; -use std::sync::{Arc}; -use tracing::{error, info}; use ferrumc_net::server::create_server_listener; +use ferrumc_net::ServerState; +use std::sync::Arc; use systems::definition; +use tracing::{error, info}; pub(crate) mod errors; mod packet_handlers; @@ -33,19 +32,18 @@ async fn main() { async fn entry() -> Result<()> { let state = create_state().await?; let global_state = Arc::new(state); - - let all_systems = tokio::spawn(definition::start_all_systems(Arc::clone(&global_state))); + + let all_system_handles = tokio::spawn(definition::start_all_systems(global_state.clone())); // Start the systems and wait until all of them are done - all_systems.await??; - + all_system_handles.await??; + // Stop all systems definition::stop_all_systems(global_state).await?; Ok(()) } - async fn create_state() -> Result { let listener = create_server_listener().await?; @@ -53,4 +51,4 @@ async fn create_state() -> Result { universe: Universe::new(), tcp_listener: listener, }) -} \ No newline at end of file +} diff --git a/src/bin/src/systems/definition.rs b/src/bin/src/systems/definition.rs index 4590cb7..0fa3989 100644 --- a/src/bin/src/systems/definition.rs +++ b/src/bin/src/systems/definition.rs @@ -1,11 +1,11 @@ -use std::sync::Arc; -use ferrumc_net::{GlobalState, NetResult}; -use futures::stream::FuturesUnordered; -use tracing::{debug, debug_span, info, Instrument}; -use async_trait::async_trait; use crate::systems::keep_alive_system::KeepAliveSystem; use crate::systems::tcp_listener_system::TcpListenerSystem; use crate::systems::ticking_system::TickingSystem; +use async_trait::async_trait; +use ferrumc_net::{GlobalState, NetResult}; +use futures::stream::FuturesUnordered; +use std::sync::{Arc, LazyLock}; +use tracing::{debug, debug_span, info, Instrument}; #[async_trait] pub trait System: Send + Sync { @@ -15,7 +15,9 @@ pub trait System: Send + Sync { fn name(&self) -> &'static str; } - +static SYSTEMS: LazyLock>> = LazyLock::new(|| { + create_systems() +}); pub fn create_systems() -> Vec> { vec![ Arc::new(TcpListenerSystem), @@ -24,14 +26,14 @@ pub fn create_systems() -> Vec> { ] } pub async fn start_all_systems(state: GlobalState) -> NetResult<()> { - let systems = create_systems(); let handles = FuturesUnordered::new(); - for system in systems { + for system in SYSTEMS.iter() { let name = system.name(); let handle = tokio::spawn( system + .clone() .start(state.clone()) .instrument(debug_span!("sys", %name)), ); @@ -44,13 +46,12 @@ pub async fn start_all_systems(state: GlobalState) -> NetResult<()> { } pub async fn stop_all_systems(state: GlobalState) -> NetResult<()> { - let systems = create_systems(); info!("Stopping all systems..."); - for system in systems { + for system in SYSTEMS.iter() { debug!("Stopping system: {}", system.name()); - system.stop(state.clone()).await; + system.clone().stop(state.clone()).await; } Ok(()) -} \ No newline at end of file +}