Last active
August 7, 2019 04:26
-
-
Save hankbao/7fe63596d1fc46388025fd7a301cf57b to your computer and use it in GitHub Desktop.
Simple pipe server
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
[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" |
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
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