1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
use crate::{
connection,
stream::{BidirectionalStream, PeerStream, ReceiveStream},
};
macro_rules! impl_accept_api {
() => {
/// Accepts an incoming [`PeerStream`](`crate::stream::PeerStream`)
///
/// The method will return
/// - `Ok(Some(stream)))` if a [`PeerStream`](`crate::stream::PeerStream`) was accepted
/// - `Ok(None)` if the connection was closed without an error
/// - `Err(stream_error)` if no stream could be accepted due to an error
///
/// # Examples
///
/// ```rust,no_run
/// # async fn test() -> s2n_quic::stream::Result<()> {
/// # let mut acceptor: s2n_quic::connection::StreamAcceptor = todo!();
/// #
/// while let Some(stream) = acceptor.accept().await? {
/// println!("Stream opened from {:?}", stream.connection().remote_addr());
/// }
/// #
/// # Ok(())
/// # }
/// ```
#[inline]
pub async fn accept(
&mut self,
) -> crate::connection::Result<Option<crate::stream::PeerStream>> {
futures::future::poll_fn(|cx| self.poll_accept(cx)).await
}
/// Poll for accepting an incoming [`PeerStream`](`crate::stream::PeerStream`)
///
/// The method will return
/// - `Poll::Ready(Ok(Some(stream)))` if a [`PeerStream`](`crate::stream::PeerStream`) was accepted
/// - `Poll::Ready(Ok(None))` if the connection was closed without an error
/// - `Poll::Ready(Err(stream_error))` if no stream could be accepted due to an error
/// - `Poll::Pending` if no new [`PeerStream`](`crate::stream::PeerStream`) was accepted by the connection yet.
/// In this case the caller must retry calling [`Self::poll_accept`].
/// For this purpose the method will save the [`core::task::Waker`]
/// which is provided as part of the [`core::task::Context`] parameter, and notify it
/// as soon as retrying the method will yield a different result.
#[inline]
pub fn poll_accept(
&mut self,
cx: &mut core::task::Context,
) -> core::task::Poll<crate::connection::Result<Option<crate::stream::PeerStream>>> {
use s2n_quic_core::stream::StreamType;
use $crate::stream::{BidirectionalStream, ReceiveStream};
Ok(
core::task::ready!(self.0.poll_accept(None, cx))?.map(|stream| {
match stream.id().stream_type() {
StreamType::Unidirectional => ReceiveStream::new(stream.into()).into(),
StreamType::Bidirectional => BidirectionalStream::new(stream).into(),
}
}),
)
.into()
}
impl_accept_bidirectional_api!();
impl_accept_receive_api!();
};
}
macro_rules! impl_accept_bidirectional_api {
() => {
/// Accepts an incoming [`BidirectionalStream`](`crate::stream::BidirectionalStream`)
///
/// The method will return
/// - `Ok(Some(stream)))` if a [`BidirectionalStream`](`crate::stream::BidirectionalStream`) was accepted
/// - `Ok(None)` if the connection was closed without an error
/// - `Err(stream_error)` if no stream could be accepted due to an error
///
/// # Examples
///
/// ```rust,no_run
/// # async fn test() -> s2n_quic::stream::Result<()> {
/// # let mut acceptor: s2n_quic::connection::StreamAcceptor = todo!();
/// #
/// while let Ok(Some(mut stream)) = acceptor.accept_bidirectional_stream().await {
/// println!("Stream opened from {:?}", stream.connection().remote_addr());
/// }
/// #
/// # Ok(())
/// # }
/// ```
#[inline]
pub async fn accept_bidirectional_stream(
&mut self,
) -> $crate::connection::Result<Option<$crate::stream::BidirectionalStream>> {
futures::future::poll_fn(|cx| self.poll_accept_bidirectional_stream(cx)).await
}
/// Poll for accepting an incoming [`BidirectionalStream`](`crate::stream::BidirectionalStream`)
///
/// The method will return
/// - `Poll::Ready(Ok(Some(stream)))` if a [`BidirectionalStream`](`crate::stream::BidirectionalStream`) was accepted
/// - `Poll::Ready(Ok(None))` if the connection was closed without an error
/// - `Poll::Ready(Err(stream_error))` if no stream could be accepted due to an error
/// - `Poll::Pending` if no new [`BidirectionalStream`](`crate::stream::BidirectionalStream`) was accepted by the connection yet.
/// In this case the caller must retry calling [`Self::poll_accept_bidirectional_stream`].
/// For this purpose the method will save the [`core::task::Waker`]
/// which is provided as part of the [`core::task::Context`] parameter, and notify it
/// as soon as retrying the method will yield a different result.
#[inline]
pub fn poll_accept_bidirectional_stream(
&mut self,
cx: &mut core::task::Context,
) -> core::task::Poll<$crate::connection::Result<Option<$crate::stream::BidirectionalStream>>> {
Ok(
core::task::ready!(self
.0
.poll_accept(Some(s2n_quic_core::stream::StreamType::Bidirectional), cx)
)?.map($crate::stream::BidirectionalStream::new)
).into()
}
};
}
macro_rules! impl_accept_receive_api {
() => {
/// Accepts an incoming [`ReceiveStream`](`crate::stream::ReceiveStream`)
///
/// The method will return
/// - `Ok(Some(stream)))` if a [`ReceiveStream`](`crate::stream::ReceiveStream`) was accepted
/// - `Ok(None)` if the connection was closed without an error
/// - `Err(stream_error)` if no stream could be accepted due to an error
///
/// # Examples
///
/// ```rust,no_run
/// # async fn test() -> s2n_quic::stream::Result<()> {
/// # let mut acceptor: s2n_quic::connection::StreamAcceptor = todo!();
/// #
/// while let Ok(Some(mut stream)) = acceptor.accept_receive_stream().await {
/// println!("Stream opened from {:?}", stream.connection().remote_addr());
/// }
/// #
/// # Ok(())
/// # }
/// ```
#[inline]
pub async fn accept_receive_stream(
&mut self,
) -> $crate::connection::Result<Option<$crate::stream::ReceiveStream>> {
futures::future::poll_fn(|cx| self.poll_accept_receive_stream(cx)).await
}
/// Poll for accepting an incoming [`ReceiveStream`](`crate::stream::ReceiveStream`)
///
/// The method will return
/// - `Poll::Ready(Ok(Some(stream)))` if a [`ReceiveStream`](`crate::stream::ReceiveStream`) was accepted
/// - `Poll::Ready(Ok(None))` if the connection was closed without an error
/// - `Poll::Ready(Err(stream_error))` if no stream could be accepted due to an error
/// - `Poll::Pending` if no new [`ReceiveStream`](`crate::stream::ReceiveStream`) was accepted by the connection yet.
/// In this case the caller must retry calling [`Self::poll_accept_receive_stream`].
/// For this purpose the method will save the [`core::task::Waker`]
/// which is provided as part of the [`core::task::Context`] parameter, and notify it
/// as soon as retrying the method will yield a different result.
#[inline]
pub fn poll_accept_receive_stream(
&mut self,
cx: &mut core::task::Context,
) -> core::task::Poll<$crate::connection::Result<Option<$crate::stream::ReceiveStream>>> {
Ok(core::task::ready!(self
.0
.poll_accept(Some(s2n_quic_core::stream::StreamType::Unidirectional), cx))?
.map(|stream| $crate::stream::ReceiveStream::new(stream.into())))
.into()
}
};
}
#[derive(Debug)]
pub struct StreamAcceptor(pub(crate) s2n_quic_transport::connection::Connection);
impl StreamAcceptor {
impl_accept_api!();
/// Splits the [`StreamAcceptor`] into [`BidirectionalStreamAcceptor`] and [`ReceiveStreamAcceptor`] halves
///
/// # Examples
///
/// ```rust,no_run
/// # use bytes::Bytes;
/// # async fn test() -> s2n_quic::stream::Result<()> {
/// # let connection: s2n_quic::connection::Connection = todo!();
/// #
/// let (handle, acceptor) = connection.split();
/// let (mut bidi, mut recv) = acceptor.split();
///
/// tokio::spawn(async move {
/// while let Ok(Some(mut stream)) = bidi.accept_bidirectional_stream().await {
/// println!("Bidirectional stream opened from {:?}", stream.connection().remote_addr());
/// }
/// });
///
/// tokio::spawn(async move {
/// while let Ok(Some(mut stream)) = recv.accept_receive_stream().await {
/// println!("Receive stream opened from {:?}", stream.connection().remote_addr());
/// }
/// });
/// #
/// # Ok(())
/// # }
/// ```
#[inline]
pub fn split(self) -> (BidirectionalStreamAcceptor, ReceiveStreamAcceptor) {
let bidi = BidirectionalStreamAcceptor(self.0.clone());
let recv = ReceiveStreamAcceptor(self.0);
(bidi, recv)
}
}
impl futures::stream::Stream for StreamAcceptor {
type Item = connection::Result<PeerStream>;
#[inline]
fn poll_next(
mut self: core::pin::Pin<&mut Self>,
cx: &mut core::task::Context<'_>,
) -> core::task::Poll<Option<Self::Item>> {
match core::task::ready!(self.poll_accept(cx)) {
Ok(Some(stream)) => Some(Ok(stream)),
Ok(None) => None,
Err(err) => Some(Err(err)),
}
.into()
}
}
#[derive(Debug)]
pub struct BidirectionalStreamAcceptor(s2n_quic_transport::connection::Connection);
impl BidirectionalStreamAcceptor {
impl_accept_bidirectional_api!();
}
impl futures::stream::Stream for BidirectionalStreamAcceptor {
type Item = connection::Result<BidirectionalStream>;
#[inline]
fn poll_next(
mut self: core::pin::Pin<&mut Self>,
cx: &mut core::task::Context<'_>,
) -> core::task::Poll<Option<Self::Item>> {
match core::task::ready!(self.poll_accept_bidirectional_stream(cx)) {
Ok(Some(stream)) => Some(Ok(stream)),
Ok(None) => None,
Err(err) => Some(Err(err)),
}
.into()
}
}
#[derive(Debug)]
pub struct ReceiveStreamAcceptor(s2n_quic_transport::connection::Connection);
impl ReceiveStreamAcceptor {
impl_accept_receive_api!();
}
impl futures::stream::Stream for ReceiveStreamAcceptor {
type Item = connection::Result<ReceiveStream>;
#[inline]
fn poll_next(
mut self: core::pin::Pin<&mut Self>,
cx: &mut core::task::Context<'_>,
) -> core::task::Poll<Option<Self::Item>> {
match core::task::ready!(self.poll_accept_receive_stream(cx)) {
Ok(Some(stream)) => Some(Ok(stream)),
Ok(None) => None,
Err(err) => Some(Err(err)),
}
.into()
}
}