Skip to content

Commit

Permalink
net: add ClientOptions.pipe_mode for named pipes
Browse files Browse the repository at this point in the history
  • Loading branch information
mhils committed Jan 6, 2023
1 parent d47f9b5 commit fc98c30
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
25 changes: 25 additions & 0 deletions tokio/src/net/windows/named_pipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2272,6 +2272,7 @@ impl ServerOptions {
pub struct ClientOptions {
desired_access: u32,
security_qos_flags: u32,
pipe_mode: PipeMode,
}

impl ClientOptions {
Expand All @@ -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,
}
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 _)
}

Expand Down
4 changes: 1 addition & 3 deletions tokio/tests/net_named_pipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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?;

Expand Down

0 comments on commit fc98c30

Please sign in to comment.