Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed stop_all_systems creating new systems to destroy #102

Merged
merged 4 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(())
}
}