Skip to content

Commit 6d1efe2

Browse files
committed
net: add ClientOptions.pipe_mode for named pipes
1 parent 24b5c68 commit 6d1efe2

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

tokio/src/net/windows/named_pipe.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2272,6 +2272,7 @@ impl ServerOptions {
22722272
pub struct ClientOptions {
22732273
desired_access: u32,
22742274
security_qos_flags: u32,
2275+
pipe_mode: PipeMode,
22752276
}
22762277

22772278
impl ClientOptions {
@@ -2293,6 +2294,7 @@ impl ClientOptions {
22932294
desired_access: windows_sys::GENERIC_READ | windows_sys::GENERIC_WRITE,
22942295
security_qos_flags: windows_sys::SECURITY_IDENTIFICATION
22952296
| windows_sys::SECURITY_SQOS_PRESENT,
2297+
pipe_mode: PipeMode::Byte,
22962298
}
22972299
}
22982300

@@ -2345,6 +2347,15 @@ impl ClientOptions {
23452347
self
23462348
}
23472349

2350+
/// The pipe mode.
2351+
///
2352+
/// The default pipe mode is [`PipeMode::Byte`]. See [`PipeMode`] for
2353+
/// documentation of what each mode means.
2354+
pub fn pipe_mode(&mut self, pipe_mode: PipeMode) -> &mut Self {
2355+
self.pipe_mode = pipe_mode;
2356+
self
2357+
}
2358+
23482359
/// Opens the named pipe identified by `addr`.
23492360
///
23502361
/// This opens the client using [`CreateFile`] with the
@@ -2441,6 +2452,20 @@ impl ClientOptions {
24412452
return Err(io::Error::last_os_error());
24422453
}
24432454

2455+
if matches!(self.pipe_mode, PipeMode::Message) {
2456+
let mut mode = windows_sys::PIPE_READMODE_MESSAGE;
2457+
let result = windows_sys::SetNamedPipeHandleState(
2458+
h,
2459+
&mut mode,
2460+
ptr::null_mut(),
2461+
ptr::null_mut(),
2462+
);
2463+
2464+
if result == 0 {
2465+
return Err(io::Error::last_os_error());
2466+
}
2467+
}
2468+
24442469
NamedPipeClient::from_raw_handle(h as _)
24452470
}
24462471

@@ -2560,7 +2585,9 @@ unsafe fn named_pipe_info(handle: RawHandle) -> io::Result<PipeInfo> {
25602585

25612586
#[cfg(test)]
25622587
mod test {
2563-
use self::windows_sys::{PIPE_REJECT_REMOTE_CLIENTS, PIPE_TYPE_BYTE, PIPE_TYPE_MESSAGE, PIPE_READMODE_MESSAGE};
2588+
use self::windows_sys::{
2589+
PIPE_READMODE_MESSAGE, PIPE_REJECT_REMOTE_CLIENTS, PIPE_TYPE_BYTE, PIPE_TYPE_MESSAGE,
2590+
};
25642591
use super::*;
25652592

25662593
#[test]

tokio/tests/net_named_pipe.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -349,9 +349,7 @@ async fn _named_pipe_mode_message(mode: PipeMode) -> io::Result<()> {
349349
.pipe_mode(mode)
350350
.create(&pipe_name)?;
351351

352-
let mut client = ClientOptions::new()
353-
//.pipe_mode(mode)
354-
.open(&pipe_name)?;
352+
let mut client = ClientOptions::new().pipe_mode(mode).open(&pipe_name)?;
355353

356354
server.connect().await?;
357355

0 commit comments

Comments
 (0)