Skip to content

Commit

Permalink
add readme and refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
youyuanwu committed Dec 7, 2024
1 parent abede7f commit bee073b
Show file tree
Hide file tree
Showing 18 changed files with 79 additions and 58 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.16)

project(msquic-asio)
project(msquic-asio LANGUAGES)

set(_msquic_version "v2.4.5")

Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# msquic-h3
Use [msquic](https://github.com/microsoft/msquic) in rust with [h3](https://github.com/hyperium/h3).

Experimental.

Currently can run h3 client and server.

# License
This project is licensed under the MIT license.
18 changes: 18 additions & 0 deletions crates/libs/msquic-h3/src/core/api.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use std::sync::Arc;

use msquic_sys2::Api;

use super::utils::SBox;

#[derive(Clone)]
pub struct QApi {
pub inner: Arc<SBox<Api>>,
}

impl Default for QApi {
fn default() -> Self {
Self {
inner: Arc::new(Api::new().into()),
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::slice;
use bytes::{Buf, BufMut, BytesMut};
use msquic_sys2::Buffer;

use crate::SBox;
use super::utils::SBox;

pub trait QOwnedBuffer {
fn as_buff_ref(&self) -> QBuffRef;
Expand Down Expand Up @@ -221,7 +221,7 @@ mod test {
use bytes::{BufMut, Bytes, BytesMut};
use msquic_sys2::Buffer;

use crate::buffer::debug_raw_buf_to_string;
use crate::core::buffer::debug_raw_buf_to_string;

use super::{debug_buf_to_string, QBufWrap, QBuffRef, QBufferVec, QBytesMut, QVecBuffer};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use std::sync::Arc;

use msquic_sys2::{Buffer, Configuration, CredentialConfig, Settings};

use crate::{reg::QRegistration, utils::SBox, QApi};
use crate::core::api::QApi;
use crate::core::{reg::QRegistration, utils::SBox};

#[derive(Clone)]
pub struct QConfiguration {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::{
config::QConfiguration,
reg::QRegistration,
stream::QStream,
sync::{QReceiver, QResetChannel, QWakableSig},
core::config::QConfiguration,
core::reg::QRegistration,
core::stream::QStream,
core::sync::{QReceiver, QResetChannel, QWakableSig},
};
use msquic_sys2::{
Configuration, Connection, ConnectionEvent, Handle, SendResumptionFlags,
Expand All @@ -13,7 +13,8 @@ use msquic_sys2::{
};
use std::{ffi::c_void, fmt::Debug, future::poll_fn, io::Error, sync::Mutex, task::Poll};

use crate::{utils::SBox, QApi};
use crate::core::api::QApi;
use crate::core::utils::SBox;

pub struct QConnection {
pub _api: QApi,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ use std::{
sync::{Arc, Mutex},
};

use crate::conn::QConnection;
use crate::core::conn::QConnection;
use msquic_sys2::{
Addr, Buffer, Configuration, Handle, Listener, ListenerEvent, LISTENER_EVENT_NEW_CONNECTION,
};

use crate::{config::QConfiguration, reg::QRegistration, utils::SBox, QApi};
use crate::core::{api::QApi, config::QConfiguration, reg::QRegistration, utils::SBox};

pub struct QListener {
_api: QApi,
Expand Down
11 changes: 11 additions & 0 deletions crates/libs/msquic-h3/src/core/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//! Provides msquic handle wrappings needed to use rust async.
pub mod api;
pub mod buffer;
pub mod config;
pub mod conn;
pub mod listener;
pub mod reg;
pub mod stream;
pub mod sync;
mod utils;
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::sync::Arc;

use msquic_sys2::{Registration, RegistrationConfig};

use crate::{utils::SBox, QApi};
use crate::core::{api::QApi, utils::SBox};

#[derive(Clone)]
pub struct QRegistration {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ use std::{
task::Poll,
};

use crate::{
use crate::core::{
api::QApi,
buffer::{QBufWrap, QBytesMut},
conn::QConnection,
sync::{QSignal, QWakableSig},
utils::SBox,
QApi, QUIC_STATUS_SUCCESS,
};
use crate::QUIC_STATUS_SUCCESS;
use bytes::{Buf, BytesMut};
use msquic_sys2::{
Buffer, Handle, SendFlags, Stream, StreamEvent, StreamOpenFlags, StreamShutdownFlags,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,14 +345,14 @@ mod tests {
time::Duration,
};

use crate::sync::QWakableQueue;
use crate::core::sync::QWakableQueue;

static COUNTER: AtomicUsize = AtomicUsize::new(0);
fn read_line(_cx: &mut Context<'_>) -> Poll<String> {
fn read_line(cx: &mut Context<'_>) -> Poll<String> {
println!("readline called");
// the second poll should work
if COUNTER.fetch_add(1, std::sync::atomic::Ordering::Acquire) < 1 {
_cx.waker().clone().wake();
cx.waker().clone().wake_by_ref();
Poll::Pending
} else {
Poll::Ready("Hello, World!".into())
Expand Down
File renamed without changes.
32 changes: 3 additions & 29 deletions crates/libs/msquic-h3/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,11 @@
use std::sync::Arc;

use msquic_sys2::Api;

use utils::SBox;

pub mod buffer;
pub mod config;
pub mod conn;
pub mod listener;
pub mod reg;
pub mod stream;
pub mod sync;
mod utils;
pub mod core;

pub mod msh3;

// Some useful defs
pub const QUIC_STATUS_PENDING: u32 = 0x703e5;
pub const QUIC_STATUS_SUCCESS: u32 = 0;

#[derive(Clone)]
pub struct QApi {
inner: Arc<SBox<Api>>,
}

impl Default for QApi {
fn default() -> Self {
Self {
inner: Arc::new(Api::new().into()),
}
}
}

// macro for enabling tracing for internals.
macro_rules! trace {
($($rest:tt)*) => {
Expand All @@ -55,14 +29,14 @@ mod tests {
};
use tokio::sync::oneshot;

use crate::{
use crate::core::{
api::QApi,
buffer::{debug_buf_to_string, QBufferVec, QVecBuffer},
config::QConfiguration,
conn::QConnection,
listener::QListener,
reg::QRegistration,
stream::QStream,
QApi,
};

use tracing::info;
Expand Down
8 changes: 3 additions & 5 deletions crates/libs/msquic-h3/src/msh3/mod.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
// h3 wrappings for msquic

use std::{fmt::Display, sync::Arc, task::Poll};
//! h3 wrappings for msquic
use crate::core::{conn::QConnection, stream::QStream};
use bytes::{Buf, BytesMut};
use h3::quic::{BidiStream, Connection, OpenStreams, RecvStream, SendStream};
use msquic_sys2::{
StreamOpenFlags, SEND_FLAG_NONE, STREAM_OPEN_FLAG_NONE, STREAM_OPEN_FLAG_UNIDIRECTIONAL,
STREAM_SHUTDOWN_FLAG_GRACEFUL,
};

use crate::{conn::QConnection, stream::QStream};
use std::{fmt::Display, sync::Arc, task::Poll};

#[derive(Debug)]
pub struct H3Error {
Expand Down
13 changes: 7 additions & 6 deletions crates/libs/msquic-h3/src/msh3/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,23 @@ use msquic_sys2::{
};
use tracing::info;

use crate::{
use crate::core::{
api::QApi,
buffer::{QBufferVec, QVecBuffer},
config::QConfiguration,
conn::QConnection,
msh3::H3Conn,
listener::QListener,
reg::QRegistration,
QApi,
};
use crate::msh3::H3Conn;

/// Send a GET request to target server.
async fn send_get_request(uri: Uri) {
let api = QApi::default();

let app_name = std::ffi::CString::new("testapp").unwrap();
let config = RegistrationConfig {
app_name: app_name.as_ptr() as *const i8,
app_name: app_name.as_ptr(),
execution_profile: EXECUTION_PROFILE_LOW_LATENCY,
};
let q_reg = QRegistration::new(&api, &config);
Expand Down Expand Up @@ -147,7 +148,7 @@ fn basic_server_test() {
let api = QApi::default();
let app_name = std::ffi::CString::new("testapp").unwrap();
let config = RegistrationConfig {
app_name: app_name.as_ptr() as *const i8,
app_name: app_name.as_ptr(),
execution_profile: EXECUTION_PROFILE_LOW_LATENCY,
};
let q_reg = QRegistration::new(&api, &config);
Expand Down Expand Up @@ -189,7 +190,7 @@ fn basic_server_test() {
let mut l;
{
let local_address = Addr::ipv4(ADDRESS_FAMILY_UNSPEC, 4568_u16.to_be(), 0);
l = crate::listener::QListener::open(&q_req_copy, &q_config);
l = QListener::open(&q_req_copy, &q_config);
info!("Start listener.");
let alpn = QBufferVec::from(args.as_slice());
l.start(alpn.as_buffers(), &local_address);
Expand Down
3 changes: 3 additions & 0 deletions crates/libs/msquic-sys/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# msuiqc-sys

Auto generated rust bindings for msquic.
4 changes: 4 additions & 0 deletions crates/libs/msquic-sys2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# msquic-sys3
Manually written wrappings copied from msquic repo.

This should eventually be replaced by msquic-sys.
2 changes: 1 addition & 1 deletion crates/tools/api/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fn main() -> Result<()> {
"--in",
"./.windows/winmd/Microsoft.MsQuic.winmd",
"--out",
"crates/libs/c/src/Microsoft.rs",
"crates/libs/msquic-sys/src/Microsoft.rs",
"--filter",
"Microsoft",
"--config",
Expand Down

0 comments on commit bee073b

Please sign in to comment.