-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
76 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,18 @@ | ||
mod side_a; | ||
mod side_b; | ||
|
||
fn main() { | ||
todo!() | ||
#[cfg(windows)] | ||
type Handle = std::os::windows::io::OwnedHandle; | ||
#[cfg(unix)] | ||
type Handle = std::os::unix::io::OwnedFd; | ||
|
||
use std::{io, sync::mpsc, thread}; | ||
|
||
fn main() -> io::Result<()> { | ||
let (htx, hrx) = mpsc::sync_channel(1); | ||
let jh = thread::spawn(move || side_a::main(htx)); | ||
let handle = hrx.recv().unwrap(); | ||
|
||
side_b::main(handle)?; | ||
jh.join().unwrap() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
//{ | ||
use std::{io, sync::mpsc}; | ||
pub(super) fn main(handle_sender: mpsc::SyncSender<super::Handle>) -> io::Result<()> { | ||
//} | ||
use interprocess::unnamed_pipe::pipe; | ||
use std::io::{prelude::*, 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 `From<unnamed_pipe::Sender>`. | ||
let txh = tx.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)?; | ||
|
||
assert_eq!(buf.trim(), "Hello from side B!"); | ||
//{ | ||
Ok(()) | ||
} //} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
//{ | ||
pub(super) fn main(handle: super::Handle) -> std::io::Result<()> { | ||
//} | ||
use interprocess::unnamed_pipe; | ||
use std::io::prelude::*; | ||
|
||
// `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::from()`/`OwnedFd::from()` thanks to handle inheritance. | ||
let mut tx = unnamed_pipe::Sender::from(handle); | ||
|
||
// Send our message to the other side. | ||
tx.write_all(b"Hello from side B!\n")?; | ||
//{ | ||
Ok(()) | ||
} //} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters