distant_net/client/builder/
windows.rs

1use std::ffi::OsString;
2use std::io;
3
4use async_trait::async_trait;
5
6use super::Connector;
7use crate::common::WindowsPipeTransport;
8
9/// Implementation of [`Connector`] to support connecting via a Windows named pipe.
10pub struct WindowsPipeConnector {
11    addr: OsString,
12    pub(crate) local: bool,
13}
14
15impl WindowsPipeConnector {
16    /// Creates a new connector for a non-local pipe using the given `addr`.
17    pub fn new(addr: impl Into<OsString>) -> Self {
18        Self {
19            addr: addr.into(),
20            local: false,
21        }
22    }
23
24    /// Creates a new connector for a local pipe using the given `name`.
25    pub fn local(name: impl Into<OsString>) -> Self {
26        Self {
27            addr: name.into(),
28            local: true,
29        }
30    }
31}
32
33impl<T: Into<OsString>> From<T> for WindowsPipeConnector {
34    fn from(addr: T) -> Self {
35        Self::new(addr)
36    }
37}
38
39#[async_trait]
40impl Connector for WindowsPipeConnector {
41    type Transport = WindowsPipeTransport;
42
43    async fn connect(self) -> io::Result<Self::Transport> {
44        if self.local {
45            let mut full_addr = OsString::from(r"\\.\pipe\");
46            full_addr.push(self.addr);
47            WindowsPipeTransport::connect(full_addr).await
48        } else {
49            WindowsPipeTransport::connect(self.addr).await
50        }
51    }
52}