diff options
author | Henauxg <19689618+Henauxg@users.noreply.github.com> | 2022-11-02 19:18:05 +0100 |
---|---|---|
committer | Henauxg <19689618+Henauxg@users.noreply.github.com> | 2022-11-02 19:18:05 +0100 |
commit | 5a81871d2f48dc54eac1801be084af5ae9e262c5 (patch) | |
tree | 8dc63e02e3cce9c3233c1557b9fc9848cbb03212 | |
parent | 1655dfb503af20f0de37cbca694443b8bac35def (diff) |
[client & server ] Add ServerState, server.is_listening and only build the async runtime once for client+server apps
-rw-r--r-- | src/client.rs | 25 | ||||
-rw-r--r-- | src/server.rs | 47 |
2 files changed, 49 insertions, 23 deletions
diff --git a/src/client.rs b/src/client.rs index 382ba66..5192b49 100644 --- a/src/client.rs +++ b/src/client.rs @@ -413,16 +413,19 @@ impl Default for QuinnetClientPlugin { impl Plugin for QuinnetClientPlugin { fn build(&self, app: &mut App) { - app.insert_resource( - tokio::runtime::Builder::new_multi_thread() - .enable_all() - .build() - .unwrap(), - ) - .add_event::<ConnectionEvent>() - .add_event::<ConnectionLostEvent>() - // StartupStage::PreStartup so that resources created in commands are available to default startup_systems - .add_startup_system_to_stage(StartupStage::PreStartup, start_async_client) - .add_system(update_sync_client); + app.add_event::<ConnectionEvent>() + .add_event::<ConnectionLostEvent>() + // StartupStage::PreStartup so that resources created in commands are available to default startup_systems + .add_startup_system_to_stage(StartupStage::PreStartup, start_async_client) + .add_system(update_sync_client); + + if app.world.get_resource_mut::<Runtime>().is_none() { + app.insert_resource( + tokio::runtime::Builder::new_multi_thread() + .enable_all() + .build() + .unwrap(), + ); + } } } diff --git a/src/server.rs b/src/server.rs index 7f4c9c5..162156e 100644 --- a/src/server.rs +++ b/src/server.rs @@ -104,6 +104,13 @@ pub enum CertificateRetrievalMode { }, } +/// Current state of the client driver +#[derive(Debug, PartialEq, Eq)] +enum ServerState { + Idle, + Listening, +} + #[derive(Debug)] pub(crate) enum InternalAsyncMessage { ClientConnected(ClientConnection), @@ -129,6 +136,7 @@ pub(crate) struct ClientConnection { pub struct Server { clients: HashMap<ClientId, ClientConnection>, receiver: mpsc::Receiver<ClientPayload>, + state: ServerState, pub(crate) internal_receiver: mpsc::Receiver<InternalAsyncMessage>, pub(crate) internal_sender: broadcast::Sender<InternalSyncMessage>, @@ -137,7 +145,7 @@ pub struct Server { impl Server { /// Run the server with the given [ServerConfigurationData] and [CertificateRetrievalMode] pub fn start( - &self, + &mut self, config: ServerConfigurationData, cert_mode: CertificateRetrievalMode, ) -> Result<(), QuinnetError> { @@ -145,11 +153,22 @@ impl Server { .internal_sender .send(InternalSyncMessage::StartListening { config, cert_mode }) { - Ok(_) => Ok(()), + Ok(_) => { + self.state = ServerState::Listening; + Ok(()) + } Err(_) => Err(QuinnetError::FullQueue), } } + /// Returns true if the server is currently listening for messages and connections. + pub fn is_listening(&self) -> bool { + match self.state { + ServerState::Idle => false, + ServerState::Listening => true, + } + } + pub fn disconnect_client(&mut self, client_id: ClientId) { match self.clients.remove(&client_id) { Some(client_connection) => { @@ -522,6 +541,7 @@ fn start_async_server(mut commands: Commands, runtime: Res<Runtime>) { commands.insert_resource(Server { clients: HashMap::new(), receiver: from_clients_receiver, + state: ServerState::Idle, internal_receiver: from_async_server, internal_sender: to_async_server.clone(), }); @@ -582,15 +602,18 @@ impl Default for QuinnetServerPlugin { impl Plugin for QuinnetServerPlugin { fn build(&self, app: &mut App) { - app.insert_resource( - tokio::runtime::Builder::new_multi_thread() - .enable_all() - .build() - .unwrap(), - ) - .add_event::<ConnectionEvent>() - .add_event::<ConnectionLostEvent>() - .add_startup_system_to_stage(StartupStage::PreStartup, start_async_server) - .add_system_to_stage(CoreStage::PreUpdate, update_sync_server); + app.add_event::<ConnectionEvent>() + .add_event::<ConnectionLostEvent>() + .add_startup_system_to_stage(StartupStage::PreStartup, start_async_server) + .add_system_to_stage(CoreStage::PreUpdate, update_sync_server); + + if app.world.get_resource_mut::<Runtime>().is_none() { + app.insert_resource( + tokio::runtime::Builder::new_multi_thread() + .enable_all() + .build() + .unwrap(), + ); + } } } |