actix-net/actix-connect/src/connector.rs

158 lines
4.5 KiB
Rust
Raw Normal View History

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