From 091d77e0f6b1955993c2e8fc6150a36e61d2bcb3 Mon Sep 17 00:00:00 2001 From: ssrlive <30760636+ssrlive@users.noreply.github.com> Date: Thu, 5 Sep 2024 09:56:40 +0800 Subject: [PATCH] remove Clone from Session --- src/adapter.rs | 6 +++--- src/async_session.rs | 10 +++++----- src/packet.rs | 4 ++-- src/session.rs | 6 ++---- 4 files changed, 12 insertions(+), 14 deletions(-) diff --git a/src/adapter.rs b/src/adapter.rs index 81d6786..e86b6b5 100644 --- a/src/adapter.rs +++ b/src/adapter.rs @@ -6,7 +6,7 @@ use crate::{ error::{Error, OutOfRangeData}, handle::{SafeEvent, UnsafeHandle}, - session, + session::Session, util::{self}, wintun_raw, Wintun, }; @@ -146,7 +146,7 @@ impl Adapter { /// /// Capacity is the size in bytes of the ring buffer used internally by the driver. Must be /// a power of two between [`crate::MIN_RING_CAPACITY`] and [`crate::MAX_RING_CAPACITY`] inclusive. - pub fn start_session(self: &Arc, capacity: u32) -> Result, Error> { + pub fn start_session(self: &Arc, capacity: u32) -> Result, Error> { Self::validate_capacity(capacity)?; let result = unsafe { self.wintun.WintunStartSession(self.adapter.0, capacity) }; @@ -156,7 +156,7 @@ impl Adapter { } // Manual reset, because we use this event once and it must fire on all threads let shutdown_event = SafeEvent::new(true, false)?; - Ok(Arc::new(session::Session { + Ok(Arc::new(Session { session: UnsafeHandle(result), read_event: OnceLock::new(), shutdown_event: Arc::new(shutdown_event), diff --git a/src/async_session.rs b/src/async_session.rs index 4487e8c..be8770c 100644 --- a/src/async_session.rs +++ b/src/async_session.rs @@ -1,4 +1,4 @@ -use crate::handle::UnsafeHandle; +use crate::{handle::UnsafeHandle, session::Session}; use futures::{AsyncRead, AsyncWrite}; use std::future::Future; use std::pin::Pin; @@ -24,20 +24,20 @@ enum ReadState { #[derive(Clone)] pub struct AsyncSession { - session: Arc, + session: Arc, read_state: ReadState, } impl std::ops::Deref for AsyncSession { - type Target = crate::session::Session; + type Target = Session; fn deref(&self) -> &Self::Target { &self.session } } -impl From> for AsyncSession { - fn from(session: Arc) -> Self { +impl From> for AsyncSession { + fn from(session: Arc) -> Self { Self { session, read_state: ReadState::Idle, diff --git a/src/packet.rs b/src/packet.rs index 2b8641f..44029f9 100644 --- a/src/packet.rs +++ b/src/packet.rs @@ -1,4 +1,4 @@ -use crate::session; +use crate::session::Session; use std::sync::Arc; pub(crate) enum Kind { @@ -38,7 +38,7 @@ pub struct Packet { /// Share ownership of session to prevent the session from being dropped before packets that /// belong to it - pub(crate) session: Arc, + pub(crate) session: Arc, } impl Packet { diff --git a/src/session.rs b/src/session.rs index 1c123f7..4e0b5a4 100644 --- a/src/session.rs +++ b/src/session.rs @@ -9,7 +9,6 @@ use windows_sys::Win32::{ }; /// Wrapper around a -#[derive(Clone)] pub struct Session { /// The session handle given to us by WintunStartSession pub(crate) session: UnsafeHandle, @@ -155,10 +154,9 @@ impl Session { impl Drop for Session { fn drop(&mut self) { - if let Err(err) = self.shutdown_event.close_handle() { - log::error!("Failed to close handle of shutdown event: {:?}", err); + if let Err(e) = self.shutdown() { + log::trace!("Failed to shutdown session: {}", e); } - unsafe { self.get_wintun().WintunEndSession(self.session.0) }; self.session.0 = ptr::null_mut(); }