1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
use super::{Interest, Ready, Reconnectable, Transport};
use async_trait::async_trait;
use std::{
    ffi::{OsStr, OsString},
    fmt, io,
};

mod pipe;
pub use pipe::NamedPipe;

/// Represents a [`Transport`] that leverages a named Windows pipe (client or server)
pub struct WindowsPipeTransport {
    pub(crate) addr: OsString,
    pub(crate) inner: NamedPipe,
}

impl WindowsPipeTransport {
    /// Establishes a connection to the pipe with the specified name, using the
    /// name for a local pipe address in the form of `\\.\pipe\my_pipe_name` where
    /// `my_pipe_name` is provided to this function
    pub async fn connect_local(name: impl AsRef<OsStr>) -> io::Result<Self> {
        let mut addr = OsString::from(r"\\.\pipe\");
        addr.push(name.as_ref());
        Self::connect(addr).await
    }

    /// Establishes a connection to the pipe at the specified address
    ///
    /// Address may be something like `\.\pipe\my_pipe_name`
    pub async fn connect(addr: impl Into<OsString>) -> io::Result<Self> {
        let addr = addr.into();
        let inner = NamedPipe::connect_as_client(&addr).await?;

        Ok(Self { addr, inner })
    }

    /// Returns the addr that the listener is bound to
    pub fn addr(&self) -> &OsStr {
        &self.addr
    }
}

impl fmt::Debug for WindowsPipeTransport {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("WindowsPipeTransport")
            .field("addr", &self.addr)
            .finish()
    }
}

#[async_trait]
impl Reconnectable for WindowsPipeTransport {
    async fn reconnect(&mut self) -> io::Result<()> {
        // We cannot reconnect from server-side
        if self.inner.is_server() {
            return Err(io::Error::from(io::ErrorKind::Unsupported));
        }

        self.inner = NamedPipe::connect_as_client(&self.addr).await?;
        Ok(())
    }
}

#[async_trait]
impl Transport for WindowsPipeTransport {
    fn try_read(&self, buf: &mut [u8]) -> io::Result<usize> {
        match &self.inner {
            NamedPipe::Client(x) => x.try_read(buf),
            NamedPipe::Server(x) => x.try_read(buf),
        }
    }

    fn try_write(&self, buf: &[u8]) -> io::Result<usize> {
        match &self.inner {
            NamedPipe::Client(x) => x.try_write(buf),
            NamedPipe::Server(x) => x.try_write(buf),
        }
    }

    async fn ready(&self, interest: Interest) -> io::Result<Ready> {
        match &self.inner {
            NamedPipe::Client(x) => x.ready(interest).await,
            NamedPipe::Server(x) => x.ready(interest).await,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::common::TransportExt;
    use test_log::test;
    use tokio::{
        net::windows::named_pipe::{NamedPipeServer, ServerOptions},
        sync::oneshot,
        task::JoinHandle,
    };

    async fn start_and_run_server(tx: oneshot::Sender<String>) -> io::Result<()> {
        let pipe = start_server(tx).await?;
        run_server(pipe).await
    }

    async fn start_server(tx: oneshot::Sender<String>) -> io::Result<NamedPipeServer> {
        // Generate a pipe address (not just a name)
        let addr = format!(r"\\.\pipe\test_pipe_{}", rand::random::<usize>());

        // Listen at the pipe
        let pipe = ServerOptions::new()
            .first_pipe_instance(true)
            .create(&addr)?;

        // Send the address back to our main test thread
        tx.send(addr)
            .map_err(|x| io::Error::new(io::ErrorKind::Other, x))?;

        Ok(pipe)
    }

    async fn run_server(pipe: NamedPipeServer) -> io::Result<()> {
        use tokio::io::{AsyncReadExt, AsyncWriteExt};

        // Get the connection
        let mut conn = {
            pipe.connect().await?;
            pipe
        };

        // Send some data to the connection (10 bytes)
        conn.write_all(b"hello conn").await?;

        // Receive some data from the connection (12 bytes)
        let mut buf: [u8; 12] = [0; 12];
        let _ = conn.read_exact(&mut buf).await?;
        assert_eq!(&buf, b"hello server");

        Ok(())
    }

    #[test(tokio::test)]
    async fn should_fail_to_connect_if_pipe_does_not_exist() {
        // Generate a pipe name
        let name = format!("test_pipe_{}", rand::random::<usize>());

        // Now this should fail as we're already bound to the name
        WindowsPipeTransport::connect_local(&name)
            .await
            .expect_err("Unexpectedly succeeded in connecting to missing pipe");
    }

    #[test(tokio::test)]
    async fn should_be_able_to_read_and_write_data() {
        let (tx, rx) = oneshot::channel();

        // Spawn a task that will wait for a connection, send data,
        // and receive data that it will return in the task
        let task: JoinHandle<io::Result<()>> = tokio::spawn(start_and_run_server(tx));

        // Wait for the server to be ready
        let address = rx.await.expect("Failed to get server address");

        // Connect to the pipe, send some bytes, and get some bytes
        let mut buf: [u8; 10] = [0; 10];

        let conn = WindowsPipeTransport::connect(&address)
            .await
            .expect("Conn failed to connect");
        conn.read_exact(&mut buf)
            .await
            .expect("Conn failed to read");
        assert_eq!(&buf, b"hello conn");

        conn.write_all(b"hello server")
            .await
            .expect("Conn failed to write");

        // Verify that the task has completed by waiting on it
        let _ = task.await.expect("Server task failed unexpectedly");
    }

    #[test(tokio::test)]
    #[ignore]
    async fn should_be_able_to_reconnect() {
        todo!();
    }
}