|
| 1 | +#![doc = include_str!("../README.md")] |
| 2 | + |
| 3 | +use std::{ |
| 4 | + ffi::OsStr, |
| 5 | + io::{self, Read, Write}, |
| 6 | + pin::Pin, |
| 7 | + task::{Context, Poll}, |
| 8 | +}; |
| 9 | + |
| 10 | +use tokio::io::{AsyncRead, AsyncWrite, ReadBuf}; |
| 11 | + |
| 12 | +#[cfg(unix)] |
| 13 | +mod unix; |
| 14 | +#[cfg(unix)] |
| 15 | +use unix as imp; |
| 16 | +#[cfg(windows)] |
| 17 | +mod windows; |
| 18 | +#[cfg(windows)] |
| 19 | +use windows as imp; |
| 20 | + |
| 21 | +#[cfg(not(any(unix, windows)))] |
| 22 | +compile_error!("pipe_socket supports only Unix and Windows"); |
| 23 | + |
| 24 | +/// A named server that asynchronously accepts byte-stream connections. |
| 25 | +pub struct Server { |
| 26 | + inner: imp::Server, |
| 27 | +} |
| 28 | + |
| 29 | +impl Server { |
| 30 | + /// Creates a server with a new unique name. |
| 31 | + /// |
| 32 | + /// # Errors |
| 33 | + /// |
| 34 | + /// Returns an error if the platform transport cannot be created. |
| 35 | + pub fn bind() -> io::Result<Self> { |
| 36 | + imp::Server::bind().map(|inner| Self { inner }) |
| 37 | + } |
| 38 | + |
| 39 | + /// Returns the opaque name clients use to connect to this server. |
| 40 | + #[must_use] |
| 41 | + pub fn name(&self) -> &OsStr { |
| 42 | + self.inner.name() |
| 43 | + } |
| 44 | + |
| 45 | + /// Waits for and accepts the next client connection. |
| 46 | + /// |
| 47 | + /// # Errors |
| 48 | + /// |
| 49 | + /// Returns an error if the connection cannot be accepted. |
| 50 | + pub async fn accept(&mut self) -> io::Result<ServerConnection> { |
| 51 | + self.inner.accept().await.map(|inner| ServerConnection { inner }) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +/// The server side of an accepted byte-stream connection. |
| 56 | +pub struct ServerConnection { |
| 57 | + inner: imp::ServerConnection, |
| 58 | +} |
| 59 | + |
| 60 | +impl AsyncRead for ServerConnection { |
| 61 | + fn poll_read( |
| 62 | + mut self: Pin<&mut Self>, |
| 63 | + cx: &mut Context<'_>, |
| 64 | + buf: &mut ReadBuf<'_>, |
| 65 | + ) -> Poll<io::Result<()>> { |
| 66 | + Pin::new(&mut self.inner).poll_read(cx, buf) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +impl AsyncWrite for ServerConnection { |
| 71 | + fn poll_write( |
| 72 | + mut self: Pin<&mut Self>, |
| 73 | + cx: &mut Context<'_>, |
| 74 | + buf: &[u8], |
| 75 | + ) -> Poll<io::Result<usize>> { |
| 76 | + Pin::new(&mut self.inner).poll_write(cx, buf) |
| 77 | + } |
| 78 | + |
| 79 | + fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> { |
| 80 | + Pin::new(&mut self.inner).poll_flush(cx) |
| 81 | + } |
| 82 | + |
| 83 | + fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> { |
| 84 | + Pin::new(&mut self.inner).poll_shutdown(cx) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +/// A synchronous client byte stream connected by a server name. |
| 89 | +pub struct Client { |
| 90 | + inner: imp::Client, |
| 91 | +} |
| 92 | + |
| 93 | +impl Client { |
| 94 | + /// Connects to the server identified by `name`. |
| 95 | + /// |
| 96 | + /// # Errors |
| 97 | + /// |
| 98 | + /// Returns an error if the name is invalid or the server cannot be reached. |
| 99 | + pub fn connect(name: &OsStr) -> io::Result<Self> { |
| 100 | + imp::Client::connect(name).map(|inner| Self { inner }) |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +impl Read for Client { |
| 105 | + fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { |
| 106 | + self.inner.read(buf) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +impl Write for Client { |
| 111 | + fn write(&mut self, buf: &[u8]) -> io::Result<usize> { |
| 112 | + self.inner.write(buf) |
| 113 | + } |
| 114 | + |
| 115 | + fn flush(&mut self) -> io::Result<()> { |
| 116 | + self.inner.flush() |
| 117 | + } |
| 118 | +} |
0 commit comments