s2n_quic/connection.rs
1// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2// SPDX-License-Identifier: Apache-2.0
3
4use core::fmt;
5use s2n_quic_transport::connection::Connection as Inner;
6
7#[macro_use]
8mod acceptor;
9#[macro_use]
10mod handle;
11
12pub use acceptor::*;
13pub use handle::*;
14pub use s2n_quic_core::connection::Error;
15
16pub mod error {
17 pub use s2n_quic_core::transport::error::Code;
18}
19
20pub type Result<T, E = Error> = core::result::Result<T, E>;
21
22pub struct Connection(Inner);
23
24impl fmt::Debug for Connection {
25 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
26 self.0.fmt(f)
27 }
28}
29
30impl Connection {
31 #[inline]
32 pub(crate) const fn new(inner: Inner) -> Self {
33 Self(inner)
34 }
35
36 impl_accept_api!();
37 impl_handle_api!(|handle, call| call!(handle));
38
39 /// Returns a cloneable handle to the connection
40 ///
41 /// # Examples
42 ///
43 /// ```rust,no_run
44 /// # async fn test() {
45 /// # let mut connection: s2n_quic::connection::Connection = todo!();
46 /// #
47 /// let handle = connection.handle();
48 /// let another_handle = handle.clone();
49 /// # }
50 /// ```
51 #[inline]
52 pub fn handle(&self) -> Handle {
53 Handle(self.0.clone())
54 }
55
56 /// Splits the connection into a [`connection::Handle`](crate::connection::Handle) and
57 /// [`connection::StreamAcceptor`](crate::connection::StreamAcceptor) halves
58 ///
59 /// # Examples
60 ///
61 /// ```rust,no_run
62 /// # async fn test() -> s2n_quic::connection::Result<()> {
63 /// # let mut connection: s2n_quic::connection::Connection = todo!();
64 /// #
65 /// let (mut handle, mut acceptor) = connection.split();
66 /// let mut send = handle.open_send_stream().await?;
67 /// tokio::spawn(async move {
68 /// let _ = send.send(bytes::Bytes::from_static(&[1, 2, 3])).await;
69 /// });
70 ///
71 /// while let Some(stream) = acceptor.accept().await? {
72 /// println!("accepted stream {}", stream.id());
73 /// }
74 ///
75 /// #
76 /// # Ok(())
77 /// # }
78 /// ```
79 #[inline]
80 pub fn split(self) -> (Handle, StreamAcceptor) {
81 let handle = Handle(self.0.clone());
82 let acceptor = StreamAcceptor(self.0);
83 (handle, acceptor)
84 }
85}