Skip to content

Commit

Permalink
streams: method access underlying quinn stream behind feature
Browse files Browse the repository at this point in the history
  • Loading branch information
BiagioFesta committed Sep 23, 2023
1 parent 1211168 commit cd89dc3
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions wtransport/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }
[features]
default = []
dangerous-configuration = ["rustls/dangerous_configuration"]
quinn = []

[package.metadata.docs.rs]
all-features = true
Expand Down
24 changes: 24 additions & 0 deletions wtransport/src/driver/streams/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@ impl QuicSendStream {
pub fn id(&self) -> StreamId {
streamid_q2w(self.0.id())
}

#[cfg(feature = "quinn")]
#[inline(always)]
pub fn quic_stream(&self) -> &quinn::SendStream {
&self.0
}

#[cfg(feature = "quinn")]
#[inline(always)]
pub fn quic_stream_mut(&mut self) -> &mut quinn::SendStream {
&mut self.0
}
}

impl wtransport_proto::bytes::AsyncWrite for QuicSendStream {
Expand Down Expand Up @@ -162,6 +174,18 @@ impl QuicRecvStream {
pub fn id(&self) -> StreamId {
streamid_q2w(self.0.id())
}

#[cfg(feature = "quinn")]
#[inline(always)]
pub fn quic_stream(&self) -> &quinn::RecvStream {
&self.0
}

#[cfg(feature = "quinn")]
#[inline(always)]
pub fn quic_stream_mut(&mut self) -> &mut quinn::RecvStream {
&mut self.0
}
}

impl wtransport_proto::bytes::AsyncRead for QuicRecvStream {
Expand Down
32 changes: 32 additions & 0 deletions wtransport/src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,22 @@ impl SendStream {
pub async fn stopped(mut self) -> StreamWriteError {
self.0.stopped().await
}

/// Returns a reference to the underlying QUIC stream.
#[cfg(feature = "quinn")]
#[cfg_attr(docsrs, doc(cfg(feature = "quinn")))]
#[inline(always)]
pub fn quic_stream(&self) -> &quinn::SendStream {
self.0.quic_stream()
}

/// Returns a mutable reference to the underlying QUIC stream.
#[cfg(feature = "quinn")]
#[cfg_attr(docsrs, doc(cfg(feature = "quinn")))]
#[inline(always)]
pub fn quic_stream_mut(&mut self) -> &mut quinn::SendStream {
self.0.quic_stream_mut()
}
}

/// A stream that can only be used to receive data.
Expand All @@ -117,6 +133,22 @@ impl RecvStream {
pub fn id(&self) -> StreamId {
self.0.id()
}

/// Returns a reference to the underlying QUIC stream.
#[cfg(feature = "quinn")]
#[cfg_attr(docsrs, doc(cfg(feature = "quinn")))]
#[inline(always)]
pub fn quic_stream(&self) -> &quinn::RecvStream {
self.0.quic_stream()
}

/// Returns a mutable reference to the underlying QUIC stream.
#[cfg(feature = "quinn")]
#[cfg_attr(docsrs, doc(cfg(feature = "quinn")))]
#[inline(always)]
pub fn quic_stream_mut(&mut self) -> &mut quinn::RecvStream {
self.0.quic_stream_mut()
}
}

impl tokio::io::AsyncWrite for SendStream {
Expand Down

2 comments on commit cd89dc3

@MOZGIII
Copy link
Contributor

@MOZGIII MOZGIII commented on cd89dc3 Oct 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is awesome! Should I be able to just use these quinn::RecvStream/quinn::SendStream instead of the wrapped ones? I am asking because they expose some nicer lower level API, and I have a code that has to multiplex between using quinn/wtransport which currently uses AsyncWrite/AsyncRead - but with this I could switch to using more optimal lower-level stuff!

As I understand, the WebTransport itself does not do anything to the individual streams (i.e. no handshake per stream) - but there is a certain protocol-level communication happening over a hidden QUIC stream. Thus using the QUIC streams obtained through this API should just work.

@BiagioFesta
Copy link
Owner Author

@BiagioFesta BiagioFesta commented on cd89dc3 Oct 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes.

WebTransport protocol requires some "initialization" logic on the QUIC streams. That part is internally handled in the core-engine of wtransport.
That means quic_stream and quic_stream_mut methods are totally safe from the protocol point of view, and accessing the raw quic stream is ok for objects SendStream and RecvStream.

This should provide some flexibility for underlying API, such requested here: #41

On the other hand, I don't want to bind to a specific implementation (in this case quinn), as the QUIC backend might change in the future of wtransport (e.g., powered by multiple backend), and that's why those method are behind a feature.

Please sign in to comment.