cloud_filter/root/
connect.rs1use std::{
2 sync::{mpsc::Sender, Arc},
3 thread::{self, JoinHandle},
4 time::Duration,
5};
6
7use windows::Win32::Storage::CloudFilters::{CfDisconnectSyncRoot, CF_CONNECTION_KEY};
8
9use crate::filter::{Callbacks, RawConnectionKey};
10
11#[derive(Debug)]
17pub struct Connection<F> {
18 connection_key: RawConnectionKey,
19
20 cancel_token: Sender<()>,
21 join_handle: JoinHandle<()>,
22
23 _callbacks: Callbacks,
24 filter: Arc<F>,
25}
26
27impl<T> Connection<T> {
30 pub(crate) fn new(
31 connection_key: RawConnectionKey,
32 cancel_token: Sender<()>,
33 join_handle: JoinHandle<()>,
34 callbacks: Callbacks,
35 filter: Arc<T>,
36 ) -> Self {
37 Self {
38 connection_key,
39 cancel_token,
40 join_handle,
41 _callbacks: callbacks,
42 filter,
43 }
44 }
45
46 pub fn connection_key(&self) -> RawConnectionKey {
48 self.connection_key
49 }
50
51 pub fn filter(&self) -> &T {
53 &self.filter
54 }
55}
56
57impl<T> Drop for Connection<T> {
58 fn drop(&mut self) {
59 unsafe { CfDisconnectSyncRoot(CF_CONNECTION_KEY(self.connection_key)) }.unwrap();
60
61 _ = self.cancel_token.send(());
62 while !self.join_handle.is_finished() {
63 thread::sleep(Duration::from_millis(150));
64 }
65 }
66}