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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
use std::collections::VecDeque;
use std::io;
use std::net::SocketAddr;
use futures::{
future::{ok, FutureResult},
Async, Future, Poll,
};
use tokio_tcp::{ConnectFuture, TcpStream};
use trust_dns_resolver::config::{ResolverConfig, ResolverOpts};
use trust_dns_resolver::system_conf::read_system_conf;
use super::resolver::{HostAware, Resolver, ResolverError, ResolverFuture};
use super::service::{NewService, Service};
#[derive(Debug)]
pub enum ConnectorError {
Resolver(ResolverError),
NoRecords,
IoError(io::Error),
}
impl From<ResolverError> for ConnectorError {
fn from(err: ResolverError) -> Self {
ConnectorError::Resolver(err)
}
}
pub struct ConnectionInfo {
pub host: String,
pub addr: SocketAddr,
}
pub struct Connector<T = String> {
resolver: Resolver<T>,
}
impl<T: HostAware> Default for Connector<T> {
fn default() -> Self {
let (cfg, opts) = if let Ok((cfg, opts)) = read_system_conf() {
(cfg, opts)
} else {
(ResolverConfig::default(), ResolverOpts::default())
};
Connector::new(cfg, opts)
}
}
impl<T: HostAware> Connector<T> {
pub fn new(cfg: ResolverConfig, opts: ResolverOpts) -> Self {
Connector {
resolver: Resolver::new(cfg, opts),
}
}
pub fn with_resolver(
resolver: Resolver<T>,
) -> impl Service<
Request = T,
Response = (T, ConnectionInfo, TcpStream),
Error = ConnectorError,
> + Clone {
Connector { resolver }
}
pub fn new_service<E>() -> impl NewService<
Request = T,
Response = (T, ConnectionInfo, TcpStream),
Error = ConnectorError,
InitError = E,
> + Clone {
|| -> FutureResult<Connector<T>, E> { ok(Connector::default()) }
}
pub fn new_service_with_config<E>(
cfg: ResolverConfig, opts: ResolverOpts,
) -> impl NewService<
Request = T,
Response = (T, ConnectionInfo, TcpStream),
Error = ConnectorError,
InitError = E,
> + Clone {
move || -> FutureResult<Connector<T>, E> { ok(Connector::new(cfg.clone(), opts)) }
}
pub fn change_request<T2: HostAware>(&self) -> Connector<T2> {
Connector {
resolver: self.resolver.change_request(),
}
}
}
impl<T> Clone for Connector<T> {
fn clone(&self) -> Self {
Connector {
resolver: self.resolver.clone(),
}
}
}
impl<T: HostAware> Service for Connector<T> {
type Request = T;
type Response = (T, ConnectionInfo, TcpStream);
type Error = ConnectorError;
type Future = ConnectorFuture<T>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
Ok(Async::Ready(()))
}
fn call(&mut self, req: Self::Request) -> Self::Future {
ConnectorFuture {
fut: self.resolver.call(req),
fut2: None,
}
}
}
#[doc(hidden)]
pub struct ConnectorFuture<T: HostAware> {
fut: ResolverFuture<T>,
fut2: Option<TcpConnector<T>>,
}
impl<T: HostAware> Future for ConnectorFuture<T> {
type Item = (T, ConnectionInfo, TcpStream);
type Error = ConnectorError;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
if let Some(ref mut fut) = self.fut2 {
return fut.poll();
}
match self.fut.poll().map_err(ConnectorError::from)? {
Async::Ready((req, host, addrs)) => {
if addrs.is_empty() {
Err(ConnectorError::NoRecords)
} else {
self.fut2 = Some(TcpConnector::new(req, host, addrs));
self.poll()
}
}
Async::NotReady => Ok(Async::NotReady),
}
}
}
#[derive(Clone)]
pub struct DefaultConnector<T: HostAware>(Connector<T>);
impl<T: HostAware> Default for DefaultConnector<T> {
fn default() -> Self {
DefaultConnector(Connector::default())
}
}
impl<T: HostAware> DefaultConnector<T> {
pub fn new(cfg: ResolverConfig, opts: ResolverOpts) -> Self {
DefaultConnector(Connector::new(cfg, opts))
}
}
impl<T: HostAware> Service for DefaultConnector<T> {
type Request = T;
type Response = TcpStream;
type Error = ConnectorError;
type Future = DefaultConnectorFuture<T>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
self.0.poll_ready()
}
fn call(&mut self, req: Self::Request) -> Self::Future {
DefaultConnectorFuture {
fut: self.0.call(req),
}
}
}
#[doc(hidden)]
pub struct DefaultConnectorFuture<T: HostAware> {
fut: ConnectorFuture<T>,
}
impl<T: HostAware> Future for DefaultConnectorFuture<T> {
type Item = TcpStream;
type Error = ConnectorError;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
Ok(Async::Ready(try_ready!(self.fut.poll()).2))
}
}
#[doc(hidden)]
pub struct TcpConnector<T> {
req: Option<T>,
host: Option<String>,
addr: Option<SocketAddr>,
addrs: VecDeque<SocketAddr>,
stream: Option<ConnectFuture>,
}
impl<T> TcpConnector<T> {
pub fn new(req: T, host: String, addrs: VecDeque<SocketAddr>) -> TcpConnector<T> {
TcpConnector {
addrs,
req: Some(req),
host: Some(host),
addr: None,
stream: None,
}
}
}
impl<T> Future for TcpConnector<T> {
type Item = (T, ConnectionInfo, TcpStream);
type Error = ConnectorError;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
loop {
if let Some(new) = self.stream.as_mut() {
match new.poll() {
Ok(Async::Ready(sock)) => {
return Ok(Async::Ready((
self.req.take().unwrap(),
ConnectionInfo {
host: self.host.take().unwrap(),
addr: self.addr.take().unwrap(),
},
sock,
)))
}
Ok(Async::NotReady) => return Ok(Async::NotReady),
Err(err) => {
if self.addrs.is_empty() {
return Err(ConnectorError::IoError(err));
}
}
}
}
let addr = self.addrs.pop_front().unwrap();
self.stream = Some(TcpStream::connect(&addr));
self.addr = Some(addr)
}
}
}