Skip to content

Commit

Permalink
use dynamic load
Browse files Browse the repository at this point in the history
  • Loading branch information
youyuanwu committed Dec 8, 2024
1 parent 0651e61 commit 77dbb33
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 34 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/libs/msquic-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ edition = "2021"
windows-core = "0.57.0"
windows = { version = "0.57.0", features = [ "Win32_Foundation", "Win32_Networking_WinSock"]}
mssf-pal = "0"
libloading = "0.8"
lazy_static = "1"

[features]
Foundation = []
Expand Down
16 changes: 0 additions & 16 deletions crates/libs/msquic-rs/src/Microsoft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,22 +157,6 @@ pub mod MsQuic {
}
}
}
#[inline]
pub unsafe fn MsQuicClose(quicapi: *const core::ffi::c_void) {
#[link(name = "msquic")]
extern "system" {
pub fn MsQuicClose(quicapi: *const core::ffi::c_void);
}
MsQuicClose(quicapi)
}
#[inline]
pub unsafe fn MsQuicOpenVersion(version: u32, quicapi: *mut *mut core::ffi::c_void) -> i32 {
#[link(name = "msquic")]
extern "system" {
pub fn MsQuicOpenVersion(version: u32, quicapi: *mut *mut core::ffi::c_void) -> i32;
}
MsQuicOpenVersion(version, quicapi)
}
pub const QUIC_ALLOWED_CIPHER_SUITE_AES_128_GCM_SHA256: QUIC_ALLOWED_CIPHER_SUITE_FLAGS =
QUIC_ALLOWED_CIPHER_SUITE_FLAGS(1i32);
pub const QUIC_ALLOWED_CIPHER_SUITE_AES_256_GCM_SHA384: QUIC_ALLOWED_CIPHER_SUITE_FLAGS =
Expand Down
3 changes: 3 additions & 0 deletions crates/libs/msquic-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ pub use win_status::*;
mod unix_status;
#[cfg(target_os = "linux")]
pub use unix_status::*;

mod load;
pub use load::{MsQuicClose, MsQuicOpenVersion};
15 changes: 15 additions & 0 deletions crates/libs/msquic-rs/src/load.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//! Dynamically load shared lib
//!
//!
use crate::Microsoft::MsQuic::QUIC_API_TABLE;

const LIB_NAME: &str = "msquic.dll";

lazy_static::lazy_static! {
static ref LIB: libloading::Library = unsafe { libloading::Library::new(LIB_NAME) }.expect("cannot load msquic shared lib");
pub static ref MsQuicOpenVersion: libloading::Symbol<'static,unsafe extern "C" fn(u32, *mut *mut QUIC_API_TABLE) -> i32>
= unsafe { LIB.get(b"MsQuicOpenVersion") }.expect("cannot load open fn");
pub static ref MsQuicClose: libloading::Symbol<'static, unsafe extern "C" fn(*const QUIC_API_TABLE)>
= unsafe { LIB.get(b"MsQuicClose") }.expect("cannot load open fn");
}
30 changes: 13 additions & 17 deletions crates/samples/simple/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
use std::{ffi::c_void, sync::Arc};
use std::sync::Arc;

use msquic_rs::{
Microsoft::MsQuic::{
MsQuicClose, MsQuicOpenVersion, HQUIC, QUIC_API_TABLE, QUIC_API_VERSION_2,
QUIC_EXECUTION_PROFILE, QUIC_EXECUTION_PROFILE_LOW_LATENCY,
QUIC_EXECUTION_PROFILE_TYPE_MAX_THROUGHPUT, QUIC_EXECUTION_PROFILE_TYPE_REAL_TIME,
QUIC_EXECUTION_PROFILE_TYPE_SCAVENGER, QUIC_HANDLE, QUIC_REGISTRATION_CONFIG,
HQUIC, QUIC_API_TABLE, QUIC_API_VERSION_2, QUIC_EXECUTION_PROFILE,
QUIC_EXECUTION_PROFILE_LOW_LATENCY, QUIC_EXECUTION_PROFILE_TYPE_MAX_THROUGHPUT,
QUIC_EXECUTION_PROFILE_TYPE_REAL_TIME, QUIC_EXECUTION_PROFILE_TYPE_SCAVENGER, QUIC_HANDLE,
QUIC_REGISTRATION_CONFIG,
},
QStatus,
MsQuicClose, MsQuicOpenVersion, QStatus,
};
use windows_core::PCSTR;

struct ApiInner {
inner: *const QUIC_API_TABLE,
inner: *mut QUIC_API_TABLE,
}

unsafe impl Send for ApiInner {}
Expand All @@ -21,17 +21,13 @@ unsafe impl Sync for ApiInner {}
impl ApiInner {
pub fn new() -> Self {
let mut ret = ApiInner {
inner: std::ptr::null(),
inner: std::ptr::null_mut(),
};

let ec = unsafe {
MsQuicOpenVersion(
QUIC_API_VERSION_2,
std::ptr::addr_of_mut!(ret.inner) as *mut *mut c_void,
)
};
let ec =
unsafe { MsQuicOpenVersion(QUIC_API_VERSION_2, std::ptr::addr_of_mut!(ret.inner)) };
QStatus::from_raw(ec).unwrap();
assert_ne!(ret.inner, std::ptr::null());
assert_ne!(ret.inner, std::ptr::null_mut());
ret
}

Expand All @@ -45,8 +41,8 @@ impl Drop for ApiInner {
if self.inner.is_null() {
return;
}
unsafe { MsQuicClose(self.inner as *const c_void) };
self.inner = std::ptr::null();
unsafe { MsQuicClose(self.inner) };
self.inner = std::ptr::null_mut();
}
}

Expand Down
4 changes: 3 additions & 1 deletion crates/tools/api/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ fn main() -> Result<()> {
"--in",
"./.windows/winmd/Microsoft.MsQuic.winmd",
"--out",
"crates/libs/msquic-sys/src/Microsoft.rs",
"crates/libs/msquic-rs/src/Microsoft.rs",
"--filter",
"Microsoft",
"!Microsoft.MsQuic.MsQuicOpenVersion", // Skip functions that needs linking
"!Microsoft.MsQuic.MsQuicClose",
"--config",
"implement",
])?;
Expand Down

0 comments on commit 77dbb33

Please sign in to comment.