From bd44fcf311207adf644e32f367d16902565d018e Mon Sep 17 00:00:00 2001 From: Florian Uekermann Date: Sun, 12 Feb 2023 22:59:16 +0100 Subject: [PATCH] consolidate shared quic trait methods and distinguish open errors from accept errors --- h3-quinn/src/lib.rs | 48 +++++++++++++++++++++++++-------------------- h3/src/quic.rs | 37 ++++++++-------------------------- h3/src/server.rs | 2 +- 3 files changed, 36 insertions(+), 51 deletions(-) diff --git a/h3-quinn/src/lib.rs b/h3-quinn/src/lib.rs index 28862ddf..eb44cb89 100644 --- a/h3-quinn/src/lib.rs +++ b/h3-quinn/src/lib.rs @@ -95,16 +95,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.next().poll_unpin(cx)) { Some(x) => x?, None => return Poll::Ready(Ok(None)), @@ -118,7 +116,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)), @@ -126,10 +124,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(self.conn.open_bi()); } @@ -137,14 +152,14 @@ where let (send, recv) = ready!(self.opening_bi.as_mut().unwrap().poll_unpin(cx))?; 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(self.conn.open_uni()); } @@ -153,14 +168,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"), @@ -183,15 +190,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(self.conn.open_bi()); } @@ -199,14 +205,14 @@ where let (send, recv) = ready!(self.opening_bi.as_mut().unwrap().poll_unpin(cx))?; 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(self.conn.open_uni()); } diff --git a/h3/src/quic.rs b/h3/src/quic.rs index e4f0aaf0..d7203f7b 100644 --- a/h3/src/quic.rs +++ b/h3/src/quic.rs @@ -30,17 +30,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; - /// Error type yielded by this trait methods - type Error: Into>; + /// Error type yielded by these trait methods + type AcceptError: Into>; /// Accept an incoming unidirectional stream /// @@ -48,7 +44,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 /// @@ -56,25 +52,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]); } /// Trait for opening outgoing streams @@ -83,22 +64,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.rs b/h3/src/server.rs index 01edba76..6c43084f 100644 --- a/h3/src/server.rs +++ b/h3/src/server.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