Skip to content

Commit

Permalink
get stream id param
Browse files Browse the repository at this point in the history
  • Loading branch information
youyuanwu committed Jun 1, 2024
1 parent 310da4d commit ae3fdc6
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 14 deletions.
16 changes: 16 additions & 0 deletions crates/libs/c2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ use libc::c_void;
use serde::{Deserialize, Serialize};
use std::convert::TryInto;
use std::fmt;
use std::mem::size_of;
use std::mem::size_of_val;
use std::option::Option;
use std::ptr;
#[macro_use]
Expand Down Expand Up @@ -1739,6 +1741,20 @@ impl Stream {
);
assert!(Status::succeeded(status), "Code: 0x{:x}", status);
}

// get stream id
pub fn get_id(&self) -> u64 {
let mut id: u64 = 0;
let size = size_of_val(&id) as u32;
let status = (unsafe { self.table.as_ref().unwrap().get_param })(
self.handle,
PARAM_STREAM_ID,
std::ptr::addr_of!(size) as *mut u32,
std::ptr::addr_of_mut!(id) as *mut c_void,
);
assert!(Status::succeeded(status), "Code: 0x{:x}", status);
id
}
}

impl Drop for Stream {
Expand Down
4 changes: 2 additions & 2 deletions crates/libs/msquic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ mod tests {
rth.spawn(async move {
info!("server accepted stream");
let mut s = s.unwrap();
info!("server stream receive");
info!("server stream {} receive", s.get_id());
let read = s.receive().await.unwrap();
let payload = debug_buf_to_string(read);
info!("server received len {}", payload.len());
Expand Down Expand Up @@ -224,7 +224,7 @@ mod tests {
info!("client stream start");
st.start(STREAM_START_FLAG_NONE).await.unwrap();
let args = Bytes::from("hello");
info!("client stream send");
info!("client stream {} send", st.get_id());
st.send(args, SEND_FLAG_FIN).await.unwrap();

info!("client stream receive");
Expand Down
27 changes: 15 additions & 12 deletions crates/libs/msquic/src/msh3/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::{fmt::Display, task::Poll};

use bytes::{Buf, BytesMut};
use c2::SEND_FLAG_NONE;
use c2::{SEND_FLAG_NONE, STREAM_OPEN_FLAG_NONE, STREAM_OPEN_FLAG_UNIDIRECTIONAL};
use h3::quic::{BidiStream, Connection, OpenStreams, RecvStream, SendStream, StreamId};

use crate::{conn::QConnection, stream::QStream};
Expand Down Expand Up @@ -58,18 +58,23 @@ impl<B: Buf> OpenStreams<B> for H3Conn {
&mut self,
_cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Result<Self::BidiStream, Self::Error>> {
todo!()
let s = QStream::open(&self._inner, STREAM_OPEN_FLAG_NONE);
// TODO: start?
Poll::Ready(Ok(H3Stream::new(s)))
}

fn poll_open_send(
&mut self,
_cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Result<Self::SendStream, Self::Error>> {
todo!()
let s = QStream::open(&self._inner, STREAM_OPEN_FLAG_UNIDIRECTIONAL);
// TODO: start?
Poll::Ready(Ok(H3Stream::new(s)))
}

fn close(&mut self, _code: h3::error::Code, _reason: &[u8]) {
todo!()
fn close(&mut self, code: h3::error::Code, _reason: &[u8]) {
// TODO?
self._inner.shutdown_only(code.value())
}
}

Expand Down Expand Up @@ -123,15 +128,13 @@ impl<B: Buf> Connection<B> for H3Conn {

pub struct H3Stream {
inner: QStream,
id: h3::quic::StreamId,
shutdown: bool,
}

impl H3Stream {
fn new(s: QStream, id: StreamId) -> Self {
fn new(s: QStream) -> Self {
Self {
inner: s,
id,
shutdown: false,
}
}
Expand Down Expand Up @@ -175,7 +178,7 @@ impl<B: Buf> SendStream<B> for H3Stream {
}

fn send_id(&self) -> h3::quic::StreamId {
self.id
self.inner.get_id().try_into().expect("cannot convert id")
}
}

Expand All @@ -200,7 +203,8 @@ impl RecvStream for H3Stream {
}

fn recv_id(&self) -> h3::quic::StreamId {
self.id
let id = self.inner.get_id();
id.try_into().expect("invalid stream id")
}
}

Expand All @@ -211,7 +215,6 @@ impl<B: Buf> BidiStream<B> for H3Stream {

fn split(self) -> (Self::SendStream, Self::RecvStream) {
let cp = self.inner.clone();
let id = self.id;
(self, H3Stream::new(cp, id))
(self, H3Stream::new(cp))
}
}
5 changes: 5 additions & 0 deletions crates/libs/msquic/src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,4 +364,9 @@ impl QStream {
}
rx.await;
}

// get stream id
pub fn get_id(&self) -> u64 {
self.inner.inner.get_id()
}
}

0 comments on commit ae3fdc6

Please sign in to comment.