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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

use core::fmt;
use s2n_quic_transport::connection::Connection as Inner;

#[macro_use]
mod acceptor;
#[macro_use]
mod handle;

pub use acceptor::*;
pub use handle::*;
pub use s2n_quic_core::connection::Error;

pub mod error {
    pub use s2n_quic_core::transport::error::Code;
}

pub type Result<T, E = Error> = core::result::Result<T, E>;

pub struct Connection(Inner);

impl fmt::Debug for Connection {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.0.fmt(f)
    }
}

impl Connection {
    #[inline]
    pub(crate) const fn new(inner: Inner) -> Self {
        Self(inner)
    }

    impl_accept_api!();
    impl_handle_api!(|handle, call| call!(handle));

    /// Returns a cloneable handle to the connection
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # async fn test() {
    /// #   let mut connection: s2n_quic::connection::Connection = todo!();
    /// #
    /// let handle = connection.handle();
    /// let another_handle = handle.clone();
    /// # }
    /// ```
    #[inline]
    pub fn handle(&self) -> Handle {
        Handle(self.0.clone())
    }

    /// Splits the connection into a  [`connection::Handle`](crate::connection::Handle) and
    /// [`connection::StreamAcceptor`](crate::connection::StreamAcceptor) halves
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # async fn test() -> s2n_quic::connection::Result<()> {
    /// #   let mut connection: s2n_quic::connection::Connection = todo!();
    /// #
    /// let (mut handle, mut acceptor) = connection.split();
    /// let mut send = handle.open_send_stream().await?;
    /// tokio::spawn(async move {
    ///     let _ = send.send(bytes::Bytes::from_static(&[1, 2, 3])).await;
    /// });
    ///
    /// while let Some(stream) = acceptor.accept().await? {
    ///     println!("accepted stream {}", stream.id());
    /// }
    ///
    /// #
    /// #   Ok(())
    /// # }
    /// ```
    #[inline]
    pub fn split(self) -> (Handle, StreamAcceptor) {
        let handle = Handle(self.0.clone());
        let acceptor = StreamAcceptor(self.0);
        (handle, acceptor)
    }
}