Skip to content

Commit

Permalink
Fix mac build
Browse files Browse the repository at this point in the history
  • Loading branch information
Berrysoft committed Sep 21, 2024
1 parent 62e1b43 commit 33b134a
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 26 deletions.
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ serde_json = "1"
serde_with = "3"

objc = "0.2"
core-foundation = "0.10"
core-foundation = "0.9"
system-configuration = "0.6"

windows = "0.58"
Expand Down
11 changes: 5 additions & 6 deletions netstatus/src/sc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use core_foundation::{
base::TCFType,
runloop::{kCFRunLoopDefaultMode, CFRunLoop, CFRunLoopRef},
};
use flume::{r#async::RecvStream, unbounded};
use objc::{
runtime::{Class, Object},
*,
Expand All @@ -16,8 +17,6 @@ use std::{
thread::JoinHandle,
};
use system_configuration::network_reachability::{ReachabilityFlags, SCNetworkReachability};
use tokio::sync::watch;
use tokio_stream::wrappers::WatchStream;

#[link(name = "CoreWLAN", kind = "framework")]
extern "C" {
Expand Down Expand Up @@ -63,7 +62,7 @@ pub fn current() -> NetStatus {
}

pub fn watch() -> impl Stream<Item = ()> {
let (tx, rx) = watch::channel(());
let (tx, rx) = unbounded();
let loop_thread = std::thread::spawn(move || {
let mut sc =
SCNetworkReachability::from(SocketAddr::new(IpAddr::V4(Ipv4Addr::UNSPECIFIED), 0));
Expand All @@ -76,7 +75,7 @@ pub fn watch() -> impl Stream<Item = ()> {
CFRunLoop::run_current();
});
StatusWatchStream {
s: WatchStream::new(rx),
rx: rx.into_stream(),
thread: CFJThread {
handle: Some(loop_thread),
},
Expand All @@ -86,15 +85,15 @@ pub fn watch() -> impl Stream<Item = ()> {
#[pin_project]
struct StatusWatchStream {
#[pin]
s: WatchStream<()>,
rx: RecvStream<'static, ()>,
thread: CFJThread,
}

impl Stream for StatusWatchStream {
type Item = ();

fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
self.project().s.poll_next(cx)
self.project().rx.poll_next(cx)
}
}

Expand Down
1 change: 1 addition & 0 deletions tunet-service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ runas = "1"
winlog2 = "0.3"

[target.'cfg(target_os = "macos")'.dependencies]
libc = "0.2"
dirs = { workspace = true }
serde = { workspace = true, features = ["derive"] }
plist = "1"
Expand Down
24 changes: 13 additions & 11 deletions tunet-service/src/mac/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use anyhow::{anyhow, Result};
use compio::signal::unix::signal;
use futures_util::{FutureExt, StreamExt};
use serde::Serialize;
use std::path::PathBuf;
use tokio::signal::unix::{signal, SignalKind};
use tokio_stream::StreamExt;

#[derive(Debug, Serialize)]
#[serde(rename_all = "PascalCase")]
Expand Down Expand Up @@ -63,23 +63,25 @@ pub fn unregister() -> Result<()> {

pub fn start(interval: Option<humantime::Duration>) -> Result<()> {
env_logger::try_init()?;
tokio::runtime::Builder::new_current_thread()
.enable_all()
compio::runtime::RuntimeBuilder::new()
.build()?
.block_on(start_impl(interval))
}

async fn start_impl(interval: Option<humantime::Duration>) -> Result<()> {
let mut ctrlc = signal(SignalKind::interrupt())?;
let mut kill = signal(SignalKind::terminate())?;
let mut timer = crate::create_timer(interval);
let mut events = netstatus::NetStatus::watch();
let mut timer = crate::create_timer(interval).fuse();
let mut timer = std::pin::pin!(timer);
let mut events = netstatus::NetStatus::watch().fuse();
loop {
tokio::select! {
_ = ctrlc.recv() => {
let mut ctrlc = signal(libc::SIGINT);
let ctrlc = std::pin::pin!(ctrlc);
let mut kill = signal(libc::SIGTERM);
let kill = std::pin::pin!(kill);
futures_util::select! {
_ = ctrlc.fuse() => {
break;
}
_ = kill.recv() => {
_ = kill.fuse() => {
break;
}
_ = timer.next() => {
Expand Down
16 changes: 9 additions & 7 deletions tunet-service/src/stub/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::{anyhow, Result};
use tokio::signal::ctrl_c;
use tokio_stream::StreamExt;
use compio::signal::ctrl_c;
use futures_util::{FutureExt, StreamExt};

pub fn register(_interval: Option<humantime::Duration>) -> Result<()> {
Err(anyhow!("不支持的命令"))
Expand All @@ -12,17 +12,19 @@ pub fn unregister() -> Result<()> {

pub fn start(interval: Option<humantime::Duration>) -> Result<()> {
env_logger::try_init()?;
tokio::runtime::Builder::new_current_thread()
.enable_all()
compio::runtime::RuntimeBuilder::new()
.build()?
.block_on(start_impl(interval))
}

async fn start_impl(interval: Option<humantime::Duration>) -> Result<()> {
let mut timer = crate::create_timer(interval);
let mut timer = crate::create_timer(interval).fuse();
let mut timer = std::pin::pin!(timer);
loop {
tokio::select! {
_ = ctrl_c() => {
let mut ctrlc = ctrl_c();
let ctrlc = std::pin::pin!(ctrlc);
futures_util::select! {
_ = ctrlc.fuse() => {
break;
}
_ = timer.next() => {
Expand Down

0 comments on commit 33b134a

Please sign in to comment.