| @@ -1,4 +1,5 @@ |
| 1 | 1 | use std::{ |
| 2 | cell::RefCell, |
| 2 | 3 | future::Future, |
| 3 | 4 | path::PathBuf, |
| 4 | 5 | rc::{Rc, Weak}, |
| @@ -8,6 +9,7 @@ use std::{ |
| 8 | 9 | use tokio::{ |
| 9 | 10 | runtime::Runtime, |
| 10 | 11 | sync::mpsc::{channel, Receiver, Sender}, |
| 12 | task::JoinHandle, |
| 11 | 13 | }; |
| 12 | 14 | |
| 13 | 15 | use tracing::error; |
| @@ -83,7 +85,7 @@ pub enum ClientMessage { |
| 83 | 85 | bool, |
| 84 | 86 | Option<AmbiguityChange>, |
| 85 | 87 | ), |
| 86 | | RestoredRoom(Room), |
| 88 | RestoredRoom(OwnedRoomId), |
| 87 | 89 | } |
| 88 | 90 | |
| 89 | 91 | /// Struct representing an active connection to the homeserver. |
| @@ -97,8 +99,8 @@ pub enum ClientMessage { |
| 97 | 99 | /// loop drop the object. |
| 98 | 100 | #[derive(Debug, Clone)] |
| 99 | 101 | pub struct Connection { |
| 100 | | #[allow(dead_code)] |
| 101 | | receiver_task: Rc<Task<()>>, |
| 102 | receiver_task: Rc<RefCell<Option<Task<()>>>>, |
| 103 | sync_task: Rc<RefCell<Option<JoinHandle<()>>>>, |
| 102 | 104 | client: Option<Client>, |
| 103 | 105 | runtime: Option<Rc<Runtime>>, |
| 104 | 106 | } |
| @@ -128,6 +130,31 @@ impl Connection { |
| 128 | 130 | .expect("Tokio error while sending a message") |
| 129 | 131 | } |
| 130 | 132 | |
| 133 | pub fn shutdown(&self) { |
| 134 | let runtime = self.runtime(); |
| 135 | |
| 136 | self.stop_sync_task(&runtime); |
| 137 | self.stop_receiver_task(&runtime); |
| 138 | } |
| 139 | |
| 140 | fn stop_sync_task(&self, runtime: &Runtime) { |
| 141 | let Some(task) = self.sync_task.borrow_mut().take() else { |
| 142 | return; |
| 143 | }; |
| 144 | |
| 145 | task.abort(); |
| 146 | let _ = runtime.block_on(async { task.await }); |
| 147 | } |
| 148 | |
| 149 | fn stop_receiver_task(&self, runtime: &Runtime) { |
| 150 | let Some(task) = self.receiver_task.borrow_mut().take() else { |
| 151 | return; |
| 152 | }; |
| 153 | |
| 154 | let _guard = runtime.enter(); |
| 155 | drop(task); |
| 156 | } |
| 157 | |
| 131 | 158 | pub fn new(server: &MatrixServer, client: &Client) -> Self { |
| 132 | 159 | let (tx, rx) = channel(10_000); |
| 133 | 160 | |
| @@ -140,7 +167,7 @@ impl Connection { |
| 140 | 167 | |
| 141 | 168 | let runtime = Runtime::new().unwrap(); |
| 142 | 169 | |
| 143 | | runtime.spawn(Connection::sync_loop( |
| 170 | let sync_task = runtime.spawn(Connection::sync_loop( |
| 144 | 171 | client.clone(), |
| 145 | 172 | tx, |
| 146 | 173 | server.user_name(), |
| @@ -150,9 +177,10 @@ impl Connection { |
| 150 | 177 | )); |
| 151 | 178 | |
| 152 | 179 | Self { |
| 180 | receiver_task: Rc::new(RefCell::new(Some(receiver_task))), |
| 181 | sync_task: Rc::new(RefCell::new(Some(sync_task))), |
| 153 | 182 | client: Some(client.clone()), |
| 154 | 183 | runtime: Some(runtime.into()), |
| 155 | | receiver_task: receiver_task.into(), |
| 156 | 184 | } |
| 157 | 185 | } |
| 158 | 186 | |
| @@ -327,8 +355,8 @@ impl Connection { |
| 327 | 355 | ClientMessage::SyncState(r, e) => { |
| 328 | 356 | server.receive_joined_state_event(&r, e).await |
| 329 | 357 | } |
| 330 | | ClientMessage::RestoredRoom(room) => { |
| 331 | | server.restore_room(room).await |
| 358 | ClientMessage::RestoredRoom(room_id) => { |
| 359 | server.restore_room_by_id(room_id).await |
| 332 | 360 | } |
| 333 | 361 | ClientMessage::ToDeviceEvent(e) => { |
| 334 | 362 | server.receive_to_device_event(e).await |
| @@ -504,7 +532,9 @@ impl Connection { |
| 504 | 532 | if !first_login { |
| 505 | 533 | for room in client.joined_rooms() { |
| 506 | 534 | if channel |
| 507 | | .send(Ok(ClientMessage::RestoredRoom(room))) |
| 535 | .send(Ok(ClientMessage::RestoredRoom( |
| 536 | room.room_id().to_owned(), |
| 537 | ))) |
| 508 | 538 | .await |
| 509 | 539 | .is_err() |
| 510 | 540 | { |
| @@ -672,20 +702,21 @@ impl Connection { |
| 672 | 702 | |
| 673 | 703 | impl Drop for Connection { |
| 674 | 704 | fn drop(&mut self) { |
| 675 | | { |
| 676 | | let runtime = self.runtime(); |
| 705 | let Some(runtime) = self.runtime.as_ref().cloned() else { |
| 706 | return; |
| 707 | }; |
| 677 | 708 | |
| 678 | | if let Some(client) = self.client.take() { |
| 679 | | let _guard = runtime.enter(); |
| 680 | | drop(client); |
| 681 | | } |
| 709 | if Rc::strong_count(&runtime) == 1 { |
| 710 | self.stop_sync_task(&runtime); |
| 711 | self.stop_receiver_task(&runtime); |
| 682 | 712 | } |
| 683 | 713 | |
| 684 | | let Some(runtime) = self.runtime.as_ref() else { |
| 685 | | return; |
| 686 | | }; |
| 714 | if let Some(client) = self.client.take() { |
| 715 | let _guard = runtime.enter(); |
| 716 | drop(client); |
| 717 | } |
| 687 | 718 | |
| 688 | | if Rc::strong_count(runtime) == 1 { |
| 719 | if Rc::strong_count(&runtime) == 1 { |
| 689 | 720 | let runtime = self.runtime.take().expect("runtime disappeared"); |
| 690 | 721 | let handle = runtime.handle().clone(); |
| 691 | 722 | let _guard = handle.enter(); |