Expand description
Create a new non-blocking unix pipe.
This is a wrapper around unix’s pipe
system call and can be used as
interprocess communication channel.
This channel may be created before forking the process and then one end used in each process, e.g. the parent process has the sending end to send command to the child process.
Deregistering
Both Sender
and Receiver
will deregister themselves when dropped,
iff the file descriptors are not duplicated (via dup(2)
).
Examples
use std::io::{self, Read, Write};
use mio_st::unix::{new_pipe, Sender, Receiver};
use mio_st::event::{Event, Events, EventedId, Ready};
use mio_st::poll::{Poller, PollOption};
// Unique ids for the two ends of the channel.
const CHANNEL_RECV_ID: EventedId = EventedId(0);
const CHANNEL_SEND_ID: EventedId = EventedId(1);
// Create a `Poller` instance and the events container.
let mut poller = Poller::new()?;
let mut events = Events::new();
// Create a new pipe.
let (mut sender, mut receiver) = new_pipe()?;
// Register both ends of the channel.
poller.register(&mut receiver, CHANNEL_RECV_ID, Receiver::INTERESTS, PollOption::Level)?;
poller.register(&mut sender, CHANNEL_SEND_ID, Sender::INTERESTS, PollOption::Level)?;
loop {
// Check for new events.
poller.poll(&mut events, None)?;
for event in &mut events {
match event.id() {
CHANNEL_RECV_ID => {
let mut buf = Vec::with_capacity(128);
let n = receiver.read(&mut buf)?;
println!("received: {:?}", &buf[0..n]);
},
CHANNEL_SEND_ID => sender.write_all(b"Hello world")?,
_ => unreachable!(),
}
}
}