Skip to content

Commit

Permalink
Switch to windows-sys.
Browse files Browse the repository at this point in the history
  • Loading branch information
kinetiknz committed Oct 25, 2023
1 parent 0b51291 commit ec6af6e
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 96 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
strategy:
fail-fast: false
matrix:
os: [ubuntu-20.04, windows-2019, macos-10.15]
os: [ubuntu-20.04, windows-2019, macos-12]
rust: [stable]
experimental: [false]
include:
Expand All @@ -21,7 +21,7 @@ jobs:
- os: windows-2019
rust: nightly
experimental: true
- os: macos-10.15
- os: macos-12
rust: nightly
experimental: true

Expand Down
15 changes: 11 additions & 4 deletions audioipc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ name = "audioipc2"
version = "0.5.0"
authors = [
"Matthew Gregan <[email protected]>",
"Dan Glastonbury <[email protected]>"
]
"Dan Glastonbury <[email protected]>",
]
license = "ISC"
description = "Remote Cubeb IPC"
edition = "2018"
Expand All @@ -18,7 +18,7 @@ log = "0.4"
serde = "1"
serde_derive = "1"
serde_bytes = "0.11"
mio = { version = "0.8", features = ["os-poll", "net", "os-ext"] }
mio = { version = "0.8", features = ["os-poll", "net", "os-ext"] }
slab = "0.4"
scopeguard = "1.1.0"
crossbeam-queue = "0.3"
Expand All @@ -34,7 +34,14 @@ version = "0.26.1"
default-features = false

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", features = ["combaseapi", "handleapi", "memoryapi", "objbase"] }
windows-sys = { version = "0.48", features = [
"Win32_Foundation",
"Win32_Security",
"Win32_Storage_FileSystem",
"Win32_System_Com",
"Win32_System_Memory",
"Win32_System_Threading",
] }

[target.'cfg(target_os = "android")'.dependencies]
ashmem = "0.1.2"
Expand Down
66 changes: 31 additions & 35 deletions audioipc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,8 @@ pub type PlatformHandleType = libc::c_int;
#[derive(Debug)]
pub struct PlatformHandle(PlatformHandleType);

#[cfg(unix)]
pub const INVALID_HANDLE_VALUE: PlatformHandleType = -1isize as PlatformHandleType;

#[cfg(windows)]
pub const INVALID_HANDLE_VALUE: PlatformHandleType = winapi::um::handleapi::INVALID_HANDLE_VALUE;

