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
use crate::{HttpIncoming, TcpStream, TlsStream};
use futures::prelude::*;
use futures::stream::{FusedStream, SelectAll};
use futures::StreamExt;
use std::pin::Pin;
use std::task::{Context, Poll};

pub trait IsTls {
    fn is_tls(&self) -> bool;
}

pub enum TcpOrTlsStream {
    Tcp(TcpStream),
    Tls(TlsStream),
}

impl IsTls for TcpOrTlsStream {
    fn is_tls(&self) -> bool {
        match self {
            Self::Tcp(_) => false,
            Self::Tls(_) => true,
        }
    }
}

pub struct TcpOrTlsIncoming {
    incomings: SelectAll<Box<dyn Stream<Item = TcpOrTlsStream> + Unpin>>,
}

impl TcpOrTlsIncoming {
    pub fn new() -> Self {
        Self {
            incomings: SelectAll::new(),
        }
    }
    pub fn push(
        &mut self,
        incoming: impl Stream<Item = impl Into<TcpOrTlsStream>> + Unpin + 'static,
    ) {
        self.incomings
            .push(Box::new(incoming.map(|stream| stream.into())))
    }
    pub fn merge(&mut self, other: Self) {
        self.incomings.extend(other.incomings.into_iter())
    }
    pub fn http(self) -> HttpIncoming<TcpOrTlsStream, Self> {
        HttpIncoming::new(self)
    }
}

impl Unpin for TcpOrTlsIncoming {}

impl Stream for TcpOrTlsIncoming {
    type Item = TcpOrTlsStream;

    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        self.incomings.poll_next_unpin(cx)
    }
}

impl FusedStream for TcpOrTlsIncoming {
    fn is_terminated(&self) -> bool {
        self.incomings.is_terminated()
    }
}

impl AsyncRead for TcpOrTlsStream {
    fn poll_read(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &mut [u8],
    ) -> Poll<std::io::Result<usize>> {
        match self.get_mut() {
            TcpOrTlsStream::Tcp(tcp) => Pin::new(tcp).poll_read(cx, buf),
            TcpOrTlsStream::Tls(tls) => Pin::new(tls).poll_read(cx, buf),
        }
    }
}

impl AsyncWrite for TcpOrTlsStream {
    fn poll_write(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &[u8],
    ) -> Poll<std::io::Result<usize>> {
        match self.get_mut() {
            TcpOrTlsStream::Tcp(tcp) => Pin::new(tcp).poll_write(cx, buf),
            TcpOrTlsStream::Tls(tls) => Pin::new(tls).poll_write(cx, buf),
        }
    }

    fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
        match self.get_mut() {
            TcpOrTlsStream::Tcp(tcp) => Pin::new(tcp).poll_flush(cx),
            TcpOrTlsStream::Tls(tls) => Pin::new(tls).poll_flush(cx),
        }
    }

    fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
        match self.get_mut() {
            TcpOrTlsStream::Tcp(tcp) => Pin::new(tcp).poll_close(cx),
            TcpOrTlsStream::Tls(tls) => Pin::new(tls).poll_close(cx),
        }
    }
}

impl From<TcpStream> for TcpOrTlsStream {
    fn from(value: TcpStream) -> Self {
        TcpOrTlsStream::Tcp(value)
    }
}

impl From<TlsStream> for TcpOrTlsStream {
    fn from(value: TlsStream) -> Self {
        TcpOrTlsStream::Tls(value)
    }
}