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

#[cfg(feature = "asyncstd")]
pub use async_std::net::*;


#[cfg(feature = "tokio2")]
pub use tokio::net::*;



#[cfg(test)]
mod tcp_stream;


#[cfg(feature = "tls")]
#[cfg(unix)]
pub mod tls;

#[cfg(unix)]
pub use connector::*;

#[cfg(unix)]
mod connector {
    use std::io::Error as IoError;
    #[cfg(unix)]
    use std::os::unix::io::RawFd;
    #[cfg(unix)]
    use std::os::unix::io::AsRawFd;

    use tracing::debug;
    use futures::io::{AsyncRead, AsyncWrite};
    use async_trait::async_trait;

    use super::TcpStream;

    /// transform raw tcp stream to another stream
    #[async_trait]
    pub trait TcpDomainConnector {

        type WrapperStream: AsyncRead + AsyncWrite + Unpin + Send;

        async fn connect(&self,domain: &str) -> Result<(Self::WrapperStream,RawFd),IoError>;
    }


    #[derive(Clone)]
    pub struct DefaultTcpDomainConnector{}

    impl DefaultTcpDomainConnector {
        pub fn new() -> Self {
            Self{}
        }
    }

    #[async_trait]
    impl TcpDomainConnector for DefaultTcpDomainConnector {

        type WrapperStream = TcpStream;

        async fn connect(&self,addr: &str) -> Result<(Self::WrapperStream,RawFd),IoError> {
            debug!("connect to tcp addr: {}",addr);
            let tcp_stream = TcpStream::connect(addr).await?;
            let fd = tcp_stream.as_raw_fd();
            Ok((tcp_stream,fd))
        }
    }


}