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
use std::io;
use std::pin::Pin;
use std::task::{Context, Poll};
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
use tokio::net::TcpStream;
#[cfg(feature = "secure")]
use tokio_rustls::client::TlsStream;
#[pin_project::pin_project(project = DataStreamProj)]
pub enum DataStream {
Tcp(#[pin] TcpStream),
#[cfg(feature = "secure")]
Ssl(#[pin] TlsStream<TcpStream>),
}
impl DataStream {
pub fn into_tcp_stream(self) -> TcpStream {
match self {
DataStream::Tcp(stream) => stream,
#[cfg(feature = "secure")]
DataStream::Ssl(stream) => stream.into_inner().0,
}
}
pub fn is_ssl(&self) -> bool {
match self {
#[cfg(feature = "secure")]
DataStream::Ssl(_) => true,
_ => false,
}
}
pub fn get_ref(&self) -> &TcpStream {
match self {
DataStream::Tcp(ref stream) => stream,
#[cfg(feature = "secure")]
DataStream::Ssl(ref stream) => stream.get_ref().0,
}
}
}
impl AsyncRead for DataStream {
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut ReadBuf<'_>,
) -> Poll<io::Result<()>> {
match self.project() {
DataStreamProj::Tcp(stream) => stream.poll_read(cx, buf),
#[cfg(feature = "secure")]
DataStreamProj::Ssl(stream) => stream.poll_read(cx, buf),
}
}
}
impl AsyncWrite for DataStream {
fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<io::Result<usize>> {
match self.project() {
DataStreamProj::Tcp(stream) => stream.poll_write(cx, buf),
#[cfg(feature = "secure")]
DataStreamProj::Ssl(stream) => stream.poll_write(cx, buf),
}
}
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
match self.project() {
DataStreamProj::Tcp(stream) => stream.poll_flush(cx),
#[cfg(feature = "secure")]
DataStreamProj::Ssl(stream) => stream.poll_flush(cx),
}
}
fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
match self.project() {
DataStreamProj::Tcp(stream) => stream.poll_shutdown(cx),
#[cfg(feature = "secure")]
DataStreamProj::Ssl(stream) => stream.poll_shutdown(cx),
}
}
}