pub struct ClientOptions { /* 私有字段 */ }展开描述
适用于从客户端构建和与命名管道交互的 builder。
实现§
源代码§impl ClientOptions
impl ClientOptions
源代码pub fn new() -> Self
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
pub fn read(&mut self, allowed: bool) -> &mut Self
如果客户端支持读取数据。默认情况下这是启用的。
这对应于在调用 CreateFile 时设置 GENERIC_READ。
源代码pub fn write(&mut self, allowed: bool) -> &mut Self
pub fn write(&mut self, allowed: bool) -> &mut Self
如果创建的管道支持写入数据。默认情况下这是启用的。
这对应于在调用 CreateFile 时设置 GENERIC_WRITE。
源代码pub fn security_qos_flags(&mut self, flags: u32) -> &mut Self
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
pub fn pipe_mode(&mut self, pipe_mode: PipeMode) -> &mut Self
管道模式。
默认管道模式为 PipeMode::Byte。关于每种模式的含义,请参阅 PipeMode 的文档。
源代码pub fn open(&self, addr: impl AsRef<OsStr>) -> Result<NamedPipeClient>
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>
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
impl Clone for ClientOptions
源代码§fn clone(&self) -> ClientOptions
fn clone(&self) -> ClientOptions
1.0.0 · 源代码§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. 更多信息