From 793bd17cc15c53e80cbb14c9303c54012702598f Mon Sep 17 00:00:00 2001 From: Florian Uekermann Date: Sat, 1 Jun 2024 09:13:53 +0200 Subject: [PATCH] consolidate shared quic trait methods and distinguish open errors from accept errors (#173) Co-authored-by: ruben --- h3-quinn/src/lib.rs | 48 ++++++++++++++++++++--------------- h3-webtransport/src/server.rs | 4 +-- h3/src/quic.rs | 44 +++++++------------------------- h3/src/server/mod.rs | 2 +- 4 files changed, 39 insertions(+), 59 deletions(-) diff --git a/h3-quinn/src/lib.rs b/h3-quinn/src/lib.rs index daec2057..7819b383 100644 --- a/h3-quinn/src/lib.rs +++ b/h3-quinn/src/lib.rs @@ -151,16 +151,14 @@ impl quic::Connection for Connection where B: Buf, { - type SendStream = SendStream; type RecvStream = RecvStream; - type BidiStream = BidiStream; type OpenStreams = OpenStreams; - type Error = ConnectionError; + type AcceptError = ConnectionError; fn poll_accept_bidi( &mut self, cx: &mut task::Context<'_>, - ) -> Poll, Self::Error>> { + ) -> Poll, Self::AcceptError>> { let (send, recv) = match ready!(self.incoming_bi.poll_next_unpin(cx)) { Some(x) => x?, None => return Poll::Ready(Ok(None)), @@ -174,7 +172,7 @@ where fn poll_accept_recv( &mut self, cx: &mut task::Context<'_>, - ) -> Poll, Self::Error>> { + ) -> Poll, Self::AcceptError>> { let recv = match ready!(self.incoming_uni.poll_next_unpin(cx)) { Some(x) => x?, None => return Poll::Ready(Ok(None)), @@ -182,10 +180,27 @@ where Poll::Ready(Ok(Some(Self::RecvStream::new(recv)))) } + fn opener(&self) -> Self::OpenStreams { + OpenStreams { + conn: self.conn.clone(), + opening_bi: None, + opening_uni: None, + } + } +} + +impl quic::OpenStreams for Connection +where + B: Buf, +{ + type SendStream = SendStream; + type BidiStream = BidiStream; + type OpenError = ConnectionError; + fn poll_open_bidi( &mut self, cx: &mut task::Context<'_>, - ) -> Poll> { + ) -> Poll> { if self.opening_bi.is_none() { self.opening_bi = Some(Box::pin(stream::unfold(self.conn.clone(), |conn| async { Some((conn.clone().open_bi().await, conn)) @@ -196,14 +211,14 @@ where ready!(self.opening_bi.as_mut().unwrap().poll_next_unpin(cx)).unwrap()?; Poll::Ready(Ok(Self::BidiStream { send: Self::SendStream::new(send), - recv: Self::RecvStream::new(recv), + recv: RecvStream::new(recv), })) } fn poll_open_send( &mut self, cx: &mut task::Context<'_>, - ) -> Poll> { + ) -> Poll> { if self.opening_uni.is_none() { self.opening_uni = Some(Box::pin(stream::unfold(self.conn.clone(), |conn| async { Some((conn.open_uni().await, conn)) @@ -214,14 +229,6 @@ where Poll::Ready(Ok(Self::SendStream::new(send))) } - fn opener(&self) -> Self::OpenStreams { - OpenStreams { - conn: self.conn.clone(), - opening_bi: None, - opening_uni: None, - } - } - fn close(&mut self, code: h3::error::Code, reason: &[u8]) { self.conn.close( VarInt::from_u64(code.value()).expect("error code VarInt"), @@ -278,15 +285,14 @@ impl quic::OpenStreams for OpenStreams where B: Buf, { - type RecvStream = RecvStream; type SendStream = SendStream; type BidiStream = BidiStream; - type Error = ConnectionError; + type OpenError = ConnectionError; fn poll_open_bidi( &mut self, cx: &mut task::Context<'_>, - ) -> Poll> { + ) -> Poll> { if self.opening_bi.is_none() { self.opening_bi = Some(Box::pin(stream::unfold(self.conn.clone(), |conn| async { Some((conn.open_bi().await, conn)) @@ -297,14 +303,14 @@ where ready!(self.opening_bi.as_mut().unwrap().poll_next_unpin(cx)).unwrap()?; Poll::Ready(Ok(Self::BidiStream { send: Self::SendStream::new(send), - recv: Self::RecvStream::new(recv), + recv: RecvStream::new(recv), })) } fn poll_open_send( &mut self, cx: &mut task::Context<'_>, - ) -> Poll> { + ) -> Poll> { if self.opening_uni.is_none() { self.opening_uni = Some(Box::pin(stream::unfold(self.conn.clone(), |conn| async { Some((conn.open_uni().await, conn)) diff --git a/h3-webtransport/src/server.rs b/h3-webtransport/src/server.rs index 435963e9..05970b61 100644 --- a/h3-webtransport/src/server.rs +++ b/h3-webtransport/src/server.rs @@ -251,13 +251,13 @@ where /// Streams are opened, but the initial webtransport header has not been sent type PendingStreams = ( - BidiStream<>::BidiStream, B>, + BidiStream<>::BidiStream, B>, WriteBuf<&'static [u8]>, ); /// Streams are opened, but the initial webtransport header has not been sent type PendingUniStreams = ( - SendStream<>::SendStream, B>, + SendStream<>::SendStream, B>, WriteBuf<&'static [u8]>, ); diff --git a/h3/src/quic.rs b/h3/src/quic.rs index bb382a33..6e8722cb 100644 --- a/h3/src/quic.rs +++ b/h3/src/quic.rs @@ -31,22 +31,13 @@ impl<'a, E: Error + 'a> From for Box { } /// Trait representing a QUIC connection. -pub trait Connection { - /// The type produced by `poll_accept_bidi()` - type BidiStream: SendStream + RecvStream; - /// The type of the sending part of `BidiStream` - type SendStream: SendStream; +pub trait Connection: OpenStreams { /// The type produced by `poll_accept_recv()` type RecvStream: RecvStream; /// A producer of outgoing Unidirectional and Bidirectional streams. - type OpenStreams: OpenStreams< - B, - SendStream = Self::SendStream, - RecvStream = Self::RecvStream, - BidiStream = Self::BidiStream, - >; - /// Error type yielded by this trait methods - type Error: Into>; + type OpenStreams: OpenStreams; + /// Error type yielded by these trait methods + type AcceptError: Into>; /// Accept an incoming unidirectional stream /// @@ -54,7 +45,7 @@ pub trait Connection { fn poll_accept_recv( &mut self, cx: &mut task::Context<'_>, - ) -> Poll, Self::Error>>; + ) -> Poll, Self::AcceptError>>; /// Accept an incoming bidirectional stream /// @@ -62,25 +53,10 @@ pub trait Connection { fn poll_accept_bidi( &mut self, cx: &mut task::Context<'_>, - ) -> Poll, Self::Error>>; - - /// Poll the connection to create a new bidirectional stream. - fn poll_open_bidi( - &mut self, - cx: &mut task::Context<'_>, - ) -> Poll>; - - /// Poll the connection to create a new unidirectional stream. - fn poll_open_send( - &mut self, - cx: &mut task::Context<'_>, - ) -> Poll>; + ) -> Poll, Self::AcceptError>>; /// Get an object to open outgoing streams. fn opener(&self) -> Self::OpenStreams; - - /// Close the connection immediately - fn close(&mut self, code: crate::error::Code, reason: &[u8]); } /// Extends the `Connection` trait for sending datagrams @@ -116,22 +92,20 @@ pub trait OpenStreams { type BidiStream: SendStream + RecvStream; /// The type produced by `poll_open_send()` type SendStream: SendStream; - /// The type of the receiving part of `BidiStream` - type RecvStream: RecvStream; /// Error type yielded by these trait methods - type Error: Into>; + type OpenError: Into>; /// Poll the connection to create a new bidirectional stream. fn poll_open_bidi( &mut self, cx: &mut task::Context<'_>, - ) -> Poll>; + ) -> Poll>; /// Poll the connection to create a new unidirectional stream. fn poll_open_send( &mut self, cx: &mut task::Context<'_>, - ) -> Poll>; + ) -> Poll>; /// Close the connection immediately fn close(&mut self, code: crate::error::Code, reason: &[u8]); diff --git a/h3/src/server/mod.rs b/h3/src/server/mod.rs index f9d62764..a4663d5b 100644 --- a/h3/src/server/mod.rs +++ b/h3/src/server/mod.rs @@ -9,7 +9,7 @@ //! async fn doc(conn: C) //! where //! C: h3::quic::Connection, -//! >::BidiStream: Send + 'static +//! >::BidiStream: Send + 'static //! { //! let mut server_builder = h3::server::builder(); //! // Build the Connection