Skip to content

Commit

Permalink
Tokio unnamed pipe examples
Browse files Browse the repository at this point in the history
  • Loading branch information
kotauskas committed Jun 13, 2024
1 parent 30fa27a commit 9a96e58
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ exclude = [
name = "unnamed_pipe_sync"
path = "examples/unnamed_pipe/sync/main.rs"

[[example]]
name = "unnamed_pipe_tokio"
path = "examples/unnamed_pipe/tokio/main.rs"

[[example]]
name = "local_socket_sync_server"
path = "examples/local_socket/sync/listener.rs"
Expand Down
19 changes: 19 additions & 0 deletions examples/unnamed_pipe/tokio/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#[cfg(feature = "tokio")]
mod side_a;
#[cfg(feature = "tokio")]
mod side_b;

#[cfg(feature = "tokio")]
#[tokio::main]
async fn main() -> std::io::Result<()> {
use tokio::{sync::oneshot, task};

let (htx, hrx) = oneshot::channel();
let jh = task::spawn(side_a::emain(htx));
let handle = hrx.await.unwrap();

side_b::emain(handle).await?;
jh.await.unwrap()
}
#[cfg(not(feature = "tokio"))]
fn main() {}
38 changes: 38 additions & 0 deletions examples/unnamed_pipe/tokio/side_a.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//{
use std::{io, os};
use tokio::sync::oneshot;
#[cfg(windows)]
type Handle = os::windows::io::OwnedHandle;
#[cfg(unix)]
type Handle = os::unix::io::OwnedFd;
pub(crate) async fn emain(handle_sender: oneshot::Sender<Handle>) -> io::Result<()> {
//}
use interprocess::unnamed_pipe::tokio::pipe;
use tokio::io::{AsyncBufReadExt, BufReader};

// Create the unnamed pipe, yielding a sender and a receiver.
let (tx, rx) = pipe()?;

// Let's extract the raw handle or file descriptor of the sender. Note that `OwnedHandle` and
// `OwnedFd` both implement `TryFrom<unnamed_pipe::tokio::Sender>`.
let txh = tx.try_into()?;
// Now deliver `txh` to the child process. This may be done by starting it here with a
// command-line argument or via stdin. This works because child processes inherit handles and
// file descriptors to unnamed pipes. You can also use a different platform-specific way of
// transferring handles or file descriptors across a process boundary.
//{
handle_sender.send(txh).unwrap();
//}

let mut buf = String::with_capacity(128);
// We'd like to receive a line, so buffer our input.
let mut rx = BufReader::new(rx);
// Receive the line from the other process.
rx.read_line(&mut buf).await?;

assert_eq!(buf.trim(), "Hello from side B!");
//{
Ok(())
} //}
#[allow(dead_code)]
fn main() {}
25 changes: 25 additions & 0 deletions examples/unnamed_pipe/tokio/side_b.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//{
use std::{io, os};
#[cfg(windows)]
type Handle = os::windows::io::OwnedHandle;
#[cfg(unix)]
type Handle = os::unix::io::OwnedFd;
pub(crate) async fn emain(handle: Handle) -> io::Result<()> {
//}
use interprocess::unnamed_pipe;
use tokio::io::AsyncWriteExt;

// `handle` here is an `OwnedHandle` or an `OwnedFd` from the standard library. Those
// implement `FromRawHandle` and `FromRawFd` respectively. The actual value can be transferred
// via a command-line parameter since it's numerically equal to the value obtained in the
// parent process via `OwnedHandle::try_from()`/`OwnedFd::try_from()` thanks to handle
// inheritance.
let mut tx = unnamed_pipe::tokio::Sender::try_from(handle)?;

// Send our message to the other side.
tx.write_all(b"Hello from side B!\n").await?;
//{
Ok(())
} //}
#[allow(dead_code)]
fn main() {}

0 comments on commit 9a96e58

Please sign in to comment.