Skip to content

Instantly share code, notes, and snippets.

@hankbao
Last active August 7, 2019 04:26
Show Gist options
  • Save hankbao/7fe63596d1fc46388025fd7a301cf57b to your computer and use it in GitHub Desktop.
Save hankbao/7fe63596d1fc46388025fd7a301cf57b to your computer and use it in GitHub Desktop.
Simple pipe server
[package]
name = "ipc-server"
version = "0.1.0"
authors = ["hankbao"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
futures = "0.1"
parity-tokio-ipc = "0.2"
tokio = "0.1"
use futures::prelude::*;
use parity_tokio_ipc::{Endpoint, Incoming};
use tokio;
use std::io::ErrorKind;
const DAEMON_PIPE_NAME: &str = "\\\\.\\pipe\\TestPipe\\ServiceControl\0";
pub struct IpcServer {
incoming: Incoming,
}
impl IpcServer {
pub fn new() -> Self {
let endpoint = Endpoint::new(DAEMON_PIPE_NAME.to_string());
let incoming = match endpoint.incoming(&tokio::reactor::Handle::default()) {
Ok(inc) => inc,
Err(e) => {
println!("IpcServer: Endpoint creation failed {:?}", e);
panic!("Check the pipe");
}
};
IpcServer {
incoming,
}
}
}
impl Future for IpcServer {
type Item = ();
type Error = ();
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
loop {
match self.incoming.poll() {
Ok(Async::Ready(Some((_, _)))) => {
println!("IpcServer: new pipe incoming");
continue;
}
Ok(Async::Ready(None)) => {
panic!("impossible!");
}
Ok(Async::NotReady) => return Ok(Async::NotReady),
Err(e) => {
match e.kind() {
ErrorKind::BrokenPipe | ErrorKind::Interrupted => continue,
_ => {
println!("IpcServer: endpoint connection error {:?}", e);
return Err(());
}
}
}
}
}
}
}
fn main() {
tokio::run(IpcServer::new());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment