1
0
mirror of https://github.com/fafhrd91/actix-net synced 2025-02-08 04:06:07 +01:00

159 lines
4.9 KiB
Rust
Raw Normal View History

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;
Migrate actix-net to std::future (#64) * Migrate actix-codec, actix-rt, and actix-threadpool to std::future * update to latest tokio alpha and futures-rs * Migrate actix-service to std::future, This is a squash of ~8 commits, since it included a lot of experimentation. To see the commits, look into the semtexzv/std-future-service-tmp branch. * update futures-rs and tokio * Migrate actix-threadpool to std::future (#59) * Migrate actix-threadpool to std::future * Cosmetic refactor - turn log::error! into log::warn! as it doesn't throw any error - add Clone and Copy impls for Cancelled making it cheap to operate with - apply rustfmt * Bump up crate version to 0.2.0 and pre-fill its changelog * Disable patching 'actix-threadpool' crate in global workspace as unnecessary * Revert patching and fix 'actix-rt' * Migrate actix-rt to std::future (#47) * remove Pin from Service::poll_ready(); simplify combinators api; make code compile * disable tests * update travis config * refactor naming * drop IntoFuture trait * Migrate actix-server to std::future (#50) Still not finished, this is more WIP, this is an aggregation of several commits, which can be found in semtexzv/std-future-server-tmp branch * update actix-server * rename Factor to ServiceFactory * start server worker in start mehtod * update actix-utils * remove IntoTransform trait * Migrate actix-server::ssl::nativetls to std futures (#61) * Refactor 'nativetls' module * Migrate 'actix-server-config' to std futures - remove "uds" feature - disable features by default * Switch NativeTlsAcceptor to use 'tokio-tls' crate * Bikeshed features names and remove unnecessary dependencies for 'actix-server-config' crate * update openssl impl * migrate actix-connect to std::future * migrate actix-ioframe to std::future * update version to alpha.1 * fix boxed service * migrate server rustls support * migratte openssl and rustls connecttors * store the thread's handle with arbiter (#62) * update ssl connect tests * restore service tests * update readme
2019-11-14 18:38:24 +06:00
use actix_service::{Service, ServiceFactory};
2021-01-26 08:05:19 +00:00
use futures_core::{future::LocalBoxFuture, ready};
use log::{error, trace};
use tokio_util::sync::ReusableBoxFuture;
2018-08-23 20:47:41 -07: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
/// TCP connector service factory
2021-01-26 08:05:19 +00:00
#[derive(Debug, Copy, Clone)]
pub struct TcpConnectorFactory;
impl TcpConnectorFactory {
/// Create TCP connector service
pub fn service(&self) -> TcpConnector {
TcpConnector
}
2019-03-13 15:37:12 -07: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;
type Config = ();
type Service = TcpConnector;
2019-03-13 12:40:11 -07:00
type InitError = ();
type Future = LocalBoxFuture<'static, Result<Self::Service, Self::InitError>>;
2018-09-10 19:42:51 -07:00
fn new_service(&self, _: ()) -> Self::Future {
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
}
/// TCP connector service
2021-01-26 08:05:19 +00:00
#[derive(Debug, Copy, Clone)]
pub struct TcpConnector;
2019-03-13 15:37:12 -07: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
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
TcpConnectorResponse::new(req, port, addr)
2018-08-23 20:47:41 -07: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<ReusableBoxFuture<Result<TcpStream, io::Error>>>,
2020-12-29 19:36:17 +08:00
},
Error(Option<ConnectError>),
2018-08-23 20:47:41 -07:00
}
impl<T: Address> TcpConnectorResponse<T> {
pub(crate) fn new(req: T, port: u16, addr: ConnectAddrs) -> TcpConnectorResponse<T> {
2021-01-26 08:05:19 +00:00
if addr.is_none() {
error!("TCP connector: unresolved connection address");
return TcpConnectorResponse::Error(Some(ConnectError::Unresolved));
}
2019-03-13 22:51:31 -07:00
trace!(
2021-01-26 08:05:19 +00:00
"TCP connector: connecting to {} on port {}",
req.hostname(),
2019-03-13 22:51:31 -07:00
port
);
2019-03-13 12:40:11 -07:00
match addr {
2021-01-26 08:05:19 +00:00
ConnectAddrs::None => unreachable!("none variant already checked"),
ConnectAddrs::One(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,
stream: Some(ReusableBoxFuture::new(TcpStream::connect(addr))),
2019-03-13 12:40:11 -07:00
},
2021-01-26 08:05:19 +00: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
}
}
}
impl<T: Address> Future for TcpConnectorResponse<T> {
Migrate actix-net to std::future (#64) * Migrate actix-codec, actix-rt, and actix-threadpool to std::future * update to latest tokio alpha and futures-rs * Migrate actix-service to std::future, This is a squash of ~8 commits, since it included a lot of experimentation. To see the commits, look into the semtexzv/std-future-service-tmp branch. * update futures-rs and tokio * Migrate actix-threadpool to std::future (#59) * Migrate actix-threadpool to std::future * Cosmetic refactor - turn log::error! into log::warn! as it doesn't throw any error - add Clone and Copy impls for Cancelled making it cheap to operate with - apply rustfmt * Bump up crate version to 0.2.0 and pre-fill its changelog * Disable patching 'actix-threadpool' crate in global workspace as unnecessary * Revert patching and fix 'actix-rt' * Migrate actix-rt to std::future (#47) * remove Pin from Service::poll_ready(); simplify combinators api; make code compile * disable tests * update travis config * refactor naming * drop IntoFuture trait * Migrate actix-server to std::future (#50) Still not finished, this is more WIP, this is an aggregation of several commits, which can be found in semtexzv/std-future-server-tmp branch * update actix-server * rename Factor to ServiceFactory * start server worker in start mehtod * update actix-utils * remove IntoTransform trait * Migrate actix-server::ssl::nativetls to std futures (#61) * Refactor 'nativetls' module * Migrate 'actix-server-config' to std futures - remove "uds" feature - disable features by default * Switch NativeTlsAcceptor to use 'tokio-tls' crate * Bikeshed features names and remove unnecessary dependencies for 'actix-server-config' crate * update openssl impl * migrate actix-connect to std::future * migrate actix-ioframe to std::future * update version to alpha.1 * fix boxed service * migrate server rustls support * migratte openssl and rustls connecttors * store the thread's handle with arbiter (#62) * update ssl connect tests * restore service tests * update readme
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> {
2021-01-26 08:05:19 +00:00
match self.get_mut() {
TcpConnectorResponse::Error(err) => Poll::Ready(Err(err.take().unwrap())),
2020-12-29 19:36:17 +08:00
TcpConnectorResponse::Response {
req,
port,
addrs,
stream,
} => loop {
if let Some(new) = stream.as_mut() {
match ready!(new.poll(cx)) {
2021-01-26 08:05:19 +00:00
Ok(sock) => {
2020-12-29 19:36:17 +08:00
let req = req.take().unwrap();
trace!(
2021-01-26 08:05:19 +00:00
"TCP connector: successfully connected to {:?} - {:?}",
req.hostname(),
sock.peer_addr()
2020-12-29 19:36:17 +08:00
);
return Poll::Ready(Ok(Connection::new(sock, req)));
}
2021-01-26 08:05:19 +00:00
Err(err) => {
2020-12-29 19:36:17 +08:00
trace!(
2021-01-26 08:05:19 +00:00
"TCP connector: failed to connect to {:?} port: {}",
req.as_ref().unwrap().hostname(),
2020-12-29 19:36:17 +08:00
port,
);
2021-01-26 08:05:19 +00:00
2020-12-29 19:36:17 +08:00
if addrs.is_none() || addrs.as_ref().unwrap().is_empty() {
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();
match stream {
Some(rbf) => rbf.set(TcpStream::connect(addr)),
None => *stream = Some(ReusableBoxFuture::new(TcpStream::connect(addr))),
}
2020-12-29 19:36:17 +08:00
},
2018-10-23 22:26:16 -07:00
}
}
}