跳到主要内容

ClientOptions

搜索

结构体 ClientOptions 

源代码
pub struct ClientOptions { /* 私有字段 */ }
展开描述

适用于从客户端构建和与命名管道交互的 builder。

参见 ClientOptions::open

实现§

源代码§

impl ClientOptions

源代码

pub fn new() -> Self

使用默认设置创建一个新的命名管道 builder。

use tokio::net::windows::named_pipe::{ServerOptions, ClientOptions};

const PIPE_NAME: &str = r"\\.\pipe\tokio-named-pipe-client-new";

// Server must be created in order for the client creation to succeed.
let server = ServerOptions::new().create(PIPE_NAME)?;
let client = ClientOptions::new().open(PIPE_NAME)?;
源代码

pub fn read(&mut self, allowed: bool) -> &mut Self

如果客户端支持读取数据。默认情况下这是启用的。

这对应于在调用 CreateFile 时设置 GENERIC_READ

源代码

pub fn write(&mut self, allowed: bool) -> &mut Self

如果创建的管道支持写入数据。默认情况下这是启用的。

这对应于在调用 CreateFile 时设置 GENERIC_WRITE

源代码

pub fn security_qos_flags(&mut self, flags: u32) -> &mut Self

设置 qos 标志,这些标志会与 CreateFile 调用中的其他标志和属性组合在一起。

默认情况下,security_qos_flags 被设置为 SECURITY_IDENTIFICATION,调用此函数将完全使用指定的参数覆盖该值。

security_qos_flags 未设置时,如果允许打开用户指定的路径,则恶意程序可以通过欺骗特权 Rust 进程打开命名管道来获得该进程的提升特权。因此,在打开任意路径时,也应该设置 security_qos_flags。但是,这些位可能与其他标志冲突,特别是 FILE_FLAG_OPEN_NO_RECALL

有关可能的值的信息,请参阅 Windows 开发人员中心网站上的模拟级别。使用此方法时会自动设置 SECURITY_SQOS_PRESENT 标志。

源代码

pub fn pipe_mode(&mut self, pipe_mode: PipeMode) -> &mut Self

管道模式。

默认管道模式为 PipeMode::Byte。关于每种模式的含义,请参阅 PipeMode 的文档。

源代码

pub fn open(&self, addr: impl AsRef<OsStr>) -> Result<NamedPipeClient>

打开由 addr 标识的命名管道。

这使用 CreateFile 打开客户端,并将 dwCreationDisposition 选项设置为 OPEN_EXISTING

§错误

如果在 Tokio 运行时 之外调用、在未启用 I/O 的运行时中调用,或发生任何操作系统特定的 I/O 错误,则返回错误。

在客户端创建命名管道时,需要考虑一些错误:

  • std::io::ErrorKind::NotFound - This indicates that the named pipe does not exist. Presumably the server is not up.
  • ERROR_PIPE_BUSY - This error is raised when the named pipe exists, but the server is not currently waiting for a connection. Please see the examples for how to check for this error.

一个等待管道可用的连接循环如下所示:

use std::time::Duration;
use tokio::net::windows::named_pipe::ClientOptions;
use tokio::time;
use windows_sys::Win32::Foundation::ERROR_PIPE_BUSY;

const PIPE_NAME: &str = r"\\.\pipe\mynamedpipe";

let client = loop {
    match ClientOptions::new().open(PIPE_NAME) {
        Ok(client) => break client,
        Err(e) if e.raw_os_error() == Some(ERROR_PIPE_BUSY as i32) => (),
        Err(e) => return Err(e),
    }

    time::sleep(Duration::from_millis(50)).await;
};

// use the connected client.
源代码

pub unsafe fn open_with_security_attributes_raw( &self, addr: impl AsRef<OsStr>, attrs: *mut c_void, ) -> Result<NamedPipeClient>

打开由 addr 标识的命名管道。

这与 open 相同,只是它支持提供指向 SECURITY_ATTRIBUTES 结构的原始指针,该指针将作为 lpSecurityAttributes 参数传递给 CreateFile

§安全性

attrs 参数必须为 null 或指向 SECURITY_ATTRIBUTES 结构体的有效实例。如果该参数为 null,则其行为与调用 open 方法完全相同。

trait 实现§

源代码§

impl Clone for ClientOptions

源代码§

fn clone(&self) -> ClientOptions

返回值的副本。 更多信息
1.0.0 · 源代码§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. 更多信息
源代码§

impl Debug for ClientOptions

源代码§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

使用给定的格式化器格式化此值。 更多信息

自动 trait 实现§

blanket 实现§

源代码§

impl<T> Any for T
where T: 'static + ?Sized,

源代码§

fn type_id(&self) -> TypeId

Gets the TypeId of self. 更多信息
源代码§

impl<T> Borrow<T> for T
where T: ?Sized,

源代码§

fn borrow(&self) -> &T

Immutably borrows from an owned value. 更多信息
源代码§

impl<T> BorrowMut<T> for T
where T: ?Sized,

源代码§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. 更多信息
源代码§

impl<T> CloneToUninit for T
where T: Clone,

源代码§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 更多信息
源代码§

impl<T> From<T> for T

源代码§

fn from(t: T) -> T

原样返回参数。

源代码§

impl<T, U> Into<U> for T
where U: From<T>,

源代码§

fn into(self) -> U

调用 U::from(self)

也就是说,此转换是 From<T> for U 实现选择执行的操作。

源代码§

impl<T> ToOwned for T
where T: Clone,

源代码§

type Owned = T

获得所有权后的类型。
源代码§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. 更多信息
源代码§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 更多信息
源代码§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

源代码§

type Error = Infallible

转换出错时返回的类型。
源代码§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

执行转换。
源代码§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

源代码§

type Error = <U as TryFrom<T>>::Error

转换出错时返回的类型。
源代码§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

执行转换。