use std::{
sync::{mpsc::Sender, Arc},
thread::{self, JoinHandle},
time::Duration,
};
use windows::Win32::Storage::CloudFilters::{CfDisconnectSyncRoot, CF_CONNECTION_KEY};
use crate::{filter::Callbacks, request::RawConnectionKey};
#[derive(Debug)]
pub struct Connection<F> {
connection_key: RawConnectionKey,
cancel_token: Sender<()>,
join_handle: JoinHandle<()>,
_callbacks: Callbacks,
filter: Arc<F>,
}
impl<T> Connection<T> {
pub(crate) fn new(
connection_key: RawConnectionKey,
cancel_token: Sender<()>,
join_handle: JoinHandle<()>,
callbacks: Callbacks,
filter: Arc<T>,
) -> Self {
Self {
connection_key,
cancel_token,
join_handle,
_callbacks: callbacks,
filter,
}
}
pub fn connection_key(&self) -> RawConnectionKey {
self.connection_key
}
pub fn filter(&self) -> &T {
&self.filter
}
}
impl<T> Drop for Connection<T> {
fn drop(&mut self) {
unsafe { CfDisconnectSyncRoot(CF_CONNECTION_KEY(self.connection_key)) }.unwrap();
_ = self.cancel_token.send(());
while !self.join_handle.is_finished() {
thread::sleep(Duration::from_millis(150));
}
}
}