46 lines
916 B
Rust
46 lines
916 B
Rust
pub mod tun_handler;
|
|
|
|
#[cfg(unix)]
|
|
mod unix;
|
|
|
|
use crossbeam_utils::atomic::AtomicCell;
|
|
use parking_lot::Mutex;
|
|
use std::sync::Arc;
|
|
#[cfg(unix)]
|
|
pub(crate) use unix::*;
|
|
|
|
#[cfg(target_os = "windows")]
|
|
mod windows;
|
|
|
|
#[cfg(target_os = "windows")]
|
|
pub(crate) use windows::*;
|
|
|
|
/// 仅仅是停止tun,不停止vnt
|
|
#[derive(Clone, Default)]
|
|
pub struct DeviceStop {
|
|
f: Arc<Mutex<Option<Box<dyn FnOnce() -> bool + Send>>>>,
|
|
stopped: Arc<AtomicCell<bool>>,
|
|
}
|
|
|
|
impl DeviceStop {
|
|
pub fn set_stop_fn<F>(&self, f: F)
|
|
where
|
|
F: FnOnce() -> bool + Send + 'static,
|
|
{
|
|
self.f.lock().replace(Box::new(f));
|
|
}
|
|
pub fn stop(&self) -> bool {
|
|
if let Some(f) = self.f.lock().take() {
|
|
f()
|
|
} else {
|
|
false
|
|
}
|
|
}
|
|
pub fn stopped(&self) {
|
|
self.stopped.store(true);
|
|
}
|
|
pub fn is_stop(&self) -> bool {
|
|
self.stopped.load()
|
|
}
|
|
}
|