#[cfg(target_os = "macos")]
mod macos;
#[cfg(target_os = "linux")]
pub(super) mod linux;
#[cfg(any(target_os = "windows", test))]
pub(super) mod windows;
#[cfg(target_os = "macos")]
use super::refresh_trigger::{self, RefreshTriggerEvent, RefreshTriggerReceiver};
#[derive(Debug)]
pub(crate) struct PlatformNetworkMonitor {
#[cfg(target_os = "macos")]
_native: Option<macos::NativeNetworkMonitor>,
#[cfg(target_os = "macos")]
changes: RefreshTriggerReceiver,
}
impl PlatformNetworkMonitor {
#[cfg(target_os = "macos")]
pub(crate) fn start() -> Self {
let (trigger, changes) = refresh_trigger::channel();
let native = macos::NativeNetworkMonitor::start(trigger);
Self::from_native(native, changes)
}
#[cfg(target_os = "macos")]
fn from_native(
native: std::io::Result<macos::NativeNetworkMonitor>,
changes: RefreshTriggerReceiver,
) -> Self {
let native = match native {
Ok(native) => Some(native),
Err(error) => {
crate::log_e!(crate::common::log::log_def::LogType::Engine;
"net_status.from_native", "native_error_kind|changes",
format!("{:?}", error.kind()), "provided");
None
}
};
Self {
_native: native,
changes,
}
}
#[cfg(not(target_os = "macos"))]
pub(crate) fn start() -> Self {
Self {}
}
#[cfg(target_os = "macos")]
pub(crate) async fn changed(&mut self) -> bool {
matches!(self.changes.recv().await, RefreshTriggerEvent::Notified)
}
#[cfg(not(target_os = "macos"))]
pub(crate) async fn changed(&mut self) -> bool {
std::future::pending().await
}
}
#[cfg(all(test, not(target_os = "macos")))]
mod tests {
use super::PlatformNetworkMonitor;
#[tokio::test]
async fn non_macos_monitor_never_manufactures_a_change() {
let mut monitor = PlatformNetworkMonitor::start();
tokio::select! {
biased;
_ = monitor.changed() => panic!("non-macOS platform monitor must remain pending"),
_ = std::future::ready(()) => {}
}
}
}
#[cfg(all(test, target_os = "macos"))]
#[path = "platform_tests.rs"]
mod platform_tests;