#[cfg(unix)]
fn valid_handle(handle: PlatformHandleType) -> bool {
handle >= 0
Expand Down Expand Up @@ -110,26 +106,31 @@ unsafe fn close_platform_handle(handle: PlatformHandleType) {
}

#[cfg(windows)]
unsafe fn close_platform_handle(handle: PlatformHandleType) {
winapi::um::handleapi::CloseHandle(handle);
}
use windows_sys::Win32::{
Foundation::{
CloseHandle, DuplicateHandle, DUPLICATE_CLOSE_SOURCE, DUPLICATE_SAME_ACCESS, FALSE,
S_FALSE, S_OK,
},
System::Com::{CoInitializeEx, COINIT_MULTITHREADED},
System::Threading::{GetCurrentProcess, OpenProcess, PROCESS_DUP_HANDLE},
};

#[cfg(windows)]
use winapi::shared::minwindef::{DWORD, FALSE};
#[cfg(windows)]
use winapi::um::{handleapi, processthreadsapi, winnt};
unsafe fn close_platform_handle(handle: PlatformHandleType) {
CloseHandle(handle as _);
}

// Duplicate `source_handle` to `target_pid`. Returns the value of the new handle inside the target process.
// If `target_pid` is `None`, `source_handle` is duplicated in the current process.
#[cfg(windows)]
pub(crate) unsafe fn duplicate_platform_handle(
source_handle: PlatformHandleType,
target_pid: Option<DWORD>,
target_pid: Option<u32>,
) -> Result<PlatformHandleType> {
let source_process = processthreadsapi::GetCurrentProcess();
let source_process = GetCurrentProcess();
let target_process = if let Some(pid) = target_pid {
let target = processthreadsapi::OpenProcess(winnt::PROCESS_DUP_HANDLE, FALSE, pid);
if !valid_handle(target) {
let target = OpenProcess(PROCESS_DUP_HANDLE, FALSE, pid);
if !valid_handle(target as _) {
return Err(std::io::Error::new(
std::io::ErrorKind::Other,
"invalid target process",
Expand All @@ -140,26 +141,26 @@ pub(crate) unsafe fn duplicate_platform_handle(
None
};

let mut target_handle = std::ptr::null_mut();
let ok = handleapi::DuplicateHandle(
let mut target_handle = windows_sys::Win32::Foundation::INVALID_HANDLE_VALUE;
let ok = DuplicateHandle(
source_process,
source_handle,
source_handle as _,
target_process.unwrap_or(source_process),
&mut target_handle,
0,
FALSE,
winnt::DUPLICATE_SAME_ACCESS,
DUPLICATE_SAME_ACCESS,
);
if let Some(target) = target_process {
handleapi::CloseHandle(target);
CloseHandle(target);
};
if ok == FALSE {
return Err(std::io::Error::new(
std::io::ErrorKind::Other,
"DuplicateHandle failed",
));
}
Ok(target_handle)
Ok(target_handle as _)
}

// Close `target_handle_to_close` inside target process `target_pid` using a
Expand All @@ -168,27 +169,26 @@ pub(crate) unsafe fn duplicate_platform_handle(
#[cfg(windows)]
pub(crate) unsafe fn close_target_handle(
target_handle_to_close: PlatformHandleType,
target_pid: DWORD,
target_pid: u32,
) -> Result<()> {
let target_process =
processthreadsapi::OpenProcess(winnt::PROCESS_DUP_HANDLE, FALSE, target_pid);
if !valid_handle(target_process) {
let target_process = OpenProcess(PROCESS_DUP_HANDLE, FALSE, target_pid);
if !valid_handle(target_process as _) {
return Err(std::io::Error::new(
std::io::ErrorKind::Other,
"invalid target process",
));
}

let ok = handleapi::DuplicateHandle(
let ok = DuplicateHandle(
target_process,
target_handle_to_close,
std::ptr::null_mut(),
target_handle_to_close as _,
0,
std::ptr::null_mut(),
0,
FALSE,
winnt::DUPLICATE_CLOSE_SOURCE,
DUPLICATE_CLOSE_SOURCE,
);
handleapi::CloseHandle(target_process);
CloseHandle(target_process);
if ok == FALSE {
return Err(std::io::Error::new(
std::io::ErrorKind::Other,
Expand All @@ -200,13 +200,9 @@ pub(crate) unsafe fn close_target_handle(

#[cfg(windows)]
pub fn server_platform_init() {
use winapi::shared::winerror;
use winapi::um::combaseapi;
use winapi::um::objbase;

unsafe {
let r = combaseapi::CoInitializeEx(std::ptr::null_mut(), objbase::COINIT_MULTITHREADED);
assert!(winerror::SUCCEEDED(r));
let r = CoInitializeEx(std::ptr::null_mut(), COINIT_MULTITHREADED);
assert!(r == S_OK || r == S_FALSE);
}
}

Expand Down
50 changes: 28 additions & 22 deletions audioipc/src/shm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,22 +244,22 @@ mod unix {

#[cfg(windows)]
mod windows {
use super::*;
use std::ptr;
use winapi::{
shared::{minwindef::DWORD, ntdef::HANDLE},
um::{
handleapi::CloseHandle,
memoryapi::{MapViewOfFile, UnmapViewOfFile, FILE_MAP_ALL_ACCESS},
winbase::CreateFileMappingA,
winnt::PAGE_READWRITE,

use windows_sys::Win32::{
Foundation::{CloseHandle, FALSE, INVALID_HANDLE_VALUE},
System::Memory::{
CreateFileMappingA, MapViewOfFile, UnmapViewOfFile, FILE_MAP_ALL_ACCESS,
MEMORYMAPPEDVIEW_HANDLE, PAGE_READWRITE,
},
};

use crate::INVALID_HANDLE_VALUE;
use crate::valid_handle;

use super::*;

pub struct SharedMem {
handle: HANDLE,
handle: MEMORYMAPPEDVIEW_HANDLE,
view: SharedMemView,
}

Expand All @@ -268,10 +268,10 @@ mod windows {
impl Drop for SharedMem {
fn drop(&mut self) {
unsafe {
let ok = UnmapViewOfFile(self.view.ptr);
assert_ne!(ok, 0);
let ok = UnmapViewOfFile(self.view.ptr as _);
assert_ne!(ok, FALSE);
let ok = CloseHandle(self.handle);
assert_ne!(ok, 0);
assert_ne!(ok, FALSE);
}
}
}
Expand All @@ -284,38 +284,44 @@ mod windows {
ptr::null_mut(),
PAGE_READWRITE,
(size as u64 >> 32).try_into().unwrap(),
(size as u64 & (DWORD::MAX as u64)).try_into().unwrap(),
(size as u64 & (u32::MAX as u64)).try_into().unwrap(),
ptr::null(),
);
if handle.is_null() {
if !valid_handle(handle as _) {
return Err(std::io::Error::last_os_error().into());
}

let ptr = MapViewOfFile(handle, FILE_MAP_ALL_ACCESS, 0, 0, size);
if ptr.is_null() {
if !valid_handle(ptr as _) {
return Err(std::io::Error::last_os_error().into());
}

Ok(SharedMem {
handle,
view: SharedMemView { ptr, size },
view: SharedMemView {
ptr: ptr as _,
size,
},
})
}
}

pub unsafe fn make_handle(&self) -> Result<PlatformHandle> {
PlatformHandle::duplicate(self.handle).map_err(|e| e.into())
PlatformHandle::duplicate(self.handle as _).map_err(|e| e.into())
}

pub unsafe fn from(handle: PlatformHandle, size: usize) -> Result<SharedMem> {
let handle = handle.into_raw();
let ptr = MapViewOfFile(handle, FILE_MAP_ALL_ACCESS, 0, 0, size);
if ptr.is_null() {
let ptr = MapViewOfFile(handle as _, FILE_MAP_ALL_ACCESS, 0, 0, size);
if !valid_handle(ptr as _) {
return Err(std::io::Error::last_os_error().into());
}
Ok(SharedMem {
handle,
view: SharedMemView { ptr, size },
handle: handle as _,
view: SharedMemView {
ptr: ptr as _,
size,
},
})
}

Expand Down
2 changes: 1 addition & 1 deletion audioipc/src/sys/unix/cmsg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ trait AsBytes {
impl<'a, T: Sized> AsBytes for &'a [T] {
fn as_bytes(&self) -> &[u8] {
// TODO: This should account for the alignment of T
let byte_count = self.len() * mem::size_of::<T>();
let byte_count = std::mem::size_of_val(*self);
unsafe { slice::from_raw_parts(self.as_ptr() as *const _, byte_count) }
}
}
Expand Down
2 changes: 1 addition & 1 deletion audioipc/src/sys/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use std::io::Result;

use bytes::{Buf, BufMut};
use mio::windows::NamedPipe;
use winapi::um::winbase::FILE_FLAG_OVERLAPPED;
use windows_sys::Win32::Storage::FileSystem::FILE_FLAG_OVERLAPPED;

use crate::PlatformHandle;

Expand Down
12 changes: 4 additions & 8 deletions client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,14 @@ name = "audioipc2-client"
version = "0.5.0"
authors = [
"Matthew Gregan <[email protected]>",
"Dan Glastonbury <[email protected]>"
]
"Dan Glastonbury <[email protected]>",
]
license = "ISC"
description = "Cubeb Backend for talking to remote cubeb server."
edition = "2018"

[dependencies]
audioipc = { package = "audioipc2", path="../audioipc" }
audioipc = { package = "audioipc2", path = "../audioipc" }
cubeb-backend = "0.10"
log = "0.4"

[dependencies.audio_thread_priority]
version = "0.26.1"
default-features = false
features = ["winapi"]
audio_thread_priority = "0.26"
6 changes: 1 addition & 5 deletions client/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,11 +264,7 @@ impl ContextOps for ClientContext {
assert_not_in_callback();
unsafe {
let coll = &mut *collection.as_ptr();
let mut devices = Vec::from_raw_parts(
coll.device as *mut ffi::cubeb_device_info,
coll.count,
coll.count,
);
let mut devices = Vec::from_raw_parts(coll.device, coll.count, coll.count);
for dev in &mut devices {
if !dev.device_id.is_null() {
let _ = CString::from_raw(dev.device_id as *mut _);
Expand Down
7 changes: 5 additions & 2 deletions ipctest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2018"

[dependencies]
audioipc = { package = "audioipc2", path = "../audioipc" }
audioipc-client= { package = "audioipc2-client", path = "../client" }
audioipc-client = { package = "audioipc2-client", path = "../client" }
audioipc-server = { package = "audioipc2-server", path = "../server" }
cubeb = "0.10.0"
env_logger = "0.9"
Expand All @@ -18,4 +18,7 @@ log = "0.4"
libc = "0.2"

[target.'cfg(windows)'.dependencies]
winapi = "0.3"
windows-sys = { version = "0.48", features = [
"Win32_Foundation",
"Win32_System_Threading",
] }
Loading

0 comments on commit ec6af6e

Please sign in to comment.