2018-08-23 20:47:41 -07:00
|
|
|
use std::collections::VecDeque;
|
2019-03-13 15:37:12 -07:00
|
|
|
use std::marker::PhantomData;
|
2019-03-13 12:40:11 -07:00
|
|
|
use std::net::SocketAddr;
|
2018-08-23 20:47:41 -07:00
|
|
|
|
2019-03-13 12:40:11 -07:00
|
|
|
use actix_service::{NewService, Service};
|
|
|
|
use futures::future::{err, ok, Either, FutureResult};
|
|
|
|
use futures::{Async, Future, Poll};
|
2018-08-23 20:47:41 -07:00
|
|
|
use tokio_tcp::{ConnectFuture, TcpStream};
|
|
|
|
|
2019-03-13 15:37:12 -07:00
|
|
|
use super::connect::{Address, Connect, Connection};
|
2019-03-13 12:40:11 -07:00
|
|
|
use super::error::ConnectError;
|
2018-08-28 16:24:36 -07:00
|
|
|
|
2019-03-13 12:40:11 -07:00
|
|
|
/// Tcp connector service factory
|
2019-03-13 15:37:12 -07:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct ConnectorFactory<T>(PhantomData<T>);
|
2018-08-23 20:47:41 -07:00
|
|
|
|
2019-03-13 15:37:12 -07:00
|
|
|
impl<T> ConnectorFactory<T> {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
ConnectorFactory(PhantomData)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Clone for ConnectorFactory<T> {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
ConnectorFactory(PhantomData)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Address> NewService for ConnectorFactory<T> {
|
|
|
|
type Request = Connect<T>;
|
|
|
|
type Response = Connection<T, TcpStream>;
|
2019-03-13 12:40:11 -07:00
|
|
|
type Error = ConnectError;
|
2019-03-13 15:37:12 -07:00
|
|
|
type Service = Connector<T>;
|
2019-03-13 12:40:11 -07:00
|
|
|
type InitError = ();
|
|
|
|
type Future = FutureResult<Self::Service, Self::InitError>;
|
2018-09-10 19:42:51 -07:00
|
|
|
|
2019-03-13 12:40:11 -07:00
|
|
|
fn new_service(&self, _: &()) -> Self::Future {
|
2019-03-13 15:37:12 -07:00
|
|
|
ok(Connector(PhantomData))
|
2018-11-29 17:17:02 -10:00
|
|
|
}
|
2018-08-27 20:32:49 -07:00
|
|
|
}
|
|
|
|
|
2019-03-13 12:40:11 -07:00
|
|
|
/// Tcp connector service
|
2019-03-13 15:37:12 -07:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Connector<T>(PhantomData<T>);
|
|
|
|
|
|
|
|
impl<T> Connector<T> {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Connector(PhantomData)
|
|
|
|
}
|
|
|
|
}
|
2018-08-23 20:47:41 -07:00
|
|
|
|
2019-03-13 15:37:12 -07:00
|
|
|
impl<T> Clone for Connector<T> {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Connector(PhantomData)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Address> Service for Connector<T> {
|
|
|
|
type Request = Connect<T>;
|
|
|
|
type Response = Connection<T, TcpStream>;
|
2019-03-13 12:40:11 -07:00
|
|
|
type Error = ConnectError;
|
2019-03-13 15:37:12 -07:00
|
|
|
type Future = Either<ConnectorResponse<T>, FutureResult<Self::Response, Self::Error>>;
|
2018-08-23 20:47:41 -07:00
|
|
|
|
|
|
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
|
|
|
Ok(Async::Ready(()))
|
|
|
|
}
|
|
|
|
|
2019-03-13 15:37:12 -07:00
|
|
|
fn call(&mut self, req: Connect<T>) -> Self::Future {
|
|
|
|
let Connect { req, addr } = req;
|
|
|
|
|
|
|
|
if let Some(addr) = addr {
|
|
|
|
Either::A(ConnectorResponse::new(req, addr))
|
|
|
|
} else {
|
|
|
|
error!("TCP connector: got unresolved address");
|
|
|
|
Either::B(err(ConnectError::Unresolverd))
|
2018-09-10 19:39:55 -07:00
|
|
|
}
|
2018-08-23 20:47:41 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-11 14:01:51 -07:00
|
|
|
#[doc(hidden)]
|
2018-11-11 21:12:30 -08:00
|
|
|
/// Tcp stream connector response future
|
2019-03-13 15:37:12 -07:00
|
|
|
pub struct ConnectorResponse<T> {
|
|
|
|
req: Option<T>,
|
2019-03-13 12:40:11 -07:00
|
|
|
addrs: Option<VecDeque<SocketAddr>>,
|
2018-08-23 20:47:41 -07:00
|
|
|
stream: Option<ConnectFuture>,
|
|
|
|
}
|
|
|
|
|
2019-03-13 15:37:12 -07:00
|
|
|
impl<T: Address> ConnectorResponse<T> {
|
2019-03-13 12:40:11 -07:00
|
|
|
pub fn new(
|
2019-03-13 15:37:12 -07:00
|
|
|
req: T,
|
2019-03-13 12:40:11 -07:00
|
|
|
addr: either::Either<SocketAddr, VecDeque<SocketAddr>>,
|
2019-03-13 15:37:12 -07:00
|
|
|
) -> ConnectorResponse<T> {
|
|
|
|
trace!("TCP connector - connecting to {:?}", req.host());
|
2019-03-13 12:40:11 -07:00
|
|
|
|
|
|
|
match addr {
|
|
|
|
either::Either::Left(addr) => ConnectorResponse {
|
2019-03-13 15:37:12 -07:00
|
|
|
req: Some(req),
|
2019-03-13 12:40:11 -07:00
|
|
|
addrs: None,
|
|
|
|
stream: Some(TcpStream::connect(&addr)),
|
|
|
|
},
|
|
|
|
either::Either::Right(addrs) => ConnectorResponse {
|
2019-03-13 15:37:12 -07:00
|
|
|
req: Some(req),
|
2019-03-13 12:40:11 -07:00
|
|
|
addrs: Some(addrs),
|
|
|
|
stream: None,
|
|
|
|
},
|
2018-08-23 20:47:41 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-13 15:37:12 -07:00
|
|
|
impl<T: Address> Future for ConnectorResponse<T> {
|
|
|
|
type Item = Connection<T, TcpStream>;
|
2019-03-13 12:40:11 -07:00
|
|
|
type Error = ConnectError;
|
2018-08-23 20:47:41 -07:00
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
|
|
// connect
|
|
|
|
loop {
|
|
|
|
if let Some(new) = self.stream.as_mut() {
|
|
|
|
match new.poll() {
|
2018-08-28 16:24:36 -07:00
|
|
|
Ok(Async::Ready(sock)) => {
|
2019-03-13 15:37:12 -07:00
|
|
|
let req = self.req.take().unwrap();
|
2019-03-13 12:40:11 -07:00
|
|
|
trace!(
|
|
|
|
"TCP connector - successfully connected to connecting to {:?} - {:?}",
|
2019-03-13 15:37:12 -07:00
|
|
|
req.host(), sock.peer_addr()
|
2019-03-13 12:40:11 -07:00
|
|
|
);
|
2019-03-13 15:37:12 -07:00
|
|
|
return Ok(Async::Ready(Connection::new(sock, req)));
|
2018-08-28 16:24:36 -07:00
|
|
|
}
|
2018-08-23 20:47:41 -07:00
|
|
|
Ok(Async::NotReady) => return Ok(Async::NotReady),
|
|
|
|
Err(err) => {
|
2019-03-13 12:40:11 -07:00
|
|
|
trace!(
|
2019-03-13 15:37:12 -07:00
|
|
|
"TCP connector - failed to connect to connecting to {:?} port: {}",
|
|
|
|
self.req.as_ref().unwrap().host(),
|
|
|
|
self.req.as_ref().unwrap().port(),
|
2019-03-13 12:40:11 -07:00
|
|
|
);
|
|
|
|
if self.addrs.as_ref().unwrap().is_empty() {
|
|
|
|
return Err(err.into());
|
2018-08-23 20:47:41 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// try to connect
|
2019-03-13 12:40:11 -07:00
|
|
|
self.stream = Some(TcpStream::connect(
|
|
|
|
&self.addrs.as_mut().unwrap().pop_front().unwrap(),
|
|
|
|
));
|
2018-10-23 22:26:16 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-13 12:40:11 -07:00
|
|
|
// #[derive(Clone)]
|
|
|
|
// pub struct DefaultConnector(Connector);
|
|
|
|
|
|
|
|
// impl Default for DefaultConnector {
|
|
|
|
// fn default() -> Self {
|
|
|
|
// DefaultConnector(Connector::default())
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
// impl DefaultConnector {
|
|
|
|
// pub fn new(cfg: ResolverConfig, opts: ResolverOpts) -> Self {
|
|
|
|
// DefaultConnector(Connector::new(cfg, opts))
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
// impl Service for DefaultConnector {
|
|
|
|
// type Request = Connect;
|
|
|
|
// type Response = TcpStream;
|
|
|
|
// type Error = ConnectorError;
|
|
|
|
// type Future = DefaultConnectorFuture;
|
|
|
|
|
|
|
|
// fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
|
|
|
// self.0.poll_ready()
|
|
|
|
// }
|
|
|
|
|
|
|
|
// fn call(&mut self, req: Connect) -> Self::Future {
|
|
|
|
// DefaultConnectorFuture {
|
|
|
|
// fut: self.0.call(req),
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
// #[doc(hidden)]
|
|
|
|
// pub struct DefaultConnectorFuture {
|
|
|
|
// fut: Either<ConnectorFuture, ConnectorTcpFuture>,
|
|
|
|
// }
|
|
|
|
|
|
|
|
// impl Future for DefaultConnectorFuture {
|
|
|
|
// type Item = TcpStream;
|
|
|
|
// type Error = ConnectorError;
|
|
|
|
|
|
|
|
// fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
|
|
// Ok(Async::Ready(try_ready!(self.fut.poll()).1))
|
|
|
|
// }
|
|
|
|
// }
|