2018-08-23 20:47:41 -07:00
|
|
|
use std::collections::VecDeque;
|
2019-11-14 18:38:24 +06:00
|
|
|
use std::future::Future;
|
|
|
|
use std::io;
|
2019-03-13 15:37:12 -07:00
|
|
|
use std::marker::PhantomData;
|
2019-03-13 12:40:11 -07:00
|
|
|
use std::net::SocketAddr;
|
2019-11-14 18:38:24 +06:00
|
|
|
use std::pin::Pin;
|
|
|
|
use std::task::{Context, Poll};
|
2018-08-23 20:47:41 -07:00
|
|
|
|
2019-12-02 11:43:52 +06:00
|
|
|
use actix_rt::net::TcpStream;
|
2019-11-14 18:38:24 +06:00
|
|
|
use actix_service::{Service, ServiceFactory};
|
2020-03-12 04:22:38 +09:00
|
|
|
use futures_util::future::{err, ok, BoxFuture, Either, FutureExt, Ready};
|
2020-12-29 00:38:41 +00:00
|
|
|
use log::{error, trace};
|
2018-08-23 20:47:41 -07:00
|
|
|
|
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
|
|
|
|
2020-09-02 22:14:07 +01: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-08-05 09:52:50 -07:00
|
|
|
|
2020-09-02 22:14:07 +01:00
|
|
|
/// Create TCP connector service
|
2019-08-05 09:52:50 -07:00
|
|
|
pub fn service(&self) -> TcpConnector<T> {
|
|
|
|
TcpConnector(PhantomData)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Default for TcpConnectorFactory<T> {
|
|
|
|
fn default() -> Self {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 04:28:00 +00:00
|
|
|
impl<T: Address> ServiceFactory<Connect<T>> for TcpConnectorFactory<T> {
|
2019-03-13 15:37:12 -07:00
|
|
|
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 = ();
|
2019-11-14 18:38:24 +06:00
|
|
|
type Future = Ready<Result<Self::Service, Self::InitError>>;
|
2018-09-10 19:42:51 -07:00
|
|
|
|
2019-12-02 21:27:48 +06:00
|
|
|
fn new_service(&self, _: ()) -> Self::Future {
|
2019-08-05 09:52:50 -07:00
|
|
|
ok(self.service())
|
2018-11-29 17:17:02 -10:00
|
|
|
}
|
2018-08-27 20:32:49 -07:00
|
|
|
}
|
|
|
|
|
2020-09-02 22:14:07 +01:00
|
|
|
/// TCP connector service
|
2019-08-17 05:15:51 +09:00
|
|
|
#[derive(Default, 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 04:28:00 +00:00
|
|
|
impl<T: Address> Service<Connect<T>> for TcpConnector<T> {
|
2019-03-13 15:37:12 -07:00
|
|
|
type Response = Connection<T, TcpStream>;
|
2019-03-13 12:40:11 -07:00
|
|
|
type Error = ConnectError;
|
2020-09-02 22:14:07 +01:00
|
|
|
#[allow(clippy::type_complexity)]
|
2019-11-14 18:38:24 +06:00
|
|
|
type Future = Either<TcpConnectorResponse<T>, Ready<Result<Self::Response, Self::Error>>>;
|
2018-08-23 20:47:41 -07:00
|
|
|
|
2020-12-27 14:15:42 +00:00
|
|
|
actix_service::always_ready!();
|
2018-08-23 20:47:41 -07:00
|
|
|
|
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-11-14 18:38:24 +06:00
|
|
|
Either::Left(TcpConnectorResponse::new(req, port, addr))
|
2019-03-13 15:37:12 -07:00
|
|
|
} else {
|
|
|
|
error!("TCP connector: got unresolved address");
|
2020-05-03 23:14:22 +01:00
|
|
|
Either::Right(err(ConnectError::Unresolved))
|
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)]
|
2020-09-02 22:14:07 +01: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>>,
|
2019-11-14 18:38:24 +06:00
|
|
|
stream: Option<BoxFuture<'static, Result<TcpStream, io::Error>>>,
|
2018-08-23 20:47:41 -07:00
|
|
|
}
|
|
|
|
|
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,
|
2019-11-14 18:38:24 +06:00
|
|
|
stream: Some(TcpStream::connect(addr).boxed()),
|
2019-03-13 12:40:11 -07:00
|
|
|
},
|
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-11-14 18:38:24 +06:00
|
|
|
type Output = Result<Connection<T, TcpStream>, ConnectError>;
|
|
|
|
|
2019-12-02 22:30:09 +06:00
|
|
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
2019-11-18 18:28:54 +06:00
|
|
|
let this = self.get_mut();
|
2018-08-23 20:47:41 -07:00
|
|
|
|
|
|
|
// connect
|
|
|
|
loop {
|
2019-11-14 18:38:24 +06:00
|
|
|
if let Some(new) = this.stream.as_mut() {
|
|
|
|
match new.as_mut().poll(cx) {
|
|
|
|
Poll::Ready(Ok(sock)) => {
|
|
|
|
let req = this.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-11-14 18:38:24 +06:00
|
|
|
return Poll::Ready(Ok(Connection::new(sock, req)));
|
2018-08-28 16:24:36 -07:00
|
|
|
}
|
2019-11-14 18:38:24 +06:00
|
|
|
Poll::Pending => return Poll::Pending,
|
|
|
|
Poll::Ready(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: {}",
|
2019-11-14 18:38:24 +06:00
|
|
|
this.req.as_ref().unwrap().host(),
|
|
|
|
this.port,
|
2019-03-13 12:40:11 -07:00
|
|
|
);
|
2019-11-14 18:38:24 +06:00
|
|
|
if this.addrs.is_none() || this.addrs.as_ref().unwrap().is_empty() {
|
|
|
|
return Poll::Ready(Err(err.into()));
|
2018-08-23 20:47:41 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// try to connect
|
2019-11-14 18:38:24 +06:00
|
|
|
let addr = this.addrs.as_mut().unwrap().pop_front().unwrap();
|
2019-11-18 18:28:54 +06:00
|
|
|
this.stream = Some(TcpStream::connect(addr).boxed());
|
2018-10-23 22:26:16 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|