pub struct NamedPipeServer { /* private fields */ }
Expand description
A Windows named pipe server.
Accepting client connections involves creating a server with
ServerOptions::create
and waiting for clients to connect using
NamedPipeServer::connect
.
To avoid having clients sporadically fail with
std::io::ErrorKind::NotFound
when they connect to a server, we must
ensure that at least one server instance is available at all times. This
means that the typical listen loop for a server is a bit involved, because
we have to ensure that we never drop a server accidentally while a client
might connect.
So a correctly implemented server looks like this:
use std::io;
use compio_fs::named_pipe::ServerOptions;
const PIPE_NAME: &str = r"\\.\pipe\named-pipe-idiomatic-server";
// The first server needs to be constructed early so that clients can
// be correctly connected. Otherwise calling .wait will cause the client to
// error.
//
// Here we also make use of `first_pipe_instance`, which will ensure that
// there are no other servers up and running already.
let mut server = ServerOptions::new()
.first_pipe_instance(true)
.create(PIPE_NAME)?;
// Spawn the server loop.
loop {
// Wait for a client to connect.
let connected = server.connect().await?;
// Construct the next server to be connected before sending the one
// we already have of onto a task. This ensures that the server
// isn't closed (after it's done in the task) before a new one is
// available. Otherwise the client might error with
// `io::ErrorKind::NotFound`.
server = ServerOptions::new().create(PIPE_NAME)?;
let client = compio_runtime::spawn(async move {
// use the connected client
});
}
Implementations§
Source§impl NamedPipeServer
impl NamedPipeServer
Sourcepub fn info(&self) -> Result<PipeInfo>
pub fn info(&self) -> Result<PipeInfo>
Retrieves information about the named pipe the server is associated with.
use compio_fs::named_pipe::{PipeEnd, PipeMode, ServerOptions};
const PIPE_NAME: &str = r"\\.\pipe\compio-named-pipe-server-info";
let server = ServerOptions::new()
.pipe_mode(PipeMode::Message)
.max_instances(5)
.create(PIPE_NAME)?;
let server_info = server.info()?;
assert_eq!(server_info.end, PipeEnd::Server);
assert_eq!(server_info.mode, PipeMode::Message);
assert_eq!(server_info.max_instances, 5);
Sourcepub async fn connect(&self) -> Result<()>
pub async fn connect(&self) -> Result<()>
Enables a named pipe server process to wait for a client process to connect to an instance of a named pipe. A client process connects by creating a named pipe with the same name.
This corresponds to the ConnectNamedPipe
system call.
§Example
use compio_fs::named_pipe::ServerOptions;
const PIPE_NAME: &str = r"\\.\pipe\mynamedpipe";
let pipe = ServerOptions::new().create(PIPE_NAME)?;
// Wait for a client to connect.
pipe.connect().await?;
// Use the connected client...
Sourcepub fn disconnect(&self) -> Result<()>
pub fn disconnect(&self) -> Result<()>
Disconnects the server end of a named pipe instance from a client process.
use compio_fs::named_pipe::{ClientOptions, ServerOptions};
use compio_io::AsyncWrite;
use windows_sys::Win32::Foundation::ERROR_PIPE_NOT_CONNECTED;
const PIPE_NAME: &str = r"\\.\pipe\compio-named-pipe-disconnect";
let server = ServerOptions::new().create(PIPE_NAME).unwrap();
let mut client = ClientOptions::new().open(PIPE_NAME).await.unwrap();
// Wait for a client to become connected.
server.connect().await.unwrap();
// Forcibly disconnect the client.
server.disconnect().unwrap();
// Write fails with an OS-specific error after client has been
// disconnected.
let e = client.write("ping").await.0.unwrap();
assert_eq!(e, 0);
Trait Implementations§
Source§impl AsRawHandle for NamedPipeServer
impl AsRawHandle for NamedPipeServer
Source§fn as_raw_handle(&self) -> RawHandle
fn as_raw_handle(&self) -> RawHandle
Source§impl AsyncRead for &NamedPipeServer
impl AsyncRead for &NamedPipeServer
Source§impl AsyncRead for NamedPipeServer
impl AsyncRead for NamedPipeServer
Source§impl AsyncWrite for &NamedPipeServer
impl AsyncWrite for &NamedPipeServer
Source§async fn flush(&mut self) -> Result<()>
async fn flush(&mut self) -> Result<()>
Source§async fn shutdown(&mut self) -> Result<()>
async fn shutdown(&mut self) -> Result<()>
Source§async fn write_vectored<T>(&mut self, buf: T) -> BufResult<usize, T>where
T: IoVectoredBuf,
async fn write_vectored<T>(&mut self, buf: T) -> BufResult<usize, T>where
T: IoVectoredBuf,
write
, except that it write bytes from a buffer implements
IoVectoredBuf
into the source. Read moreSource§impl AsyncWrite for NamedPipeServer
impl AsyncWrite for NamedPipeServer
Source§async fn flush(&mut self) -> Result<()>
async fn flush(&mut self) -> Result<()>
Source§async fn shutdown(&mut self) -> Result<()>
async fn shutdown(&mut self) -> Result<()>
Source§async fn write_vectored<T>(&mut self, buf: T) -> BufResult<usize, T>where
T: IoVectoredBuf,
async fn write_vectored<T>(&mut self, buf: T) -> BufResult<usize, T>where
T: IoVectoredBuf,
write
, except that it write bytes from a buffer implements
IoVectoredBuf
into the source. Read moreSource§impl Clone for NamedPipeServer
impl Clone for NamedPipeServer
Source§fn clone(&self) -> NamedPipeServer
fn clone(&self) -> NamedPipeServer
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Debug for NamedPipeServer
impl Debug for NamedPipeServer
Source§impl FromRawHandle for NamedPipeServer
impl FromRawHandle for NamedPipeServer
Source§unsafe fn from_raw_handle(handle: RawHandle) -> Self
unsafe fn from_raw_handle(handle: RawHandle) -> Self
SharedFd
.Auto Trait Implementations§
impl Freeze for NamedPipeServer
impl RefUnwindSafe for NamedPipeServer
impl Send for NamedPipeServer
impl Sync for NamedPipeServer
impl Unpin for NamedPipeServer
impl UnwindSafe for NamedPipeServer
Blanket Implementations§
Source§impl<A> AsyncReadExt for A
impl<A> AsyncReadExt for A
Source§async fn read_exact<T>(&mut self, buf: T) -> BufResult<(), T>where
T: IoBufMut,
async fn read_exact<T>(&mut self, buf: T) -> BufResult<(), T>where
T: IoBufMut,
Source§async fn read_to_end(&mut self, buf: Vec<u8>) -> BufResult<usize, Vec<u8>>
async fn read_to_end(&mut self, buf: Vec<u8>) -> BufResult<usize, Vec<u8>>
EOF
.Source§async fn read_vectored_exact<T>(&mut self, buf: T) -> BufResult<(), T>where
T: IoVectoredBufMut,
async fn read_vectored_exact<T>(&mut self, buf: T) -> BufResult<(), T>where
T: IoVectoredBufMut,
Source§fn take(self, limit: u64) -> Take<Self>where
Self: Sized,
fn take(self, limit: u64) -> Take<Self>where
Self: Sized,
limit
bytes from it. Read moreSource§async fn read_u8(&mut self) -> Result<u8, Error>
async fn read_u8(&mut self) -> Result<u8, Error>
u8
from the underlying reader.Source§async fn read_u8_le(&mut self) -> Result<u8, Error>
async fn read_u8_le(&mut self) -> Result<u8, Error>
u8
from the underlying reader.Source§async fn read_u16(&mut self) -> Result<u16, Error>
async fn read_u16(&mut self) -> Result<u16, Error>
u16
from the underlying reader.Source§async fn read_u16_le(&mut self) -> Result<u16, Error>
async fn read_u16_le(&mut self) -> Result<u16, Error>
u16
from the underlying reader.Source§async fn read_u32(&mut self) -> Result<u32, Error>
async fn read_u32(&mut self) -> Result<u32, Error>
u32
from the underlying reader.Source§async fn read_u32_le(&mut self) -> Result<u32, Error>
async fn read_u32_le(&mut self) -> Result<u32, Error>
u32
from the underlying reader.Source§async fn read_u64(&mut self) -> Result<u64, Error>
async fn read_u64(&mut self) -> Result<u64, Error>
u64
from the underlying reader.Source§async fn read_u64_le(&mut self) -> Result<u64, Error>
async fn read_u64_le(&mut self) -> Result<u64, Error>
u64
from the underlying reader.Source§async fn read_u128(&mut self) -> Result<u128, Error>
async fn read_u128(&mut self) -> Result<u128, Error>
u128
from the underlying reader.Source§async fn read_u128_le(&mut self) -> Result<u128, Error>
async fn read_u128_le(&mut self) -> Result<u128, Error>
u128
from the underlying reader.Source§async fn read_i8(&mut self) -> Result<i8, Error>
async fn read_i8(&mut self) -> Result<i8, Error>
i8
from the underlying reader.Source§async fn read_i8_le(&mut self) -> Result<i8, Error>
async fn read_i8_le(&mut self) -> Result<i8, Error>
i8
from the underlying reader.Source§async fn read_i16(&mut self) -> Result<i16, Error>
async fn read_i16(&mut self) -> Result<i16, Error>
i16
from the underlying reader.Source§async fn read_i16_le(&mut self) -> Result<i16, Error>
async fn read_i16_le(&mut self) -> Result<i16, Error>
i16
from the underlying reader.Source§async fn read_i32(&mut self) -> Result<i32, Error>
async fn read_i32(&mut self) -> Result<i32, Error>
i32
from the underlying reader.Source§async fn read_i32_le(&mut self) -> Result<i32, Error>
async fn read_i32_le(&mut self) -> Result<i32, Error>
i32
from the underlying reader.Source§async fn read_i64(&mut self) -> Result<i64, Error>
async fn read_i64(&mut self) -> Result<i64, Error>
i64
from the underlying reader.Source§async fn read_i64_le(&mut self) -> Result<i64, Error>
async fn read_i64_le(&mut self) -> Result<i64, Error>
i64
from the underlying reader.Source§async fn read_i128(&mut self) -> Result<i128, Error>
async fn read_i128(&mut self) -> Result<i128, Error>
i128
from the underlying reader.Source§async fn read_i128_le(&mut self) -> Result<i128, Error>
async fn read_i128_le(&mut self) -> Result<i128, Error>
i128
from the underlying reader.Source§async fn read_f32(&mut self) -> Result<f32, Error>
async fn read_f32(&mut self) -> Result<f32, Error>
f32
from the underlying reader.Source§async fn read_f32_le(&mut self) -> Result<f32, Error>
async fn read_f32_le(&mut self) -> Result<f32, Error>
f32
from the underlying reader.Source§impl<A> AsyncWriteExt for Awhere
A: AsyncWrite + ?Sized,
impl<A> AsyncWriteExt for Awhere
A: AsyncWrite + ?Sized,
Source§fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
AsyncWrite
. Read moreSource§async fn write_all<T>(&mut self, buf: T) -> BufResult<(), T>where
T: IoBuf,
async fn write_all<T>(&mut self, buf: T) -> BufResult<(), T>where
T: IoBuf,
Source§async fn write_vectored_all<T>(&mut self, buf: T) -> BufResult<(), T>where
T: IoVectoredBuf,
async fn write_vectored_all<T>(&mut self, buf: T) -> BufResult<(), T>where
T: IoVectoredBuf,
AsyncWrite::write_vectored
, except that it tries to write the entire
contents of the buffer into this writer.Source§async fn write_u8(&mut self, num: u8) -> Result<(), Error>
async fn write_u8(&mut self, num: u8) -> Result<(), Error>
u8
into the underlying writer.Source§async fn write_u8_le(&mut self, num: u8) -> Result<(), Error>
async fn write_u8_le(&mut self, num: u8) -> Result<(), Error>
u8
into the underlying writer.Source§async fn write_u16(&mut self, num: u16) -> Result<(), Error>
async fn write_u16(&mut self, num: u16) -> Result<(), Error>
u16
into the underlying writer.Source§async fn write_u16_le(&mut self, num: u16) -> Result<(), Error>
async fn write_u16_le(&mut self, num: u16) -> Result<(), Error>
u16
into the underlying writer.Source§async fn write_u32(&mut self, num: u32) -> Result<(), Error>
async fn write_u32(&mut self, num: u32) -> Result<(), Error>
u32
into the underlying writer.Source§async fn write_u32_le(&mut self, num: u32) -> Result<(), Error>
async fn write_u32_le(&mut self, num: u32) -> Result<(), Error>
u32
into the underlying writer.Source§async fn write_u64(&mut self, num: u64) -> Result<(), Error>
async fn write_u64(&mut self, num: u64) -> Result<(), Error>
u64
into the underlying writer.Source§async fn write_u64_le(&mut self, num: u64) -> Result<(), Error>
async fn write_u64_le(&mut self, num: u64) -> Result<(), Error>
u64
into the underlying writer.Source§async fn write_u128(&mut self, num: u128) -> Result<(), Error>
async fn write_u128(&mut self, num: u128) -> Result<(), Error>
u128
into the underlying writer.Source§async fn write_u128_le(&mut self, num: u128) -> Result<(), Error>
async fn write_u128_le(&mut self, num: u128) -> Result<(), Error>
u128
into the underlying writer.Source§async fn write_i8(&mut self, num: i8) -> Result<(), Error>
async fn write_i8(&mut self, num: i8) -> Result<(), Error>
i8
into the underlying writer.Source§async fn write_i8_le(&mut self, num: i8) -> Result<(), Error>
async fn write_i8_le(&mut self, num: i8) -> Result<(), Error>
i8
into the underlying writer.Source§async fn write_i16(&mut self, num: i16) -> Result<(), Error>
async fn write_i16(&mut self, num: i16) -> Result<(), Error>
i16
into the underlying writer.Source§async fn write_i16_le(&mut self, num: i16) -> Result<(), Error>
async fn write_i16_le(&mut self, num: i16) -> Result<(), Error>
i16
into the underlying writer.Source§async fn write_i32(&mut self, num: i32) -> Result<(), Error>
async fn write_i32(&mut self, num: i32) -> Result<(), Error>
i32
into the underlying writer.Source§async fn write_i32_le(&mut self, num: i32) -> Result<(), Error>
async fn write_i32_le(&mut self, num: i32) -> Result<(), Error>
i32
into the underlying writer.Source§async fn write_i64(&mut self, num: i64) -> Result<(), Error>
async fn write_i64(&mut self, num: i64) -> Result<(), Error>
i64
into the underlying writer.Source§async fn write_i64_le(&mut self, num: i64) -> Result<(), Error>
async fn write_i64_le(&mut self, num: i64) -> Result<(), Error>
i64
into the underlying writer.Source§async fn write_i128(&mut self, num: i128) -> Result<(), Error>
async fn write_i128(&mut self, num: i128) -> Result<(), Error>
i128
into the underlying writer.Source§async fn write_i128_le(&mut self, num: i128) -> Result<(), Error>
async fn write_i128_le(&mut self, num: i128) -> Result<(), Error>
i128
into the underlying writer.Source§async fn write_f32(&mut self, num: f32) -> Result<(), Error>
async fn write_f32(&mut self, num: f32) -> Result<(), Error>
f32
into the underlying writer.Source§async fn write_f32_le(&mut self, num: f32) -> Result<(), Error>
async fn write_f32_le(&mut self, num: f32) -> Result<(), Error>
f32
into the underlying writer.