1
2
Fork 0
mirror of https://github.com/mat-1/azalea.git synced 2025-08-02 14:26:04 +00:00

fix another panic on disconnect and slightly optimize client events loop

This commit is contained in:
mat 2025-04-18 00:32:31 +02:00
parent 3f60bdadac
commit 43d7c428e3
2 changed files with 15 additions and 5 deletions

View file

@ -126,7 +126,11 @@ fn poll_all_writer_tasks(mut conn_query: Query<&mut RawConnection>) {
// this needs to be done at some point every update to make sure packets are // this needs to be done at some point every update to make sure packets are
// actually sent to the network // actually sent to the network
net_conn.poll_writer(); if net_conn.poll_writer().is_some() {
// means the writer task ended
conn.network = None;
conn.is_alive = false;
}
} }
} }
} }
@ -319,9 +323,11 @@ impl NetworkConnection {
Ok(()) Ok(())
} }
pub fn poll_writer(&mut self) { /// Makes sure packets get sent and returns Some(()) if the connection has
/// closed.
pub fn poll_writer(&mut self) -> Option<()> {
let poll_once_res = future::poll_once(&mut self.writer_task); let poll_once_res = future::poll_once(&mut self.writer_task);
future::block_on(poll_once_res); future::block_on(poll_once_res)
} }
pub fn set_compression_threshold(&mut self, threshold: Option<u32>) { pub fn set_compression_threshold(&mut self, threshold: Option<u32>) {
@ -348,6 +354,7 @@ async fn write_task(
break; break;
}; };
} }
trace!("write task is done"); trace!("write task is done");
} }

View file

@ -485,7 +485,8 @@ where
if let Some(handler) = &self.handler { if let Some(handler) = &self.handler {
let ecs_mutex = first_bot.ecs.clone(); let ecs_mutex = first_bot.ecs.clone();
let mut ecs = ecs_mutex.lock(); let mut ecs = ecs_mutex.lock();
let Some(first_bot_state) = first_bot.query::<Option<&S>>(&mut ecs).cloned() else { let mut query = ecs.query::<Option<&S>>();
let Ok(Some(first_bot_state)) = query.get(&mut ecs, first_bot.entity) else {
error!( error!(
"the first bot ({} / {}) is missing the required state component! none of the client handler functions will be called.", "the first bot ({} / {}) is missing the required state component! none of the client handler functions will be called.",
first_bot.username(), first_bot.username(),
@ -494,6 +495,7 @@ where
continue; continue;
}; };
let first_bot_entity = first_bot.entity; let first_bot_entity = first_bot.entity;
let first_bot_state = first_bot_state.clone();
tokio::spawn((handler)(first_bot, first_event, first_bot_state.clone())); tokio::spawn((handler)(first_bot, first_event, first_bot_state.clone()));
@ -504,7 +506,7 @@ where
let state = match states.entry(bot.entity) { let state = match states.entry(bot.entity) {
hash_map::Entry::Occupied(e) => e.into_mut(), hash_map::Entry::Occupied(e) => e.into_mut(),
hash_map::Entry::Vacant(e) => { hash_map::Entry::Vacant(e) => {
let Some(state) = bot.query::<Option<&S>>(&mut ecs).cloned() else { let Ok(Some(state)) = query.get(&mut ecs, bot.entity) else {
error!( error!(
"one of our bots ({} / {}) is missing the required state component! its client handler function will not be called.", "one of our bots ({} / {}) is missing the required state component! its client handler function will not be called.",
bot.username(), bot.username(),
@ -512,6 +514,7 @@ where
); );
continue; continue;
}; };
let state = state.clone();
e.insert(state) e.insert(state)
} }
}; };