增加超时方法

This commit is contained in:
lbl8603
2024-05-08 23:58:38 +08:00
parent be767ae300
commit b57a38e81e
5 changed files with 43 additions and 4 deletions
+7 -1
View File
@@ -11,7 +11,7 @@ import java.io.IOException;
public class Vnt implements Closeable { public class Vnt implements Closeable {
private final long raw; private final long raw;
public Vnt(Config config, CallBack callBack) throws Exception{ public Vnt(Config config, CallBack callBack) throws Exception {
this.raw = new0(config, callBack); this.raw = new0(config, callBack);
if (this.raw == 0) { if (this.raw == 0) {
throw new RuntimeException(); throw new RuntimeException();
@@ -26,6 +26,10 @@ public class Vnt implements Closeable {
wait0(raw); wait0(raw);
} }
public boolean awaitTimeout(long ms) {
return waitTimeout0(raw, ms);
}
public PeerRouteInfo[] list() { public PeerRouteInfo[] list() {
return list0(raw); return list0(raw);
} }
@@ -36,6 +40,8 @@ public class Vnt implements Closeable {
private native void wait0(long raw); private native void wait0(long raw);
private native boolean waitTimeout0(long raw, long ms);
private native void drop0(long raw); private native void drop0(long raw);
private native PeerRouteInfo[] list0(long raw); private native PeerRouteInfo[] list0(long raw);
+12 -1
View File
@@ -1,8 +1,9 @@
use std::ptr; use std::ptr;
use std::time::Duration;
use jni::errors::Error; use jni::errors::Error;
use jni::objects::{JClass, JObject, JValue}; use jni::objects::{JClass, JObject, JValue};
use jni::sys::{jint, jlong, jobject, jobjectArray, jsize}; use jni::sys::{jboolean, jint, jlong, jobject, jobjectArray, jsize};
use jni::JNIEnv; use jni::JNIEnv;
use vnt::channel::Route; use vnt::channel::Route;
@@ -74,6 +75,16 @@ pub unsafe extern "C" fn Java_top_wherewego_vnt_jni_Vnt_wait0(
let vnt = raw_vnt as *mut Vnt; let vnt = raw_vnt as *mut Vnt;
let _ = (&*vnt).wait(); let _ = (&*vnt).wait();
} }
#[no_mangle]
pub unsafe extern "C" fn Java_top_wherewego_vnt_jni_Vnt_waitTimeout0(
_env: JNIEnv,
_class: JClass,
raw_vnt: jlong,
time: jlong,
) -> jboolean {
let vnt = raw_vnt as *mut Vnt;
(&*vnt).wait_timeout(Duration::from_millis(time as _)) as _
}
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn Java_top_wherewego_vnt_jni_Vnt_drop0( pub unsafe extern "C" fn Java_top_wherewego_vnt_jni_Vnt_drop0(
+3
View File
@@ -412,4 +412,7 @@ impl Vnt {
pub fn wait(&self) { pub fn wait(&self) {
self.stop_manager.wait() self.stop_manager.wait()
} }
pub fn wait_timeout(&self, dur: Duration) -> bool {
self.stop_manager.wait_timeout(dur)
}
} }
+5 -2
View File
@@ -86,15 +86,16 @@ pub fn start(
mut up_counter: SingleU64Adder, mut up_counter: SingleU64Adder,
device_list: Arc<Mutex<(u16, Vec<PeerDeviceInfo>)>>, device_list: Arc<Mutex<(u16, Vec<PeerDeviceInfo>)>>,
) -> io::Result<()> { ) -> io::Result<()> {
#[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))]
let worker = { let worker = {
#[cfg(any(target_os = "macos", target_os = "android"))] #[cfg(target_os = "macos")]
let current_device = current_device.clone(); let current_device = current_device.clone();
let device = device.clone(); let device = device.clone();
stop_manager.add_listener("tun_device".into(), move || { stop_manager.add_listener("tun_device".into(), move || {
if let Err(e) = device.shutdown() { if let Err(e) = device.shutdown() {
log::warn!("{:?}", e); log::warn!("{:?}", e);
} }
#[cfg(any(target_os = "macos", target_os = "android"))] #[cfg(target_os = "macos")]
{ {
let ip = current_device.load().virtual_ip; let ip = current_device.load().virtual_ip;
if let Ok(udp) = std::net::UdpSocket::bind("0.0.0.0:0") { if let Ok(udp) = std::net::UdpSocket::bind("0.0.0.0:0") {
@@ -150,6 +151,7 @@ pub fn start(
if let Err(e) = start_multi(stop_manager, device, sender, &mut up_counter) { if let Err(e) = start_multi(stop_manager, device, sender, &mut up_counter) {
log::warn!("stop:{}", e); log::warn!("stop:{}", e);
} }
#[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))]
worker.stop_all(); worker.stop_all();
})?; })?;
} else { } else {
@@ -171,6 +173,7 @@ pub fn start(
) { ) {
log::warn!("stop:{}", e); log::warn!("stop:{}", e);
} }
#[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))]
worker.stop_all(); worker.stop_all();
})?; })?;
} }
+16
View File
@@ -1,6 +1,7 @@
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::Arc; use std::sync::Arc;
use std::thread::Thread; use std::thread::Thread;
use std::time::Duration;
use std::{io, thread}; use std::{io, thread};
use parking_lot::Mutex; use parking_lot::Mutex;
@@ -31,6 +32,9 @@ impl StopManager {
pub fn wait(&self) { pub fn wait(&self) {
self.inner.wait(); self.inner.wait();
} }
pub fn wait_timeout(&self, dur: Duration) -> bool {
self.inner.wait_timeout(dur)
}
pub fn is_stop(&self) -> bool { pub fn is_stop(&self) -> bool {
self.inner.state.load(Ordering::Acquire) self.inner.state.load(Ordering::Acquire)
} }
@@ -103,6 +107,18 @@ impl StopManagerInner {
thread::park() thread::park()
} }
} }
fn wait_timeout(&self, dur: Duration) -> bool {
{
let mut guard = self.park_threads.lock();
guard.push(thread::current());
drop(guard);
}
if self.worker_num.load(Ordering::Acquire) == 0 {
return true;
}
thread::park_timeout(dur);
self.worker_num.load(Ordering::Acquire) == 0
}
fn stop_call(&self) { fn stop_call(&self) {
if let Some(call) = self.stop_call.lock().take() { if let Some(call) = self.stop_call.lock().take() {
call(); call();