commit 90ae4febd4075b77934f65d6e2771f65dc2c578a Author: Alex Crichton Date: Sun Dec 11 23:57:14 2016 -0800 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a9d37c5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +target +Cargo.lock diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..a7783d9 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "tokio-named-pipes" +version = "0.1.0" +authors = ["Alex Crichton "] + +[dependencies] +tokio-core = "0.1.1" +mio-named-pipes = { git = "https://github.com/alexcrichton/mio-named-pipes" } +futures = '0.1.4' + +[replace.'mio:0.6.1'] +git = "https://github.com/alexcrichton/mio" +branch = 'custom-iocp' diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..5fc9234 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,116 @@ +#![cfg(windows)] + +extern crate tokio_core; +extern crate mio_named_pipes; +extern crate futures; + +use std::ffi::OsStr; +use std::fmt; +use std::io::{self, Read, Write}; +use std::os::windows::io::*; + +use futures::Async; +use tokio_core::io::Io; +use tokio_core::reactor::{PollEvented, Handle}; + +pub struct NamedPipe { + io: PollEvented, +} + +impl NamedPipe { + pub fn new>(p: P, handle: &Handle) -> io::Result { + NamedPipe::_new(p.as_ref(), handle) + } + + fn _new(p: &OsStr, handle: &Handle) -> io::Result { + let inner = try!(mio_named_pipes::NamedPipe::new(p)); + NamedPipe::from_pipe(inner, handle) + } + + pub fn from_pipe(pipe: mio_named_pipes::NamedPipe, + handle: &Handle) + -> io::Result { + Ok(NamedPipe { + io: try!(PollEvented::new(pipe, handle)), + }) + } + + pub fn connect(&self) -> io::Result<()> { + self.io.get_ref().connect() + } + + pub fn disconnect(&self) -> io::Result<()> { + self.io.get_ref().disconnect() + } + + pub fn poll_read(&self) -> Async<()> { + self.io.poll_read() + } + + pub fn poll_write(&self) -> Async<()> { + self.io.poll_write() + } +} + +impl Read for NamedPipe { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + self.io.read(buf) + } +} + +impl Write for NamedPipe { + fn write(&mut self, buf: &[u8]) -> io::Result { + self.io.write(buf) + } + fn flush(&mut self) -> io::Result<()> { + self.io.flush() + } +} + +impl Io for NamedPipe { + fn poll_read(&mut self) -> Async<()> { + ::poll_read(self) + } + + fn poll_write(&mut self) -> Async<()> { + ::poll_write(self) + } +} + +impl<'a> Read for &'a NamedPipe { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + (&self.io).read(buf) + } +} + +impl<'a> Write for &'a NamedPipe { + fn write(&mut self, buf: &[u8]) -> io::Result { + (&self.io).write(buf) + } + + fn flush(&mut self) -> io::Result<()> { + (&self.io).flush() + } +} + +impl<'a> Io for &'a NamedPipe { + fn poll_read(&mut self) -> Async<()> { + ::poll_read(self) + } + + fn poll_write(&mut self) -> Async<()> { + ::poll_write(self) + } +} + +impl fmt::Debug for NamedPipe { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.io.get_ref().fmt(f) + } +} + +impl AsRawHandle for NamedPipe { + fn as_raw_handle(&self) -> RawHandle { + self.io.get_ref().as_raw_handle() + } +}