Skip to content

Commit

Permalink
Merge pull request #102 from AnonymousBit0111/rewrite/v3
Browse files Browse the repository at this point in the history
Fixed stop_all_systems creating new systems to destroy
  • Loading branch information
ReCore-sys authored Oct 29, 2024
2 parents 1e24a8a + 1088c9f commit 80c1a7c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 22 deletions.
18 changes: 8 additions & 10 deletions src/bin/src/main.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -33,24 +32,23 @@ 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<ServerState> {
let listener = create_server_listener().await?;

Ok(ServerState {
universe: Universe::new(),
tcp_listener: listener,
})
}
}
25 changes: 13 additions & 12 deletions src/bin/src/systems/definition.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -15,7 +15,9 @@ pub trait System: Send + Sync {
fn name(&self) -> &'static str;
}


static SYSTEMS: LazyLock<Vec<Arc<dyn System>>> = LazyLock::new(|| {
create_systems()
});
pub fn create_systems() -> Vec<Arc<dyn System>> {
vec![
Arc::new(TcpListenerSystem),
Expand All @@ -24,14 +26,14 @@ pub fn create_systems() -> Vec<Arc<dyn System>> {
]
}
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)),
);
Expand All @@ -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(())
}
}

0 comments on commit 80c1a7c

Please sign in to comment.