1use std::fmt::{Debug, Display};
7use std::sync::Arc;
8use std::task::{self, Poll};
9
10use bytes::Buf;
11
12use crate::error::Code;
13pub use crate::proto::stream::{InvalidStreamId, StreamId};
14pub use crate::stream::WriteBuf;
15
16#[derive(Clone)]
20pub enum ConnectionErrorIncoming {
21 ApplicationClose {
23 error_code: u64,
25 },
26 Timeout,
28 InternalError(String),
31 Undefined(Arc<dyn std::error::Error + Send + Sync>),
35}
36
37impl Debug for ConnectionErrorIncoming {
39 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
40 match self {
41 Self::ApplicationClose { error_code } => {
42 let error_code = Code::from(*error_code);
43 write!(f, "ApplicationClose({})", error_code)
44 }
45 Self::Timeout => write!(f, "Timeout"),
46 Self::InternalError(arg0) => f.debug_tuple("InternalError").field(arg0).finish(),
47 Self::Undefined(arg0) => f.debug_tuple("Undefined").field(arg0).finish(),
48 }
49 }
50}
51
52#[derive(Debug)]
57pub enum StreamErrorIncoming {
58 ConnectionErrorIncoming {
60 connection_error: ConnectionErrorIncoming,
62 },
63 StreamTerminated {
67 error_code: u64,
69 },
70 Unknown(Box<dyn std::error::Error + Send + Sync>),
75}
76
77impl std::error::Error for StreamErrorIncoming {}
78
79impl Display for StreamErrorIncoming {
80 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
81 match self {
83 StreamErrorIncoming::ConnectionErrorIncoming { connection_error } => {
84 write!(f, "ConnectionError: {}", connection_error)
85 }
86 StreamErrorIncoming::StreamTerminated { error_code } => {
87 let error_code = Code::from(*error_code);
88 write!(f, "StreamClosed: {}", error_code)
89 }
90 StreamErrorIncoming::Unknown(error) => write!(f, "Error undefined by h3: {}", error),
91 }
92 }
93}
94
95impl Display for ConnectionErrorIncoming {
96 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
97 match self {
99 ConnectionErrorIncoming::ApplicationClose { error_code } => {
100 let error_code = Code::from(*error_code);
101 write!(f, "ApplicationClose: {}", error_code)
102 }
103 ConnectionErrorIncoming::Timeout => write!(f, "Timeout"),
104 ConnectionErrorIncoming::InternalError(error) => {
105 write!(
106 f,
107 "InternalError in the quic trait implementation: {}",
108 error
109 )
110 }
111 ConnectionErrorIncoming::Undefined(error) => {
112 write!(f, "Error undefined by h3: {}", error)
113 }
114 }
115 }
116}
117
118impl std::error::Error for ConnectionErrorIncoming {}
119
120pub trait Connection<B: Buf>: OpenStreams<B> {
122 type RecvStream: RecvStream;
124 type OpenStreams: OpenStreams<B, SendStream = Self::SendStream, BidiStream = Self::BidiStream>;
126
127 fn poll_accept_recv(
131 &mut self,
132 cx: &mut task::Context<'_>,
133 ) -> Poll<Result<Self::RecvStream, ConnectionErrorIncoming>>;
134
135 fn poll_accept_bidi(
139 &mut self,
140 cx: &mut task::Context<'_>,
141 ) -> Poll<Result<Self::BidiStream, ConnectionErrorIncoming>>;
142
143 fn opener(&self) -> Self::OpenStreams;
145}
146
147pub trait OpenStreams<B: Buf> {
149 type BidiStream: SendStream<B> + RecvStream;
151 type SendStream: SendStream<B>;
153
154 fn poll_open_bidi(
156 &mut self,
157 cx: &mut task::Context<'_>,
158 ) -> Poll<Result<Self::BidiStream, StreamErrorIncoming>>;
159
160 fn poll_open_send(
162 &mut self,
163 cx: &mut task::Context<'_>,
164 ) -> Poll<Result<Self::SendStream, StreamErrorIncoming>>;
165
166 fn close(&mut self, code: crate::error::Code, reason: &[u8]);
168}
169
170pub trait SendStream<B: Buf> {
172 fn poll_ready(&mut self, cx: &mut task::Context<'_>) -> Poll<Result<(), StreamErrorIncoming>>;
174
175 fn send_data<T: Into<WriteBuf<B>>>(&mut self, data: T) -> Result<(), StreamErrorIncoming>;
177
178 fn poll_finish(&mut self, cx: &mut task::Context<'_>) -> Poll<Result<(), StreamErrorIncoming>>;
180
181 fn reset(&mut self, reset_code: u64);
183
184 fn send_id(&self) -> StreamId;
186}
187
188pub trait SendStreamUnframed<B: Buf>: SendStream<B> {
190 fn poll_send<D: Buf>(
196 &mut self,
197 cx: &mut task::Context<'_>,
198 buf: &mut D,
199 ) -> Poll<Result<usize, StreamErrorIncoming>>;
200}
201
202pub trait RecvStream {
204 type Buf: Buf;
206
207 fn poll_data(
212 &mut self,
213 cx: &mut task::Context<'_>,
214 ) -> Poll<Result<Option<Self::Buf>, StreamErrorIncoming>>;
215
216 fn stop_sending(&mut self, error_code: u64);
218
219 fn recv_id(&self) -> StreamId;
221}
222
223pub trait BidiStream<B: Buf>: SendStream<B> + RecvStream {
225 type SendStream: SendStream<B>;
227 type RecvStream: RecvStream;
229
230 fn split(self) -> (Self::SendStream, Self::RecvStream);
232}