closing tui no longer crashes + onShutdown event

This commit is contained in:
Luuk van Oijen
2023-11-22 11:02:53 +01:00
parent 669e061fdf
commit 6ed714b863
7 changed files with 39 additions and 33 deletions

View File

@@ -212,14 +212,14 @@ impl Client {
Some(v) => "/".to_string() + v.to_str().unwrap(),
None => {
error!("Client requests invalid mod. Disconnecting");
self.kick("Invalid mod request");
self.kick("Invalid mod request").await;
return Ok(());
}, // client requested path (fResources/Client/) or nothing at all (f) - invalid
};
let mod_path = format!("Resources/Client{mod_name}");
if !std::path::Path::new(&mod_path).exists() {
error!("Client requests inexistent mod. Disconnecting");
self.kick("Invalid mod request");
self.kick("Invalid mod request").await;
return Ok(()) // client requested mod that doesnt exists within "Resources/Client/*"
}

View File

@@ -327,29 +327,18 @@ impl Server {
}
}
pub async fn process_tcp(&mut self, index: usize, raw_packet: RawPacket) -> anyhow::Result<()> {
// if self.clients.len() > 0 {
// let (result, index, _) = futures::future::select_all(
// self.clients.iter_mut().map(|client| Box::pin(client.process_blocking()))
// ).await;
//
// match result {
// Ok(packet_opt) => {
// if let Some(raw_packet) = packet_opt {
// self.parse_packet(index, raw_packet).await?;
// }
// },
// Err(e) => {
// if let Some(client) = self.clients.get_mut(index) {
// client.kick(&format!("Kicked: {:?}", e)).await;
// }
// }
// }
// } else {
// // TODO: Find a better solution than this lol
// tokio::time::sleep(tokio::time::Duration::from_millis(20)).await;
// }
pub async fn close(mut self) {
self.connect_runtime_handle.abort();
for mut client in self.clients.drain(..) {
client.kick("Server is closing!").await;
}
// TODO: We can probably race these with futures::future::select_all?
for plugin in self.plugins.drain(..) {
plugin.close().await;
}
}
pub async fn process_tcp(&mut self, index: usize, raw_packet: RawPacket) -> anyhow::Result<()> {
self.parse_packet(index, raw_packet).await?;
Ok(())

View File

@@ -137,6 +137,7 @@ impl Backend for BackendLua {
fn call_event_handler(&mut self, event: ScriptEvent, resp: Option<oneshot::Sender<Argument>>) {
let (event_name, args) = match event {
ScriptEvent::OnPluginLoaded => ("onInit", vec![]),
ScriptEvent::OnShutdown => ("onShutdown", vec![]),
ScriptEvent::OnPlayerAuthenticated { name, role, is_guest, identifiers } => ("onPlayerAuth", vec![Argument::String(name), Argument::String(role), Argument::Boolean(is_guest), Argument::Table(identifiers.to_map())]),
ScriptEvent::OnPlayerDisconnect { pid, name } => ("onPlayerDisconnect", vec![Argument::Number(pid as f32), Argument::String(name)]),
};

View File

@@ -45,6 +45,7 @@ impl PlayerIdentifiers {
#[derive(Debug)]
pub enum ScriptEvent {
OnPluginLoaded,
OnShutdown,
OnPlayerAuthenticated { name: String, role: String, is_guest: bool, identifiers: PlayerIdentifiers },
@@ -114,6 +115,13 @@ impl Plugin {
})
}
pub async fn close(mut self) {
let (tx, mut rx) = oneshot::channel();
self.send_event(PluginBoundPluginEvent::CallEventHandler((ScriptEvent::OnShutdown, Some(tx)))).await;
let _ = rx.await; // We just wait for it to finish shutting down
self.runtime.shutdown_background();
}
// TODO: For performance I think we can turn this into an iterator instead of first allocating
// a full vector?
pub fn get_events(&mut self) -> Vec<ServerBoundPluginEvent> {