Skip to content

Commit

Permalink
consolidate shared quic trait methods and distinguish open errors fro…
Browse files Browse the repository at this point in the history
…m accept errors
  • Loading branch information
FlorianUekermann committed Mar 16, 2023
1 parent da29aea commit bd44fcf
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 51 deletions.
48 changes: 27 additions & 21 deletions h3-quinn/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,14 @@ impl<B> quic::Connection<B> for Connection
where
B: Buf,
{
type SendStream = SendStream<B>;
type RecvStream = RecvStream;
type BidiStream = BidiStream<B>;
type OpenStreams = OpenStreams;
type Error = ConnectionError;
type AcceptError = ConnectionError;

fn poll_accept_bidi(
&mut self,
cx: &mut task::Context<'_>,
) -> Poll<Result<Option<Self::BidiStream>, Self::Error>> {
) -> Poll<Result<Option<Self::BidiStream>, Self::AcceptError>> {
let (send, recv) = match ready!(self.incoming_bi.next().poll_unpin(cx)) {
Some(x) => x?,
None => return Poll::Ready(Ok(None)),
Expand All @@ -118,33 +116,50 @@ where
fn poll_accept_recv(
&mut self,
cx: &mut task::Context<'_>,
) -> Poll<Result<Option<Self::RecvStream>, Self::Error>> {
) -> Poll<Result<Option<Self::RecvStream>, Self::AcceptError>> {
let recv = match ready!(self.incoming_uni.poll_next_unpin(cx)) {
Some(x) => x?,
None => return Poll::Ready(Ok(None)),
};
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<B> quic::OpenStreams<B> for Connection
where
B: Buf,
{
type SendStream = SendStream<B>;
type BidiStream = BidiStream<B>;
type OpenError = ConnectionError;

fn poll_open_bidi(
&mut self,
cx: &mut task::Context<'_>,
) -> Poll<Result<Self::BidiStream, Self::Error>> {
) -> Poll<Result<Self::BidiStream, Self::OpenError>> {
if self.opening_bi.is_none() {
self.opening_bi = Some(self.conn.open_bi());
}

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<Result<Self::SendStream, Self::Error>> {
) -> Poll<Result<Self::SendStream, Self::OpenError>> {
if self.opening_uni.is_none() {
self.opening_uni = Some(self.conn.open_uni());
}
Expand All @@ -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"),
Expand All @@ -183,30 +190,29 @@ impl<B> quic::OpenStreams<B> for OpenStreams
where
B: Buf,
{
type RecvStream = RecvStream;
type SendStream = SendStream<B>;
type BidiStream = BidiStream<B>;
type Error = ConnectionError;
type OpenError = ConnectionError;

fn poll_open_bidi(
&mut self,
cx: &mut task::Context<'_>,
) -> Poll<Result<Self::BidiStream, Self::Error>> {
) -> Poll<Result<Self::BidiStream, Self::OpenError>> {
if self.opening_bi.is_none() {
self.opening_bi = Some(self.conn.open_bi());
}

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<Result<Self::SendStream, Self::Error>> {
) -> Poll<Result<Self::SendStream, Self::OpenError>> {
if self.opening_uni.is_none() {
self.opening_uni = Some(self.conn.open_uni());
}
Expand Down
37 changes: 8 additions & 29 deletions h3/src/quic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,51 +30,32 @@ impl<'a, E: Error + 'a> From<E> for Box<dyn Error + 'a> {
}

/// Trait representing a QUIC connection.
pub trait Connection<B: Buf> {
/// The type produced by `poll_accept_bidi()`
type BidiStream: SendStream<B> + RecvStream;
/// The type of the sending part of `BidiStream`
type SendStream: SendStream<B>;
pub trait Connection<B: Buf>: OpenStreams<B> {
/// The type produced by `poll_accept_recv()`
type RecvStream: RecvStream;
/// A producer of outgoing Unidirectional and Bidirectional streams.
type OpenStreams: OpenStreams<B>;
/// Error type yielded by this trait methods
type Error: Into<Box<dyn Error>>;
/// Error type yielded by these trait methods
type AcceptError: Into<Box<dyn Error>>;

/// Accept an incoming unidirectional stream
///
/// Returning `None` implies the connection is closing or closed.
fn poll_accept_recv(
&mut self,
cx: &mut task::Context<'_>,
) -> Poll<Result<Option<Self::RecvStream>, Self::Error>>;
) -> Poll<Result<Option<Self::RecvStream>, Self::AcceptError>>;

/// Accept an incoming bidirectional stream
///
/// Returning `None` implies the connection is closing or closed.
fn poll_accept_bidi(
&mut self,
cx: &mut task::Context<'_>,
) -> Poll<Result<Option<Self::BidiStream>, Self::Error>>;

/// Poll the connection to create a new bidirectional stream.
fn poll_open_bidi(
&mut self,
cx: &mut task::Context<'_>,
) -> Poll<Result<Self::BidiStream, Self::Error>>;

/// Poll the connection to create a new unidirectional stream.
fn poll_open_send(
&mut self,
cx: &mut task::Context<'_>,
) -> Poll<Result<Self::SendStream, Self::Error>>;
) -> Poll<Result<Option<Self::BidiStream>, 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
Expand All @@ -83,22 +64,20 @@ pub trait OpenStreams<B: Buf> {
type BidiStream: SendStream<B> + RecvStream;
/// The type produced by `poll_open_send()`
type SendStream: SendStream<B>;
/// The type of the receiving part of `BidiStream`
type RecvStream: RecvStream;
/// Error type yielded by these trait methods
type Error: Into<Box<dyn Error>>;
type OpenError: Into<Box<dyn Error>>;

/// Poll the connection to create a new bidirectional stream.
fn poll_open_bidi(
&mut self,
cx: &mut task::Context<'_>,
) -> Poll<Result<Self::BidiStream, Self::Error>>;
) -> Poll<Result<Self::BidiStream, Self::OpenError>>;

/// Poll the connection to create a new unidirectional stream.
fn poll_open_send(
&mut self,
cx: &mut task::Context<'_>,
) -> Poll<Result<Self::SendStream, Self::Error>>;
) -> Poll<Result<Self::SendStream, Self::OpenError>>;

/// Close the connection immediately
fn close(&mut self, code: crate::error::Code, reason: &[u8]);
Expand Down
2 changes: 1 addition & 1 deletion h3/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
//! async fn doc<C>(conn: C)
//! where
//! C: h3::quic::Connection<bytes::Bytes>,
//! <C as h3::quic::Connection<bytes::Bytes>>::BidiStream: Send + 'static
//! <C as h3::quic::OpenStreams<bytes::Bytes>>::BidiStream: Send + 'static
//! {
//! let mut server_builder = h3::server::builder();
//! // Build the Connection
Expand Down

0 comments on commit bd44fcf

Please sign in to comment.