Skip to content

Commit

Permalink
switch some apis to use polling
Browse files Browse the repository at this point in the history
  • Loading branch information
youyuanwu committed May 19, 2024
1 parent 791f294 commit 9eb9a8f
Show file tree
Hide file tree
Showing 7 changed files with 721 additions and 83 deletions.
87 changes: 87 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/libs/msquic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ edition = "2021"
tokio = {version = "1", features = ["sync"]}
tracing = { version = "0.1.40", features = ["log"] }
bytes = "*"
h3 = "*"

[dev-dependencies]
# env_logger = "0.10.1"
Expand Down
59 changes: 36 additions & 23 deletions crates/libs/msquic/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,22 +144,21 @@ impl From<&SBuffer> for Buffer {
}
}

pub struct QBufWrap<B: Buf> {
_inner: Box<B>, // mem owner
pub struct QBufWrap {
_inner: Box<dyn Buf>, // mem owner
v: Vec<Buffer>,
}

unsafe impl<B: Buf> Send for QBufWrap<B> {}
unsafe impl Send for QBufWrap {}

impl<B: Buf> QBufWrap<B> {
pub fn new(buf: B) -> Self {
impl QBufWrap {
pub fn new(mut buf: Box<dyn Buf>) -> Self {
// make on heap so that no ptr move.
let mut inner = Box::new(buf);
let v = Self::convert_buf(&mut inner);
Self { _inner: inner, v }
let v = Self::convert_buf(&mut buf);
Self { _inner: buf, v }
}

fn convert_buf(b: &mut impl Buf) -> Vec<Buffer> {
fn convert_buf(b: &mut Box<dyn Buf>) -> Vec<Buffer> {
let mut v = Vec::new();
// change buf to vecs
while b.has_remaining() {
Expand Down Expand Up @@ -188,32 +187,36 @@ impl QBytesMut {
let mut res = BytesMut::new();
b.iter().for_each(|i| {
let s = unsafe { slice::from_raw_parts(i.buffer, i.length.try_into().unwrap()) };
res.reserve(s.len());
res.put_slice(s);
});
Self(res)
}
}

pub fn debug_buf_to_string(mut b: impl Buf) -> String {
let cp = b.copy_to_bytes(b.remaining());
String::from_utf8_lossy(&cp).into_owned()
let mut dst = vec![0; b.remaining()];
b.copy_to_slice(&mut dst[..]);
// let cp = b.copy_to_bytes(b.remaining());
String::from_utf8_lossy(&dst).into_owned()
}

pub fn debug_raw_buf_to_string(b: Buffer) -> String {
let s = String::from_utf8_lossy(unsafe {
slice::from_raw_parts(b.buffer, b.length.try_into().unwrap())
});
s.into_owned()
}

#[cfg(test)]
mod test {
use core::slice;

use bytes::{BufMut, Bytes, BytesMut};
use c2::Buffer;

use super::{QBufWrap, QBuffRef, QBufferVec, QVecBuffer};
use crate::buffer::debug_raw_buf_to_string;

fn buf_to_string(b: Buffer) -> String {
let s = String::from_utf8_lossy(unsafe {
slice::from_raw_parts(b.buffer, b.length.try_into().unwrap())
});
s.into_owned()
}
use super::{debug_buf_to_string, QBufWrap, QBuffRef, QBufferVec, QBytesMut, QVecBuffer};

#[test]
fn test_vec_buffer() {
Expand All @@ -239,11 +242,11 @@ mod test {
#[test]
fn test_buf() {
let b = Bytes::from("mydata");
let wrap = QBufWrap::new(b);
let wrap = QBufWrap::new(Box::new(b));
let v = wrap.as_buffs();
assert_eq!(v.len(), 1);
let b1 = v[0];
let s = buf_to_string(b1);
let s = debug_raw_buf_to_string(b1);
assert_eq!(s, "mydata");
}

Expand All @@ -252,11 +255,21 @@ mod test {
let mut b = BytesMut::with_capacity(5);
b.put(&b"hello"[..]);
b.put(&b"world"[..]); // this will grow
let wrap = QBufWrap::new(b);
let wrap = QBufWrap::new(Box::new(b));
let v = wrap.as_buffs();
assert_eq!(v.len(), 1);
let b1 = v[0];
let s = buf_to_string(b1);
let s = debug_raw_buf_to_string(b1);
assert_eq!(s, "helloworld");
}

#[test]
fn test_buf2str() {
let args: [QVecBuffer; 2] = [QVecBuffer::from("hello"), QVecBuffer::from("world")];
let buffer_vec = QBufferVec::from(args.as_slice());
let bm = QBytesMut::from_buffs(buffer_vec.as_buffers());
std::mem::drop(args);
let s = debug_buf_to_string(bm.0);
assert_eq!(s, "helloworld");
}
}
2 changes: 2 additions & 0 deletions crates/libs/msquic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ pub mod stream;
pub mod sync;
mod utils;

//pub mod msh3;

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

0 comments on commit 9eb9a8f

Please sign in to comment.