Skip to content

Commit ffb0953

Browse files
committed
Add QUIC traits
This adds a `quic` module with a few traits, which can be used to abstract over the QUIC transport.
1 parent 5bc3067 commit ffb0953

File tree

3 files changed

+109
-1
lines changed

3 files changed

+109
-1
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ authors = ["Sean McArthur <[email protected]>"]
55
edition = "2018"
66

77
[dependencies]
8+
bytes = "0.5"

src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
1+
#[deny(missing_docs)]
2+
pub mod quic;

src/quic.rs

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
//! QUIC Transport traits
2+
//!
3+
//! This module includes traits and types meant to allow being generic over any
4+
//! QUIC implementation.
5+
6+
use std::task::{self, Poll};
7+
8+
use bytes::Buf;
9+
10+
// Unresolved questions:
11+
//
12+
// - Should the `poll_` methods be `Pin<&mut Self>`?
13+
14+
/// Trait representing a QUIC connection.
15+
pub trait Connection<B: Buf> {
16+
/// The stream type returned when opening a unidirectional send stream.
17+
type SendStream: SendStream<B>;
18+
/// The stream type returned when accepting a unidirectional receive stream.
19+
type RecvStream: RecvStream;
20+
/// The stream type returned when accepting or opening a bidirectional
21+
/// stream.
22+
type BidiStream: SendStream<B> + RecvStream;
23+
/// The error type that can be returned when accepting or opening a stream.
24+
type Error;
25+
26+
// Accepting streams
27+
28+
/// Poll the connection for any received bidirectional streams.
29+
///
30+
/// Returning `None` implies the connection is closing or closed.
31+
fn poll_accept_bidi_stream(
32+
&mut self,
33+
cx: &mut task::Context<'_>,
34+
) -> Poll<Result<Option<Self::BidiStream>, Self::Error>>;
35+
36+
/// Poll the connection for any received unidirectional streams.
37+
///
38+
/// Returning `None` implies the connection is closing or closed.
39+
fn poll_accept_recv_stream(
40+
&mut self,
41+
cx: &mut task::Context<'_>,
42+
) -> Poll<Result<Option<Self::RecvStream>, Self::Error>>;
43+
44+
// Opening streams
45+
46+
/// Poll the connection to create a new bidirectional stream.
47+
fn poll_open_bidi_stream(
48+
&mut self,
49+
cx: &mut task::Context<'_>,
50+
) -> Poll<Result<Self::BidiStream, Self::Error>>;
51+
52+
/// Poll the connection to create a new unidirectional stream.
53+
fn poll_open_send_stream(
54+
&mut self,
55+
cx: &mut task::Context<'_>,
56+
) -> Poll<Result<Self::SendStream, Self::Error>>;
57+
}
58+
59+
/// A trait describing the "send" actions of a QUIC stream.
60+
pub trait SendStream<B: Buf> {
61+
/// The error type returned by fallible send methods.
62+
type Error; // bounds?
63+
64+
/// Polls if the stream can send more data.
65+
fn poll_ready(&mut self, cx: &mut task::Context<'_>) -> Poll<Result<(), Self::Error>>;
66+
67+
/// Send more data on the stream.
68+
fn send_data(&mut self, data: B) -> Result<(), Self::Error>;
69+
70+
/// Poll to finish the sending side of the stream.
71+
fn poll_finish(&mut self) -> Poll<Result<(), Self::Error>>;
72+
73+
/// Send a QUIC reset code.
74+
fn reset(&mut self, reset_code: u64);
75+
}
76+
77+
/// A trait describing the "receive" actions of a QUIC stream.
78+
pub trait RecvStream {
79+
/// The type of `Buf` for data received on this stream.
80+
type Buf: Buf;
81+
/// The error type that can occur when receiving data.
82+
type Error; // bounds?
83+
84+
/// Poll the stream for more data.
85+
///
86+
/// When the receive side will no longer receive more data (such as because
87+
/// the peer closed their sending side), this should return `None`.
88+
fn poll_data(
89+
&mut self,
90+
cx: &mut task::Context<'_>,
91+
) -> Poll<Result<Option<Self::Buf>, Self::Error>>;
92+
93+
/// Send a `STOP_SENDING` QUIC code.
94+
fn stop_sending(&mut self, error_code: u64);
95+
}
96+
97+
/// Optional trait to allow "splitting" a bidirectional stream into two sides.
98+
pub trait BidiStream<B: Buf>: SendStream<B> + RecvStream {
99+
/// The type for the send half.
100+
type SendStream: SendStream<B>;
101+
/// The type for the receive half.
102+
type RecvStream: RecvStream;
103+
104+
/// Split this stream into two halves.
105+
fn split(self) -> (Self::SendStream, Self::RecvStream);
106+
}

0 commit comments

Comments
 (0)