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)]
|
2019-03-31 19:14:13 -07:00
|
|
|
pub struct TcpConnectorFactory<T>(PhantomData<T>);
|
2018-08-23 20:47:41 -07:00
|
|
|
|
2019-03-31 19:14:13 -07:00
|
|
|
impl<T> TcpConnectorFactory<T> {
|
2019-03-13 15:37:12 -07:00
|
|
|
pub fn new() -> Self {
|
2019-03-31 19:14:13 -07:00
|
|
|
TcpConnectorFactory(PhantomData)
|
2019-03-13 15:37:12 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-31 19:14:13 -07:00
|
|
|
impl<T> Clone for TcpConnectorFactory<T> {
|
2019-03-13 15:37:12 -07:00
|
|
|
fn clone(&self) -> Self {
|
2019-03-31 19:14:13 -07:00
|
|
|
TcpConnectorFactory(PhantomData)
|
2019-03-13 15:37:12 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-31 19:14:13 -07:00
|
|
|
impl<T: Address> NewService for TcpConnectorFactory<T> {
|
2019-03-13 15:37:12 -07:00
|
|
|
type Request = Connect<T>;
|
|
|
|
type Response = Connection<T, TcpStream>;
|
2019-03-13 12:40:11 -07:00
|
|
|
type Error = ConnectError;
|
2019-05-12 06:03:50 -07:00
|
|
|
type Config = ();
|
2019-03-31 19:14:13 -07:00
|
|
|
type Service = TcpConnector<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-31 19:14:13 -07:00
|
|
|
ok(TcpConnector(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)]
|
2019-03-31 19:14:13 -07:00
|
|
|
pub struct TcpConnector<T>(PhantomData<T>);
|
2019-03-13 15:37:12 -07:00
|
|
|
|
2019-03-31 19:14:13 -07:00
|
|
|
impl<T> TcpConnector<T> {
|
2019-03-13 15:37:12 -07:00
|
|
|
pub fn new() -> Self {
|
2019-03-31 19:14:13 -07:00
|
|
|
TcpConnector(PhantomData)
|
2019-03-13 15:37:12 -07:00
|
|
|
}
|
|
|
|
}
|
2018-08-23 20:47:41 -07:00
|
|
|
|
2019-03-31 19:14:13 -07:00
|
|
|
impl<T> Clone for TcpConnector<T> {
|
2019-03-13 15:37:12 -07:00
|
|
|
fn clone(&self) -> Self {
|
2019-03-31 19:14:13 -07:00
|
|
|
TcpConnector(PhantomData)
|
2019-03-13 15:37:12 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-31 19:14:13 -07:00
|
|
|
impl<T: Address> Service for TcpConnector<T> {
|
2019-03-13 15:37:12 -07:00
|
|
|
type Request = Connect<T>;
|
|
|
|
type Response = Connection<T, TcpStream>;
|
2019-03-13 12:40:11 -07:00
|
|
|
type Error = ConnectError;
|
2019-03-31 19:14:13 -07:00
|
|
|
type Future = Either<TcpConnectorResponse<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 {
|
2019-03-13 22:51:31 -07:00
|
|
|
let port = req.port();
|
|
|
|
let Connect { req, addr, .. } = req;
|
2019-03-13 15:37:12 -07:00
|
|
|
|
|
|
|
if let Some(addr) = addr {
|
2019-03-31 19:14:13 -07:00
|
|
|
Either::A(TcpConnectorResponse::new(req, port, addr))
|
2019-03-13 15:37:12 -07:00
|
|
|
} 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-31 19:14:13 -07:00
|
|
|
pub struct TcpConnectorResponse<T> {
|
2019-03-13 15:37:12 -07:00
|
|
|
req: Option<T>,
|
2019-03-13 22:51:31 -07:00
|
|
|
port: u16,
|
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-31 19:14:13 -07:00
|
|
|
impl<T: Address> TcpConnectorResponse<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 22:51:31 -07:00
|
|
|
port: u16,
|
2019-03-13 12:40:11 -07:00
|
|
|
addr: either::Either<SocketAddr, VecDeque<SocketAddr>>,
|
2019-03-31 19:14:13 -07:00
|
|
|
) -> TcpConnectorResponse<T> {
|
2019-03-13 22:51:31 -07:00
|
|
|
trace!(
|
|
|
|
"TCP connector - connecting to {:?} port:{}",
|
|
|
|
req.host(),
|
|
|
|
port
|
|
|
|
);
|
2019-03-13 12:40:11 -07:00
|
|
|
|
|
|
|
match addr {
|
2019-03-31 19:14:13 -07:00
|
|
|
either::Either::Left(addr) => TcpConnectorResponse {
|
2019-03-13 15:37:12 -07:00
|
|
|
req: Some(req),
|
2019-03-13 22:51:31 -07:00
|
|
|
port,
|
2019-03-13 12:40:11 -07:00
|
|
|
addrs: None,
|
|
|
|
stream: Some(TcpStream::connect(&addr)),
|
|
|
|
},
|
2019-03-31 19:14:13 -07:00
|
|
|
either::Either::Right(addrs) => TcpConnectorResponse {
|
2019-03-13 15:37:12 -07:00
|
|
|
req: Some(req),
|
2019-03-13 22:51:31 -07:00
|
|
|
port,
|
2019-03-13 12:40:11 -07:00
|
|
|
addrs: Some(addrs),
|
|
|
|
stream: None,
|
|
|
|
},
|
2018-08-23 20:47:41 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-31 19:14:13 -07:00
|
|
|
impl<T: Address> Future for TcpConnectorResponse<T> {
|
2019-03-13 15:37:12 -07:00
|
|
|
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(),
|
2019-03-13 22:51:31 -07:00
|
|
|
self.port,
|
2019-03-13 12:40:11 -07:00
|
|
|
);
|
2019-03-15 11:37:51 -07:00
|
|
|
if self.addrs.is_none() || self.addrs.as_ref().unwrap().is_empty() {
|
2019-03-13 12:40:11 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|