Skip to content

Commit

Permalink
remove Clone from Session
Browse files Browse the repository at this point in the history
  • Loading branch information
ssrlive committed Sep 5, 2024
1 parent 70e4b2d commit 091d77e
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 14 deletions.
6 changes: 3 additions & 3 deletions src/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use crate::{
error::{Error, OutOfRangeData},
handle::{SafeEvent, UnsafeHandle},
session,
session::Session,
util::{self},
wintun_raw, Wintun,
};
Expand Down Expand Up @@ -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<Self>, capacity: u32) -> Result<Arc<session::Session>, Error> {
pub fn start_session(self: &Arc<Self>, capacity: u32) -> Result<Arc<Session>, Error> {
Self::validate_capacity(capacity)?;

let result = unsafe { self.wintun.WintunStartSession(self.adapter.0, capacity) };
Expand All @@ -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),
Expand Down
10 changes: 5 additions & 5 deletions src/async_session.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -24,20 +24,20 @@ enum ReadState {

#[derive(Clone)]
pub struct AsyncSession {
session: Arc<crate::session::Session>,
session: Arc<Session>,
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<Arc<crate::session::Session>> for AsyncSession {
fn from(session: Arc<crate::session::Session>) -> Self {
impl From<Arc<Session>> for AsyncSession {
fn from(session: Arc<Session>) -> Self {
Self {
session,
read_state: ReadState::Idle,
Expand Down
4 changes: 2 additions & 2 deletions src/packet.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::session;
use crate::session::Session;
use std::sync::Arc;

pub(crate) enum Kind {
Expand Down Expand Up @@ -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<session::Session>,
pub(crate) session: Arc<Session>,
}

impl Packet {
Expand Down
6 changes: 2 additions & 4 deletions src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use windows_sys::Win32::{
};

/// Wrapper around a <https://git.zx2c4.com/wintun/about/#wintun_session_handle>
#[derive(Clone)]
pub struct Session {
/// The session handle given to us by WintunStartSession
pub(crate) session: UnsafeHandle<wintun_raw::WINTUN_SESSION_HANDLE>,
Expand Down Expand Up @@ -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();
}
Expand Down

0 comments on commit 091d77e

Please sign in to comment.