| 1 |
// Copyright © 2021 Mozilla Foundation |
| 2 |
// |
| 3 |
// This program is made available under an ISC-style license. See the |
| 4 |
// accompanying file LICENSE for details |
| 5 |
|
| 6 |
use std::{ |
| 7 |
fs::OpenOptions, |
| 8 |
io::{Read, Write}, |
| 9 |
os::windows::prelude::{FromRawHandle, OpenOptionsExt}, |
| 10 |
sync::atomic::{AtomicUsize, Ordering}, |
| 11 |
}; |
| 12 |
|
| 13 |
use std::io::Result; |
| 14 |
|
| 15 |
use bytes::{Buf, BufMut}; |
| 16 |
use mio::windows::NamedPipe; |
| 17 |
use windows_sys::Win32::Storage::FileSystem::FILE_FLAG_OVERLAPPED; |
| 18 |
|
| 19 |
use crate::PlatformHandle; |
| 20 |
|
| 21 |
use super::{ConnectionBuffer, RecvMsg, SendMsg}; |
| 22 |
|
| 23 |
pub struct Pipe { |
| 24 |
pub(crate) io: NamedPipe, |
| 25 |
} |
| 26 |
|
| 27 |
// Create a connected "pipe" pair. The `Pipe` is the server end, |
| 28 |
// the `PlatformHandle` is the client end to be remoted. |
| 29 |
pub fn make_pipe_pair() -> Result<(Pipe, PlatformHandle)> { |
| 30 |
let pipe_name = get_pipe_name(); |
| 31 |
let server = NamedPipe::new(&pipe_name)?; |
| 32 |
|
| 33 |
let client = { |
| 34 |
let mut opts = OpenOptions::new(); |
| 35 |
opts.read(true) |
| 36 |
.write(true) |
| 37 |
.custom_flags(FILE_FLAG_OVERLAPPED); |
| 38 |
let file = opts.open(&pipe_name)?; |
| 39 |
PlatformHandle::from(file) |
| 40 |
}; |
| 41 |
|
| 42 |
Ok((Pipe::new(server), client)) |
| 43 |
} |
| 44 |
|
| 45 |
static PIPE_ID: AtomicUsize = AtomicUsize::new(0); |
| 46 |
|
| 47 |
fn get_pipe_name() -> String { |
| 48 |
let pid = std::process::id(); |
| 49 |
let pipe_id = PIPE_ID.fetch_add(1, Ordering::Relaxed); |
| 50 |
format!("\\\\.\\pipe\\LOCAL\\cubeb-pipe-{pid}-{pipe_id}") |
| 51 |
} |
| 52 |
|
| 53 |
impl Pipe { |
| 54 |
pub fn new(io: NamedPipe) -> Self { |
| 55 |
Self { io } |
| 56 |
} |
| 57 |
|
| 58 |
#[allow(clippy::missing_safety_doc)] |
| 59 |
pub unsafe fn from_raw_handle(handle: crate::PlatformHandle) -> Pipe { |
| 60 |
Pipe::new(NamedPipe::from_raw_handle(handle.into_raw())) |
| 61 |
} |
| 62 |
|
| 63 |
pub fn shutdown(&mut self) -> Result<()> { |
| 64 |
self.io.disconnect() |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
impl RecvMsg for Pipe { |
| 69 |
// Receive data from the associated connection. `recv_msg` expects the capacity of |
| 70 |
// the `ConnectionBuffer` members has been adjusted appropriate by the caller. |
| 71 |
fn recv_msg(&mut self, buf: &mut ConnectionBuffer) -> Result<usize> { |
| 72 |
assert!(buf.buf.remaining_mut() > 0); |
| 73 |
let r = unsafe { |
| 74 |
let chunk = buf.buf.chunk_mut(); |
| 75 |
let slice = std::slice::from_raw_parts_mut(chunk.as_mut_ptr(), chunk.len()); |
| 76 |
self.io.read(slice) |
| 77 |
}; |
| 78 |
match r { |
| 79 |
Ok(n) => unsafe { |
| 80 |
buf.buf.advance_mut(n); |
| 81 |
Ok(n) |
| 82 |
}, |
| 83 |
e => e, |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
impl SendMsg for Pipe { |
| 89 |
// Send data on the associated connection. `send_msg` adjusts the length of the |
| 90 |
// `ConnectionBuffer` members based on the size of the successful send operation. |
| 91 |
fn send_msg(&mut self, buf: &mut ConnectionBuffer) -> Result<usize> { |
| 92 |
assert!(!buf.buf.is_empty()); |
| 93 |
let r = self.io.write(&buf.buf[..buf.buf.len()]); |
| 94 |
if let Ok(n) = r { |
| 95 |
buf.buf.advance(n); |
| 96 |
while let Some(mut handle) = buf.pop_handle() { |
| 97 |
handle.mark_sent() |
| 98 |
} |
| 99 |
} |
| 100 |
r |
| 101 |
} |
| 102 |
} |