diff --git a/tokio/src/net/windows/named_pipe.rs b/tokio/src/net/windows/named_pipe.rs index 501f12cbbda..4fa9019b6f5 100644 --- a/tokio/src/net/windows/named_pipe.rs +++ b/tokio/src/net/windows/named_pipe.rs @@ -2272,6 +2272,7 @@ impl ServerOptions { pub struct ClientOptions { desired_access: u32, security_qos_flags: u32, + pipe_mode: PipeMode, } impl ClientOptions { @@ -2293,6 +2294,7 @@ impl ClientOptions { desired_access: windows_sys::GENERIC_READ | windows_sys::GENERIC_WRITE, security_qos_flags: windows_sys::SECURITY_IDENTIFICATION | windows_sys::SECURITY_SQOS_PRESENT, + pipe_mode: PipeMode::Byte, } } @@ -2345,6 +2347,15 @@ impl ClientOptions { self } + /// The pipe mode. + /// + /// The default pipe mode is [`PipeMode::Byte`]. See [`PipeMode`] for + /// documentation of what each mode means. + pub fn pipe_mode(&mut self, pipe_mode: PipeMode) -> &mut Self { + self.pipe_mode = pipe_mode; + self + } + /// Opens the named pipe identified by `addr`. /// /// This opens the client using [`CreateFile`] with the @@ -2441,6 +2452,20 @@ impl ClientOptions { return Err(io::Error::last_os_error()); } + if matches!(self.pipe_mode, PipeMode::Message) { + let mut mode = windows_sys::PIPE_READMODE_MESSAGE; + let result = windows_sys::SetNamedPipeHandleState( + h, + &mut mode, + ptr::null_mut(), + ptr::null_mut(), + ); + + if result == 0 { + return Err(io::Error::last_os_error()); + } + } + NamedPipeClient::from_raw_handle(h as _) } diff --git a/tokio/tests/net_named_pipe.rs b/tokio/tests/net_named_pipe.rs index dbf1514b870..3ddc4c8a9bf 100644 --- a/tokio/tests/net_named_pipe.rs +++ b/tokio/tests/net_named_pipe.rs @@ -349,9 +349,7 @@ async fn _named_pipe_mode_message(mode: PipeMode) -> io::Result<()> { .pipe_mode(mode) .create(&pipe_name)?; - let mut client = ClientOptions::new() - //.pipe_mode(mode) - .open(&pipe_name)?; + let mut client = ClientOptions::new().pipe_mode(mode).open(&pipe_name)?; server.connect().await?;