Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/async_session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ impl AsyncSession {
}
}

/// Receives the next packet. Once [`Session::shutdown`] is called, returns an [`std::io::Error`] that wraps
/// [`crate::Error::ShuttingDown`], see [`Session::recv`]
pub async fn recv(&self, buf: &mut [u8]) -> std::io::Result<usize> {
loop {
match self.session.try_receive() {
Expand Down
19 changes: 16 additions & 3 deletions src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl Session {

/// Blocks until a packet is available, returning the next packet in the receive queue once this happens.
/// If the session is closed via [`Session::shutdown`] all threads currently blocking inside this function
/// will return Err(())
/// will return [`Error::ShuttingDown`]
pub fn receive_blocking(self: &Arc<Self>) -> Result<packet::Packet, Error> {
loop {
//Try 5 times to receive without blocking so we don't have to issue a syscall to wait
Expand All @@ -128,6 +128,7 @@ impl Session {
}
}

/// Blocks until data is available to read, or returns [`Error::ShuttingDown`] once [`Session::shutdown`] is called
pub fn wait_read(&self) -> Result<(), Error> {
//Wait on both the read handle and the shutdown handle so that we stop when requested
let handles = [self.get_read_wait_event()?.0, self.shutdown_event.0.0];
Expand All @@ -154,7 +155,8 @@ impl Session {
}
}

/// Cancels any active calls to [`Session::receive_blocking`] making them instantly return Err(_) so that session can be shutdown cleanly
/// Cancels any active calls to [`Session::receive_blocking`], [`Session::recv`] and [`Session::wait_read`],
/// making them instantly return [`Error::ShuttingDown`] so that session can be shutdown cleanly
pub fn shutdown(&self) -> Result<(), Error> {
self.shutdown_event.set_event()?;
Ok(())
Expand Down Expand Up @@ -189,7 +191,18 @@ impl Session {

/// Blocks until a packet is available, returning the next packet in the receive queue once this happens.
/// If the session is closed via [`Session::shutdown`] all threads currently blocking inside this function
/// will return Err(())
/// will return an [`std::io::Error`] that wraps [`Error::ShuttingDown`]. It can be told apart from other errors:
///
/// ```
/// fn is_shutting_down(err: &std::io::Error) -> bool {
/// matches!(
/// err.get_ref().and_then(|e| e.downcast_ref::<wintun_bindings::Error>()),
/// Some(wintun_bindings::Error::ShuttingDown)
/// )
/// }
/// assert!(is_shutting_down(&wintun_bindings::Error::ShuttingDown.into()));
/// assert!(!is_shutting_down(&std::io::ErrorKind::BrokenPipe.into()));
/// ```
pub fn recv(&self, buf: &mut [u8]) -> std::io::Result<usize> {
loop {
// Try 5 times to receive without blocking so we don't have to issue a syscall to wait
Expand Down
Loading