调整安卓端tun逻辑

This commit is contained in:
lbl8603
2024-05-09 22:51:27 +08:00
parent 1338bcbbc5
commit f6cfa1b84c
2 changed files with 9 additions and 13 deletions
+1 -2
View File
@@ -320,8 +320,7 @@ impl<Call: VntCallback> ServerPacketHandler<Call> {
"device_fd == 0".into(),
));
} else {
let device = Arc::new(tun::Device::new(device_fd as _)?);
if let Err(e) = self.device.start(device) {
if let Err(e) = self.device.start(device_fd as _) {
self.callback.error(ErrorInfo::new_msg(
ErrorType::Unknown,
format!("{:?}", e),
+8 -11
View File
@@ -26,7 +26,7 @@ impl DeviceAdapter {
#[cfg(target_os = "android")]
pub fn new(tun_device_helper: TunDeviceHelper) -> Self {
Self {
tun: Arc::new(Mutex::new(None)),
tun: Arc::new(AtomicCell::new(-1 as _)),
tun_device_helper,
}
}
@@ -43,22 +43,19 @@ impl std::ops::Deref for DeviceAdapter {
#[cfg(target_os = "android")]
#[derive(Clone)]
pub struct DeviceAdapter {
tun: Arc<Mutex<Option<Arc<Device>>>>,
tun: Arc<AtomicCell<std::os::fd::RawFd>>,
tun_device_helper: TunDeviceHelper,
}
#[cfg(target_os = "android")]
impl DeviceAdapter {
pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
if let Some(device) = self.tun.lock().as_ref() {
use tun::device::IFace;
device.write(buf)
} else {
Err(io::Error::new(io::ErrorKind::Other, "not tun device"))
}
let fd = self.tun.load();
tun::Fd(fd).write(buf)
}
pub fn start(&self, device: Arc<Device>) -> io::Result<()> {
self.tun_device_helper.start(device.clone())?;
self.tun.lock().replace(device);
pub fn start(&self, fd: std::os::fd::RawFd) -> io::Result<()> {
//安卓端fd是由外部释放的,所以这里这么搞免得加锁
self.tun_device_helper.start(Arc::new(Device::new(fd)?))?;
self.tun.store(fd);
Ok(())
}
}