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
use async_compat::Compat;
use std::{io::Result, net::SocketAddr};
use tokio::net::{TcpStream, ToSocketAddrs};
use trillium_macros::{AsyncRead, AsyncWrite};
use trillium_server_common::{AsyncRead, AsyncWrite, Transport};

/// A transport newtype for tokio
#[derive(Debug, Clone, AsyncRead, AsyncWrite)]
pub struct TokioTransport<T>(pub(crate) T);

impl<T> TokioTransport<T> {
    /// returns the contained type
    pub fn into_inner(self) -> T {
        self.0
    }
}

impl TokioTransport<Compat<TcpStream>> {
    /// initiates an outbound http connection
    pub async fn connect(socket: impl ToSocketAddrs) -> Result<Self> {
        TcpStream::connect(socket)
            .await
            .map(|t| Self(Compat::new(t)))
    }
}

impl<T> From<T> for TokioTransport<T> {
    fn from(value: T) -> Self {
        Self(value)
    }
}

impl Transport for TokioTransport<Compat<TcpStream>> {
    fn peer_addr(&self) -> Result<Option<SocketAddr>> {
        self.0.get_ref().peer_addr().map(Some)
    }

    fn set_ip_ttl(&mut self, ttl: u32) -> Result<()> {
        self.0.get_mut().set_ttl(ttl)
    }

    fn set_nodelay(&mut self, nodelay: bool) -> Result<()> {
        self.0.get_mut().set_nodelay(nodelay)
    }
}

#[cfg(unix)]
impl Transport for TokioTransport<Compat<tokio::net::UnixStream>> {}