Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mitsuhiko/unix-ipc
A super minimal wrapper around unix sockets for IPC.
https://github.com/mitsuhiko/unix-ipc
Last synced: 3 months ago
JSON representation
A super minimal wrapper around unix sockets for IPC.
- Host: GitHub
- URL: https://github.com/mitsuhiko/unix-ipc
- Owner: mitsuhiko
- Created: 2020-02-29T11:30:41.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2021-12-17T13:39:54.000Z (about 3 years ago)
- Last Synced: 2024-10-13T01:35:30.540Z (3 months ago)
- Language: Rust
- Size: 62.5 KB
- Stars: 38
- Watchers: 3
- Forks: 7
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# unix-ipc
This crate implements a minimal abstraction over UNIX domain sockets for
the purpose of IPC. It lets you send both file handles and rust objects
between processes.## How it works
This uses [serde](https://serde.rs/) to serialize data over unix sockets
via [bincode](https://github.com/servo/bincode). Thanks to the
[`Handle`](https://docs.rs/unix-ipc/latest/unix_ipc/struct.Handle.html)
abstraction you can also send any object across that is convertable into a unix
file handle.The way this works under the hood is that during serialization and
deserialization encountered file descriptors are tracked. They are then
sent over the unix socket separately. This lets unassociated processes
share file handles.If you only want the unix socket abstraction you can disable all default
features and use the raw channels.## Example
```rust
use std::env;
use std::process;
use unix_ipc::{channel, Bootstrapper, Receiver, Sender};
use serde::{Deserialize, Serialize};const ENV_VAR: &str = "PROC_CONNECT_TO";
#[derive(Serialize, Deserialize, Debug)]
pub enum Task {
Sum(Vec, Sender),
Shutdown,
}if let Ok(path) = env::var(ENV_VAR) {
let receiver = Receiver::::connect(path).unwrap();
loop {
match receiver.recv().unwrap() {
Task::Sum(values, tx) => {
tx.send(values.into_iter().sum::()).unwrap();
}
Task::Shutdown => break,
}
}
} else {
let bootstrapper = Bootstrapper::new().unwrap();
let mut child = process::Command::new(env::current_exe().unwrap())
.env(ENV_VAR, bootstrapper.path())
.spawn()
.unwrap();let (tx, rx) = channel().unwrap();
bootstrapper.send(Task::Sum(vec![23, 42], tx)).unwrap();
println!("sum: {}", rx.recv().unwrap());
bootstrapper.send(Task::Shutdown).unwrap();
}
```License: MIT/Apache-2.0