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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
use bytes::{Buf, BufMut};
use futures::Poll;
#[cfg(feature = "tls")]
use rustls::ServerSession;
use std::io;
use tokio::io::{AsyncRead, AsyncWrite};
#[cfg(feature = "tls")]
use tokio_rustls::TlsStream;
use tokio_tcp::TcpStream;
#[cfg(unix)]
use tokio_uds::UnixStream;
#[derive(Debug)]
pub(super) enum MaybeTls<S> {
Raw(S),
#[cfg(feature = "tls")]
Tls(TlsStream<S, ServerSession>),
}
macro_rules! impl_tls {
($self:expr, $s:ident => $e:expr) => {
match *$self {
MaybeTls::Raw(ref $s) => $e,
#[cfg(feature = "tls")]
MaybeTls::Tls(ref $s) => $e,
}
};
(@mut $self:expr, $s:ident => $e:expr) => {
match *$self {
MaybeTls::Raw(ref mut $s) => $e,
#[cfg(feature = "tls")]
MaybeTls::Tls(ref mut $s) => $e,
}
};
}
#[cfg(feature = "tls")]
impl<S> MaybeTls<S> {
fn session(&self) -> Option<&ServerSession> {
match *self {
#[cfg(feature = "tls")]
MaybeTls::Tls(ref s) => Some(s.get_ref().1),
_ => None,
}
}
fn session_mut(&mut self) -> Option<&mut ServerSession> {
match *self {
#[cfg(feature = "tls")]
MaybeTls::Tls(ref mut s) => Some(s.get_mut().1),
_ => None,
}
}
}
impl<S: AsyncRead + AsyncWrite> io::Read for MaybeTls<S> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
impl_tls!(@mut self, s => s.read(buf))
}
}
impl<S: AsyncRead + AsyncWrite> io::Write for MaybeTls<S> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
impl_tls!(@mut self, s => s.write(buf))
}
fn flush(&mut self) -> io::Result<()> {
impl_tls!(@mut self, s => s.flush())
}
}
impl<S: AsyncRead + AsyncWrite> AsyncRead for MaybeTls<S> {
unsafe fn prepare_uninitialized_buffer(&self, buf: &mut [u8]) -> bool {
impl_tls!(self, s => s.prepare_uninitialized_buffer(buf))
}
fn read_buf<B: BufMut>(&mut self, buf: &mut B) -> Poll<usize, io::Error> {
impl_tls!(@mut self, s => s.read_buf(buf))
}
}
impl<S: AsyncRead + AsyncWrite> AsyncWrite for MaybeTls<S> {
fn shutdown(&mut self) -> Poll<(), io::Error> {
impl_tls!(@mut self, s => s.shutdown())
}
fn write_buf<B: Buf>(&mut self, buf: &mut B) -> Poll<usize, io::Error> {
impl_tls!(@mut self, s => s.write_buf(buf))
}
}
#[derive(Debug)]
pub struct Io(IoKind);
#[derive(Debug)]
enum IoKind {
Tcp(MaybeTls<TcpStream>),
#[cfg(unix)]
Uds(MaybeTls<UnixStream>),
}
macro_rules! impl_io {
($self:expr, $s:ident => $e:expr) => {
match $self.0 {
IoKind::Tcp(ref $s) => $e,
#[cfg(unix)]
IoKind::Uds(ref $s) => $e,
}
};
(@mut $self:expr, $s:ident => $e:expr) => {
match $self.0 {
IoKind::Tcp(ref mut $s) => $e,
#[cfg(unix)]
IoKind::Uds(ref mut $s) => $e,
}
};
}
impl Io {
pub(super) fn tcp(stream: MaybeTls<TcpStream>) -> Io {
Io(IoKind::Tcp(stream))
}
#[cfg(unix)]
pub(super) fn uds(stream: MaybeTls<UnixStream>) -> Io {
Io(IoKind::Uds(stream))
}
#[allow(missing_docs)]
pub fn shutdown(&mut self) -> Poll<(), io::Error> {
impl_io!(@mut self, s => s.shutdown())
}
}
#[cfg(feature = "tls")]
impl Io {
pub fn session(&self) -> Option<&ServerSession> {
impl_io!(self, s => s.session())
}
pub fn session_mut(&mut self) -> Option<&mut ServerSession> {
impl_io!(@mut self, s => s.session_mut())
}
}
impl io::Read for Io {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
impl_io!(@mut self, s => s.read(buf))
}
}
impl io::Write for Io {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
impl_io!(@mut self, s => s.write(buf))
}
fn flush(&mut self) -> io::Result<()> {
impl_io!(@mut self, s => s.flush())
}
}
impl AsyncRead for Io {
unsafe fn prepare_uninitialized_buffer(&self, buf: &mut [u8]) -> bool {
impl_io!(self, s => s.prepare_uninitialized_buffer(buf))
}
fn read_buf<B: BufMut>(&mut self, buf: &mut B) -> Poll<usize, io::Error> {
impl_io!(@mut self, s => s.read_buf(buf))
}
}
impl AsyncWrite for Io {
#[inline]
fn shutdown(&mut self) -> Poll<(), io::Error> {
self.shutdown()
}
fn write_buf<B: Buf>(&mut self, buf: &mut B) -> Poll<usize, io::Error> {
impl_io!(@mut self, s => s.write_buf(buf))
}
}