Last active
August 7, 2019 08:22
-
-
Save LuoZijun/8f91e99b5aaa87fba1f4696d1783a762 to your computer and use it in GitHub Desktop.
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
// mio = "0.6" | |
// mio-named-pipes = "0.1" | |
extern crate mio; | |
extern crate mio_named_pipes; | |
use mio_named_pipes::NamedPipe; | |
use std::io::{self, Read, Write}; | |
const SERVER: Token = Token(0); | |
const CLIENT: Token = Token(1); | |
const DOMAIN_NAME: &str = "pipe.sock"; | |
fn server() -> Result<(), io::Error> { | |
let mut pipe = NamedPipe::new(DOMAIN_NAME)?; | |
let poll = mio::Poll::new().unwrap(); | |
poll.register(&pipe, SERVER, mio::Ready::readable(), mio::PollOpt::edge())?; | |
let mut buffer = [0u8; 2048]; | |
let mut events = mio::Events::with_capacity(1024); | |
loop { | |
poll.poll(&mut events, None)?; | |
for event in events.iter() { | |
match event.token() { | |
SERVER => { | |
let amt = pipe.read(&mut buffer)?; | |
println!("read {:?} bytes.", amt); | |
}, | |
_ => unreachable!(), | |
} | |
} | |
} | |
Ok(()) | |
} | |
fn client() -> Result<(), io::Error> { | |
let mut pipe = NamedPipe::new(DOMAIN_NAME)?; | |
pipe.connect()?; | |
let poll = mio::Poll::new().unwrap(); | |
poll.register(&pipe, CLIENT, mio::Ready::readable(), mio::PollOpt::edge())?; | |
let mut events = mio::Events::with_capacity(1024); | |
let payload = b"hi. hi."; | |
let amt = pipe.write_all(&payload)?; | |
println!("write {:?} bytes.", amt); | |
Ok(()) | |
} | |
fn main() -> Result<(), io::Error> { | |
server()?; | |
// client()?; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment