|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +use crate::{constants::APPLICATION_NAME, util::errors::CodeError}; |
| 7 | +use std::path::{Path, PathBuf}; |
| 8 | +use uuid::Uuid; |
| 9 | + |
| 10 | +// todo: we could probably abstract this into some crate, if one doesn't already exist |
| 11 | + |
| 12 | +cfg_if::cfg_if! { |
| 13 | + if #[cfg(unix)] { |
| 14 | + pub type AsyncPipe = tokio::net::UnixStream; |
| 15 | + pub type AsyncPipeWriteHalf = tokio::net::unix::OwnedWriteHalf; |
| 16 | + pub type AsyncPipeReadHalf = tokio::net::unix::OwnedReadHalf; |
| 17 | + |
| 18 | + pub async fn get_socket_rw_stream(path: &Path) -> Result<AsyncPipe, CodeError> { |
| 19 | + tokio::net::UnixStream::connect(path) |
| 20 | + .await |
| 21 | + .map_err(CodeError::AsyncPipeFailed) |
| 22 | + } |
| 23 | + |
| 24 | + pub async fn listen_socket_rw_stream(path: &Path) -> Result<AsyncPipeListener, CodeError> { |
| 25 | + tokio::net::UnixListener::bind(path) |
| 26 | + .map(AsyncPipeListener) |
| 27 | + .map_err(CodeError::AsyncPipeListenerFailed) |
| 28 | + } |
| 29 | + |
| 30 | + pub struct AsyncPipeListener(tokio::net::UnixListener); |
| 31 | + |
| 32 | + impl AsyncPipeListener { |
| 33 | + pub async fn accept(&mut self) -> Result<AsyncPipe, CodeError> { |
| 34 | + self.0.accept().await.map_err(CodeError::AsyncPipeListenerFailed).map(|(s, _)| s) |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + pub fn socket_stream_split(pipe: AsyncPipe) -> (AsyncPipeReadHalf, AsyncPipeWriteHalf) { |
| 39 | + pipe.into_split() |
| 40 | + } |
| 41 | + } else { |
| 42 | + use tokio::{time::sleep, io::{AsyncRead, AsyncWrite, ReadBuf}}; |
| 43 | + use tokio::net::windows::named_pipe::{ClientOptions, ServerOptions, NamedPipeClient, NamedPipeServer}; |
| 44 | + use std::{time::Duration, pin::Pin, task::{Context, Poll}, io}; |
| 45 | + use pin_project::pin_project; |
| 46 | + |
| 47 | + #[pin_project(project = AsyncPipeProj)] |
| 48 | + pub enum AsyncPipe { |
| 49 | + PipeClient(#[pin] NamedPipeClient), |
| 50 | + PipeServer(#[pin] NamedPipeServer), |
| 51 | + } |
| 52 | + |
| 53 | + impl AsyncRead for AsyncPipe { |
| 54 | + fn poll_read( |
| 55 | + self: Pin<&mut Self>, |
| 56 | + cx: &mut Context<'_>, |
| 57 | + buf: &mut ReadBuf<'_>, |
| 58 | + ) -> Poll<io::Result<()>> { |
| 59 | + match self.project() { |
| 60 | + AsyncPipeProj::PipeClient(c) => c.poll_read(cx, buf), |
| 61 | + AsyncPipeProj::PipeServer(c) => c.poll_read(cx, buf), |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + impl AsyncWrite for AsyncPipe { |
| 67 | + fn poll_write( |
| 68 | + self: Pin<&mut Self>, |
| 69 | + cx: &mut Context<'_>, |
| 70 | + buf: &[u8], |
| 71 | + ) -> Poll<io::Result<usize>> { |
| 72 | + match self.project() { |
| 73 | + AsyncPipeProj::PipeClient(c) => c.poll_write(cx, buf), |
| 74 | + AsyncPipeProj::PipeServer(c) => c.poll_write(cx, buf), |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + fn poll_write_vectored( |
| 79 | + self: Pin<&mut Self>, |
| 80 | + cx: &mut Context<'_>, |
| 81 | + bufs: &[io::IoSlice<'_>], |
| 82 | + ) -> Poll<Result<usize, io::Error>> { |
| 83 | + match self.project() { |
| 84 | + AsyncPipeProj::PipeClient(c) => c.poll_write_vectored(cx, bufs), |
| 85 | + AsyncPipeProj::PipeServer(c) => c.poll_write_vectored(cx, bufs), |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> { |
| 90 | + match self.project() { |
| 91 | + AsyncPipeProj::PipeClient(c) => c.poll_flush(cx), |
| 92 | + AsyncPipeProj::PipeServer(c) => c.poll_flush(cx), |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + fn is_write_vectored(&self) -> bool { |
| 97 | + match self { |
| 98 | + AsyncPipe::PipeClient(c) => c.is_write_vectored(), |
| 99 | + AsyncPipe::PipeServer(c) => c.is_write_vectored(), |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> { |
| 104 | + match self.project() { |
| 105 | + AsyncPipeProj::PipeClient(c) => c.poll_shutdown(cx), |
| 106 | + AsyncPipeProj::PipeServer(c) => c.poll_shutdown(cx), |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + pub type AsyncPipeWriteHalf = tokio::io::WriteHalf<AsyncPipe>; |
| 112 | + pub type AsyncPipeReadHalf = tokio::io::ReadHalf<AsyncPipe>; |
| 113 | + |
| 114 | + pub async fn get_socket_rw_stream(path: &Path) -> Result<AsyncPipe, CodeError> { |
| 115 | + // Tokio says we can need to try in a loop. Do so. |
| 116 | + // https://docs.rs/tokio/latest/tokio/net/windows/named_pipe/struct.NamedPipeClient.html |
| 117 | + let client = loop { |
| 118 | + match ClientOptions::new().open(path) { |
| 119 | + Ok(client) => break client, |
| 120 | + // ERROR_PIPE_BUSY https://docs.microsoft.com/en-us/windows/win32/debug/system-error-codes--0-499- |
| 121 | + Err(e) if e.raw_os_error() == Some(231) => sleep(Duration::from_millis(100)).await, |
| 122 | + Err(e) => return Err(CodeError::AsyncPipeFailed(e)), |
| 123 | + } |
| 124 | + }; |
| 125 | + |
| 126 | + Ok(AsyncPipe::PipeClient(client)) |
| 127 | + } |
| 128 | + |
| 129 | + pub struct AsyncPipeListener { |
| 130 | + path: PathBuf, |
| 131 | + server: NamedPipeServer |
| 132 | + } |
| 133 | + |
| 134 | + impl AsyncPipeListener { |
| 135 | + pub async fn accept(&mut self) -> Result<AsyncPipe, CodeError> { |
| 136 | + // see https://docs.rs/tokio/latest/tokio/net/windows/named_pipe/struct.NamedPipeServer.html |
| 137 | + // this is a bit weird in that the server becomes the client once |
| 138 | + // they get a connection, and we create a new client. |
| 139 | + |
| 140 | + self.server |
| 141 | + .connect() |
| 142 | + .await |
| 143 | + .map_err(CodeError::AsyncPipeListenerFailed)?; |
| 144 | + |
| 145 | + // Construct the next server to be connected before sending the one |
| 146 | + // we already have of onto a task. This ensures that the server |
| 147 | + // isn't closed (after it's done in the task) before a new one is |
| 148 | + // available. Otherwise the client might error with |
| 149 | + // `io::ErrorKind::NotFound`. |
| 150 | + let next_server = ServerOptions::new() |
| 151 | + .create(&self.path) |
| 152 | + .map_err(CodeError::AsyncPipeListenerFailed)?; |
| 153 | + |
| 154 | + |
| 155 | + Ok(AsyncPipe::PipeServer(std::mem::replace(&mut self.server, next_server))) |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + pub async fn listen_socket_rw_stream(path: &Path) -> Result<AsyncPipeListener, CodeError> { |
| 160 | + let server = ServerOptions::new() |
| 161 | + .first_pipe_instance(true) |
| 162 | + .create(path) |
| 163 | + .map_err(CodeError::AsyncPipeListenerFailed)?; |
| 164 | + |
| 165 | + Ok(AsyncPipeListener { path: path.to_owned(), server }) |
| 166 | + } |
| 167 | + |
| 168 | + pub fn socket_stream_split(pipe: AsyncPipe) -> (AsyncPipeReadHalf, AsyncPipeWriteHalf) { |
| 169 | + tokio::io::split(pipe) |
| 170 | + } |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +/// Gets a random name for a pipe/socket on the paltform |
| 175 | +pub fn get_socket_name() -> PathBuf { |
| 176 | + cfg_if::cfg_if! { |
| 177 | + if #[cfg(unix)] { |
| 178 | + std::env::temp_dir().join(format!("{}-{}", APPLICATION_NAME, Uuid::new_v4())) |
| 179 | + } else { |
| 180 | + PathBuf::from(format!(r"\\.\pipe\{}-{}", APPLICATION_NAME, Uuid::new_v4())) |
| 181 | + } |
| 182 | + } |
| 183 | +} |
0 commit comments