2021-01-22 17:33:50 -08:00
|
|
|
use std::{
|
|
|
|
collections::VecDeque,
|
|
|
|
future::Future,
|
|
|
|
io,
|
|
|
|
net::SocketAddr,
|
|
|
|
pin::Pin,
|
|
|
|
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};
|
2021-01-22 17:33:50 -08:00
|
|
|
use futures_core::future::LocalBoxFuture;
|
2020-12-29 00:38:41 +00:00
|
|
|
use log::{error, trace};
|
2018-08-23 20:47:41 -07:00
|
|
|
|
2021-01-22 17:33:50 -08:00
|
|
|
use super::connect::{Address, Connect, ConnectAddrs, 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
|
2021-01-22 17:33:50 -08:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
pub struct TcpConnectorFactory;
|
2019-08-05 09:52:50 -07:00
|
|
|
|
2021-01-22 17:33:50 -08:00
|
|
|
impl TcpConnectorFactory {
|
2020-09-02 22:14:07 +01:00
|
|
|
/// Create TCP connector service
|
2021-01-22 17:33:50 -08:00
|
|
|
pub fn service(&self) -> TcpConnector {
|
|
|
|
TcpConnector
|
2019-08-05 09:52:50 -07:00
|
|
|
}
|
2019-03-13 15:37:12 -07:00
|
|
|
}
|
|
|
|
|
2021-01-22 17:33:50 -08:00
|
|
|
impl<T: Address> ServiceFactory<Connect<T>> for TcpConnectorFactory {
|
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 = ();
|
2021-01-22 17:33:50 -08:00
|
|
|
type Service = TcpConnector;
|
2019-03-13 12:40:11 -07:00
|
|
|
type InitError = ();
|
2021-01-22 17:33:50 -08:00
|
|
|
type Future = LocalBoxFuture<'static, 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 {
|
2021-01-22 17:33:50 -08:00
|
|
|
let service = self.service();
|
|
|
|
Box::pin(async move { Ok(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
|
2021-01-22 17:33:50 -08:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
pub struct TcpConnector;
|
2019-03-13 15:37:12 -07:00
|
|
|
|
2021-01-22 17:33:50 -08:00
|
|
|
impl<T: Address> Service<Connect<T>> for TcpConnector {
|
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-12-29 19:36:17 +08:00
|
|
|
type Future = TcpConnectorResponse<T>;
|
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
|
|
|
|
2021-01-22 19:06:22 -08:00
|
|
|
fn call(&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
|
|
|
|
2021-01-22 17:33:50 -08:00
|
|
|
TcpConnectorResponse::new(req, port, addr)
|
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
|
2020-12-29 19:36:17 +08:00
|
|
|
pub enum TcpConnectorResponse<T> {
|
|
|
|
Response {
|
|
|
|
req: Option<T>,
|
|
|
|
port: u16,
|
|
|
|
addrs: Option<VecDeque<SocketAddr>>,
|
|
|
|
stream: Option<LocalBoxFuture<'static, Result<TcpStream, io::Error>>>,
|
|
|
|
},
|
|
|
|
Error(Option<ConnectError>),
|
2018-08-23 20:47:41 -07:00
|
|
|
}
|
|
|
|
|
2019-03-31 19:14:13 -07:00
|
|
|
impl<T: Address> TcpConnectorResponse<T> {
|
2021-01-22 17:33:50 -08:00
|
|
|
pub(crate) fn new(req: T, port: u16, addr: ConnectAddrs) -> 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 {
|
2021-01-22 17:33:50 -08:00
|
|
|
ConnectAddrs::One(None) => {
|
|
|
|
error!("TCP connector: got unresolved address");
|
|
|
|
TcpConnectorResponse::Error(Some(ConnectError::Unresolved))
|
|
|
|
}
|
|
|
|
ConnectAddrs::One(Some(addr)) => TcpConnectorResponse::Response {
|
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,
|
2020-12-29 19:36:17 +08:00
|
|
|
stream: Some(Box::pin(TcpStream::connect(addr))),
|
2019-03-13 12:40:11 -07:00
|
|
|
},
|
2021-01-22 17:33:50 -08:00
|
|
|
// when resolver returns multiple socket addr for request they would be popped from
|
|
|
|
// front end of queue and returns with the first successful tcp connection.
|
|
|
|
ConnectAddrs::Multi(addrs) => TcpConnectorResponse::Response {
|
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();
|
2020-12-29 19:36:17 +08:00
|
|
|
match this {
|
|
|
|
TcpConnectorResponse::Error(e) => Poll::Ready(Err(e.take().unwrap())),
|
|
|
|
// connect
|
|
|
|
TcpConnectorResponse::Response {
|
|
|
|
req,
|
|
|
|
port,
|
|
|
|
addrs,
|
|
|
|
stream,
|
|
|
|
} => loop {
|
|
|
|
if let Some(new) = stream.as_mut() {
|
|
|
|
match new.as_mut().poll(cx) {
|
|
|
|
Poll::Ready(Ok(sock)) => {
|
|
|
|
let req = req.take().unwrap();
|
|
|
|
trace!(
|
|
|
|
"TCP connector - successfully connected to connecting to {:?} - {:?}",
|
|
|
|
req.host(), sock.peer_addr()
|
|
|
|
);
|
|
|
|
return Poll::Ready(Ok(Connection::new(sock, req)));
|
|
|
|
}
|
|
|
|
Poll::Pending => return Poll::Pending,
|
|
|
|
Poll::Ready(Err(err)) => {
|
|
|
|
trace!(
|
|
|
|
"TCP connector - failed to connect to connecting to {:?} port: {}",
|
|
|
|
req.as_ref().unwrap().host(),
|
|
|
|
port,
|
|
|
|
);
|
|
|
|
if addrs.is_none() || addrs.as_ref().unwrap().is_empty() {
|
2021-01-22 17:33:50 -08:00
|
|
|
return Poll::Ready(Err(ConnectError::Io(err)));
|
2020-12-29 19:36:17 +08:00
|
|
|
}
|
2018-08-23 20:47:41 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-29 19:36:17 +08:00
|
|
|
// try to connect
|
|
|
|
let addr = addrs.as_mut().unwrap().pop_front().unwrap();
|
|
|
|
*stream = Some(Box::pin(TcpStream::connect(addr)));
|
|
|
|
},
|
2018-10-23 22:26:16 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|