|
| 1 | +//! Generic QUIC support |
| 2 | +
|
| 3 | +use std::pin::Pin; |
| 4 | +use std::task::{Context, Poll}; |
| 5 | + |
| 6 | +use bytes::Buf; |
| 7 | + |
| 8 | +// TODO: Should this be gated by an `http3` feature? |
| 9 | + |
| 10 | +/// A QUIC connection. |
| 11 | +pub trait Connection<B> { |
| 12 | + /// Send streams that can be opened by this connection. |
| 13 | + type SendStream: SendStream<B>; |
| 14 | + /// Receive streams that can be accepted by this connection. |
| 15 | + type RecvStream: RecvStream; |
| 16 | + /// Bidirectional streams that can be opened or accepted by this connection. |
| 17 | + type BidiStream: SendStream<B> + RecvStream; |
| 18 | + /// Errors that may occur opening or accepting streams. |
| 19 | + type Error; |
| 20 | + |
| 21 | + // Accepting streams |
| 22 | + |
| 23 | + // Q: shorten to bidi? |
| 24 | + /// Accept a bidirection stream. |
| 25 | + fn poll_accept_bidirectional_stream( |
| 26 | + self: Pin<&mut Self>, |
| 27 | + cx: &mut Context<'_>, |
| 28 | + ) -> Poll<Result<Option<Self::BidiStream>, Self::Error>>; |
| 29 | + |
| 30 | + /// Accept a unidirectional receive stream. |
| 31 | + fn poll_accept_recv_stream( |
| 32 | + self: Pin<&mut Self>, |
| 33 | + cx: &mut Context<'_>, |
| 34 | + ) -> Poll<Result<Option<Self::RecvStream>, Self::Error>>; |
| 35 | + |
| 36 | + // Creating streams |
| 37 | + |
| 38 | + // Q: shorten to bidi? |
| 39 | + /// Open a bidirectional stream. |
| 40 | + fn poll_open_bidirectional_stream( |
| 41 | + self: Pin<&mut Self>, |
| 42 | + cx: &mut Context<'_>, |
| 43 | + ) -> Poll<Result<Self::BidiStream, Self::Error>>; |
| 44 | + |
| 45 | + /// Open a unidirectional send stream. |
| 46 | + fn poll_open_send_stream( |
| 47 | + self: Pin<&mut Self>, |
| 48 | + cx: &mut Context<'_>, |
| 49 | + ) -> Poll<Result<Self::SendStream, Self::Error>>; |
| 50 | +} |
| 51 | + |
| 52 | +/// The send portion of a QUIC stream. |
| 53 | +pub trait SendStream<B> { |
| 54 | + /// Errors that may happen trying to send data. |
| 55 | + type Error; // bounds? |
| 56 | + /// Polls that the stream is ready to send more data. |
| 57 | + // Q: Should this be Pin<&mut Self>? |
| 58 | + fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>>; |
| 59 | + /// Send data on the stream. |
| 60 | + fn send_data(&mut self, data: B) -> Result<(), Self::Error>; |
| 61 | + // fn poll_flush? |
| 62 | + /// finish? |
| 63 | + fn poll_finish(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>>; |
| 64 | + /// Close the stream with an error code. |
| 65 | + fn reset(&mut self, reset_code: u64); |
| 66 | +} |
| 67 | + |
| 68 | +/// The receive portion of a QUIC stream. |
| 69 | +pub trait RecvStream { |
| 70 | + /// Buffers of data that can be received. |
| 71 | + type Buf: Buf; |
| 72 | + /// Errors that may be received. |
| 73 | + type Error; // bounds? |
| 74 | + |
| 75 | + // Q: should this be Pin? |
| 76 | + /// Poll for more data received from the remote on this stream. |
| 77 | + fn poll_data(&mut self, cx: &mut Context<'_>) -> Poll<Result<Option<Self::Buf>, Self::Error>>; |
| 78 | + /// Signal to the remote peer to stop sending data. |
| 79 | + fn stop_sending(&mut self, error_code: u64); |
| 80 | +} |
| 81 | + |
| 82 | +/// An optional trait if a QUIC stream can be split into two sides. |
| 83 | +pub trait BidiStream<B>: SendStream<B> + RecvStream { |
| 84 | + /// The send side of a stream. |
| 85 | + type SendStream: SendStream<B>; |
| 86 | + /// The receive side of a stream. |
| 87 | + type RecvStream: RecvStream; |
| 88 | + |
| 89 | + /// Split this stream into two sides. |
| 90 | + fn split(self) -> (Self::SendStream, Self::RecvStream); |
| 91 | +} |
0 commit comments