From 6f81bb7723742aae38fb4b4aa97293d79688c572 Mon Sep 17 00:00:00 2001 From: ssrlive <30760636+ssrlive@users.noreply.github.com> Date: Sat, 31 Aug 2024 10:00:01 +0800 Subject: [PATCH] 'start_session' return Arc now --- README.md | 4 ++-- examples/basic.rs | 7 ++----- examples/udp-echo-async.rs | 7 ++----- examples/udp-echo.rs | 3 +-- examples/wireshark.rs | 7 ++----- src/adapter.rs | 6 +++--- src/lib.rs | 2 +- 7 files changed, 13 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 7588b9b..ff89dfe 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ let adapter = match wintun_bindings::Adapter::open(&wintun, "Demo") { } }; //Specify the size of the ring buffer the wintun driver should use. -let session = Arc::new(adapter.start_session(wintun_bindings::MAX_RING_CAPACITY).unwrap()); +let session = adapter.start_session(wintun_bindings::MAX_RING_CAPACITY).unwrap(); //Get a 20 byte packet from the ring buffer let mut packet = session.allocate_send_packet(20).unwrap(); @@ -87,7 +87,7 @@ wintun's internal ring buffer. And simply transform your `Session` into an `AsyncSession`: ```rust // ... - let session = Arc::new(adapter.start_session(MAX_RING_CAPACITY)?); + let session = adapter.start_session(MAX_RING_CAPACITY)?; let mut reader_session = AsyncSession::from(session.clone()); let mut writer_session: AsyncSession = session.clone().into(); // ... diff --git a/examples/basic.rs b/examples/basic.rs index 3e01573..d56357f 100644 --- a/examples/basic.rs +++ b/examples/basic.rs @@ -1,7 +1,4 @@ -use std::sync::{ - atomic::{AtomicBool, Ordering}, - Arc, -}; +use std::sync::atomic::{AtomicBool, Ordering}; static RUNNING: AtomicBool = AtomicBool::new(true); @@ -22,7 +19,7 @@ fn main() -> Result<(), wintun_bindings::BoxError> { let version = wintun_bindings::get_running_driver_version(&wintun)?; log::info!("Using wintun version: {:?}", version); - let session = Arc::new(adapter.start_session(wintun_bindings::MAX_RING_CAPACITY)?); + let session = adapter.start_session(wintun_bindings::MAX_RING_CAPACITY)?; let reader_session = session.clone(); let reader = std::thread::spawn(move || { diff --git a/examples/udp-echo-async.rs b/examples/udp-echo-async.rs index f89b187..21ca11b 100644 --- a/examples/udp-echo-async.rs +++ b/examples/udp-echo-async.rs @@ -6,10 +6,7 @@ use futures::{AsyncReadExt, AsyncWriteExt}; use std::{ net::{IpAddr, SocketAddr}, - sync::{ - atomic::{AtomicBool, Ordering}, - Arc, - }, + sync::atomic::{AtomicBool, Ordering}, }; use tokio::sync::mpsc::channel; use windows_sys::Win32::{ @@ -119,7 +116,7 @@ async fn main() -> Result<(), BoxError> { get_active_network_interface_gateways()? ); - let session = Arc::new(adapter.start_session(MAX_RING_CAPACITY)?); + let session = adapter.start_session(MAX_RING_CAPACITY)?; let mut reader_session = AsyncSession::from(session.clone()); let mut writer_session: AsyncSession = session.clone().into(); diff --git a/examples/udp-echo.rs b/examples/udp-echo.rs index 4ba6661..2e290c4 100644 --- a/examples/udp-echo.rs +++ b/examples/udp-echo.rs @@ -8,7 +8,6 @@ use std::{ sync::{ atomic::{AtomicBool, Ordering}, mpsc::channel, - Arc, }, }; use windows_sys::Win32::{ @@ -117,7 +116,7 @@ fn main() -> Result<(), BoxError> { get_active_network_interface_gateways()? ); - let session = Arc::new(adapter.start_session(MAX_RING_CAPACITY)?); + let session = adapter.start_session(MAX_RING_CAPACITY)?; let reader_session = session.clone(); let writer_session = session.clone(); diff --git a/examples/wireshark.rs b/examples/wireshark.rs index 15dc094..1b2fa7e 100644 --- a/examples/wireshark.rs +++ b/examples/wireshark.rs @@ -6,10 +6,7 @@ use packet::Builder; use std::{ fs::File, net::IpAddr, - sync::{ - atomic::{AtomicBool, Ordering}, - Arc, - }, + sync::atomic::{AtomicBool, Ordering}, time::{SystemTime, UNIX_EPOCH}, }; use subprocess::{Popen, PopenConfig, Redirection}; @@ -181,7 +178,7 @@ fn main() -> Result<(), BoxError> { endianness: pcap_file::Endianness::Little, }; let mut writer = pcap_file::pcap::PcapWriter::with_header(file, header)?; - let main_session = Arc::new(adapter.start_session(wintun_bindings::MAX_RING_CAPACITY)?); + let main_session = adapter.start_session(wintun_bindings::MAX_RING_CAPACITY)?; let reader_session = main_session.clone(); let writer_session = main_session.clone(); diff --git a/src/adapter.rs b/src/adapter.rs index 4cb83fe..81d6786 100644 --- a/src/adapter.rs +++ b/src/adapter.rs @@ -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 { + 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,12 +156,12 @@ 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(session::Session { + Ok(Arc::new(session::Session { session: UnsafeHandle(result), read_event: OnceLock::new(), shutdown_event: Arc::new(shutdown_event), adapter: self.clone(), - }) + })) } /// Returns the Win32 LUID for this adapter diff --git a/src/lib.rs b/src/lib.rs index f24c364..528967f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -31,7 +31,7 @@ //! } //! }; //! //Specify the size of the ring buffer the wintun driver should use. -//! let session = Arc::new(adapter.start_session(wintun_bindings::MAX_RING_CAPACITY).unwrap()); +//! let session = adapter.start_session(wintun_bindings::MAX_RING_CAPACITY).unwrap(); //! //! //Get a 20 byte packet from the ring buffer //! let mut packet = session.allocate_send_packet(20).unwrap();