Skip to content

Commit

Permalink
'start_session' return Arc<Session> now
Browse files Browse the repository at this point in the history
  • Loading branch information
ssrlive committed Aug 31, 2024
1 parent 04be2a9 commit 6f81bb7
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 23 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
// ...
Expand Down
7 changes: 2 additions & 5 deletions examples/basic.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use std::sync::{
atomic::{AtomicBool, Ordering},
Arc,
};
use std::sync::atomic::{AtomicBool, Ordering};

static RUNNING: AtomicBool = AtomicBool::new(true);

Expand All @@ -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 || {
Expand Down
7 changes: 2 additions & 5 deletions examples/udp-echo-async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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();
Expand Down
3 changes: 1 addition & 2 deletions examples/udp-echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use std::{
sync::{
atomic::{AtomicBool, Ordering},
mpsc::channel,
Arc,
},
};
use windows_sys::Win32::{
Expand Down Expand Up @@ -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();

Expand Down
7 changes: 2 additions & 5 deletions examples/wireshark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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();
Expand Down
6 changes: 3 additions & 3 deletions src/adapter.rs
Original file line number Diff line number Diff line change
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<session::Session, Error> {
pub fn start_session(self: &Arc<Self>, capacity: u32) -> Result<Arc<session::Session>, Error> {
Self::validate_capacity(capacity)?;

let result = unsafe { self.wintun.WintunStartSession(self.adapter.0, capacity) };
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 6f81bb7

Please sign in to comment.