2019-07-18 13:05:40 +02:00
|
|
|
use std::marker::PhantomData;
|
|
|
|
use std::net::SocketAddr;
|
2019-11-14 13:38:24 +01:00
|
|
|
use std::task::{Context, Poll};
|
2018-08-19 19:47:04 +02:00
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
use actix_service::{Service, ServiceFactory as BaseServiceFactory};
|
2021-04-29 17:27:08 +02:00
|
|
|
use actix_utils::future::{ready, Ready};
|
2020-12-29 00:44:53 +01:00
|
|
|
use futures_core::future::LocalBoxFuture;
|
2018-12-06 23:04:42 +01:00
|
|
|
use log::error;
|
2018-08-22 02:08:23 +02:00
|
|
|
|
2020-12-29 00:44:53 +01:00
|
|
|
use crate::socket::{FromStream, MioStream};
|
2021-04-29 17:27:08 +02:00
|
|
|
use crate::worker::WorkerCounterGuard;
|
2018-08-19 19:47:04 +02:00
|
|
|
|
2019-07-18 13:05:40 +02:00
|
|
|
pub trait ServiceFactory<Stream: FromStream>: Send + Clone + 'static {
|
2020-12-27 05:28:00 +01:00
|
|
|
type Factory: BaseServiceFactory<Stream, Config = ()>;
|
2018-09-27 05:40:45 +02:00
|
|
|
|
2019-11-20 19:35:44 +01:00
|
|
|
fn create(&self) -> Self::Factory;
|
2018-09-27 05:40:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) trait InternalServiceFactory: Send {
|
2021-04-29 17:27:08 +02:00
|
|
|
fn name(&self, token: usize) -> &str;
|
2018-09-27 05:40:45 +02:00
|
|
|
|
2019-07-18 13:05:40 +02:00
|
|
|
fn clone_factory(&self) -> Box<dyn InternalServiceFactory>;
|
2018-09-27 05:40:45 +02:00
|
|
|
|
2021-04-29 17:27:08 +02:00
|
|
|
fn create(&self) -> LocalBoxFuture<'static, Result<(usize, BoxedServerService), ()>>;
|
2018-09-27 05:40:45 +02:00
|
|
|
}
|
|
|
|
|
2018-08-19 19:47:04 +02:00
|
|
|
pub(crate) type BoxedServerService = Box<
|
2019-07-18 13:05:40 +02:00
|
|
|
dyn Service<
|
2021-04-29 17:27:08 +02:00
|
|
|
(WorkerCounterGuard, MioStream),
|
2018-08-19 19:47:04 +02:00
|
|
|
Response = (),
|
|
|
|
Error = (),
|
2019-11-14 13:38:24 +01:00
|
|
|
Future = Ready<Result<(), ()>>,
|
2018-08-19 19:47:04 +02:00
|
|
|
>,
|
|
|
|
>;
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
pub(crate) struct StreamService<S, I> {
|
|
|
|
service: S,
|
|
|
|
_phantom: PhantomData<I>,
|
2018-09-07 20:35:25 +02:00
|
|
|
}
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
impl<S, I> StreamService<S, I> {
|
|
|
|
pub(crate) fn new(service: S) -> Self {
|
|
|
|
StreamService {
|
|
|
|
service,
|
|
|
|
_phantom: PhantomData,
|
|
|
|
}
|
2018-09-07 20:35:25 +02:00
|
|
|
}
|
2018-08-19 19:47:04 +02:00
|
|
|
}
|
|
|
|
|
2021-04-29 17:27:08 +02:00
|
|
|
impl<S, I> Service<(WorkerCounterGuard, MioStream)> for StreamService<S, I>
|
2018-08-19 19:47:04 +02:00
|
|
|
where
|
2020-12-27 05:28:00 +01:00
|
|
|
S: Service<I>,
|
|
|
|
S::Future: 'static,
|
|
|
|
S::Error: 'static,
|
2019-07-18 13:05:40 +02:00
|
|
|
I: FromStream,
|
2018-08-19 19:47:04 +02:00
|
|
|
{
|
|
|
|
type Response = ();
|
|
|
|
type Error = ();
|
2019-11-14 13:38:24 +01:00
|
|
|
type Future = Ready<Result<(), ()>>;
|
2018-08-19 19:47:04 +02:00
|
|
|
|
2021-01-23 04:06:22 +01:00
|
|
|
fn poll_ready(&self, ctx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
2019-11-14 13:38:24 +01:00
|
|
|
self.service.poll_ready(ctx).map_err(|_| ())
|
2018-08-19 19:47:04 +02:00
|
|
|
}
|
|
|
|
|
2021-04-29 17:27:08 +02:00
|
|
|
fn call(&self, (guard, req): (WorkerCounterGuard, MioStream)) -> Self::Future {
|
2020-12-29 00:44:53 +01:00
|
|
|
ready(match FromStream::from_mio(req) {
|
2020-12-13 01:46:32 +01:00
|
|
|
Ok(stream) => {
|
|
|
|
let f = self.service.call(stream);
|
2020-12-29 00:44:53 +01:00
|
|
|
actix_rt::spawn(async move {
|
2020-12-13 01:46:32 +01:00
|
|
|
let _ = f.await;
|
|
|
|
drop(guard);
|
2019-03-11 20:01:55 +01:00
|
|
|
});
|
2020-12-29 00:44:53 +01:00
|
|
|
Ok(())
|
2020-12-13 01:46:32 +01:00
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
error!("Can not convert to an async tcp stream: {}", e);
|
2020-12-29 00:44:53 +01:00
|
|
|
Err(())
|
2018-09-07 22:06:51 +02:00
|
|
|
}
|
2020-12-29 00:44:53 +01:00
|
|
|
})
|
2018-08-19 19:47:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-18 13:05:40 +02:00
|
|
|
pub(crate) struct StreamNewService<F: ServiceFactory<Io>, Io: FromStream> {
|
2018-09-27 05:40:45 +02:00
|
|
|
name: String,
|
|
|
|
inner: F,
|
2021-04-29 17:27:08 +02:00
|
|
|
token: usize,
|
2019-03-09 16:27:56 +01:00
|
|
|
addr: SocketAddr,
|
2019-07-18 13:05:40 +02:00
|
|
|
_t: PhantomData<Io>,
|
2018-09-27 05:40:45 +02:00
|
|
|
}
|
|
|
|
|
2019-07-18 13:05:40 +02:00
|
|
|
impl<F, Io> StreamNewService<F, Io>
|
2018-09-27 05:40:45 +02:00
|
|
|
where
|
2019-07-18 13:05:40 +02:00
|
|
|
F: ServiceFactory<Io>,
|
|
|
|
Io: FromStream + Send + 'static,
|
2018-09-27 05:40:45 +02:00
|
|
|
{
|
2019-03-09 16:27:56 +01:00
|
|
|
pub(crate) fn create(
|
|
|
|
name: String,
|
2021-04-29 17:27:08 +02:00
|
|
|
token: usize,
|
2019-03-09 16:27:56 +01:00
|
|
|
inner: F,
|
|
|
|
addr: SocketAddr,
|
2019-07-18 13:05:40 +02:00
|
|
|
) -> Box<dyn InternalServiceFactory> {
|
2019-03-09 16:27:56 +01:00
|
|
|
Box::new(Self {
|
|
|
|
name,
|
|
|
|
token,
|
|
|
|
inner,
|
|
|
|
addr,
|
2019-07-18 13:05:40 +02:00
|
|
|
_t: PhantomData,
|
2019-03-09 16:27:56 +01:00
|
|
|
})
|
2018-09-27 05:40:45 +02:00
|
|
|
}
|
2018-08-19 19:47:04 +02:00
|
|
|
}
|
|
|
|
|
2019-07-18 13:05:40 +02:00
|
|
|
impl<F, Io> InternalServiceFactory for StreamNewService<F, Io>
|
2018-08-19 19:47:04 +02:00
|
|
|
where
|
2019-07-18 13:05:40 +02:00
|
|
|
F: ServiceFactory<Io>,
|
|
|
|
Io: FromStream + Send + 'static,
|
2018-08-19 19:47:04 +02:00
|
|
|
{
|
2021-04-29 17:27:08 +02:00
|
|
|
fn name(&self, _: usize) -> &str {
|
2018-09-18 05:19:48 +02:00
|
|
|
&self.name
|
|
|
|
}
|
|
|
|
|
2019-07-18 13:05:40 +02:00
|
|
|
fn clone_factory(&self) -> Box<dyn InternalServiceFactory> {
|
2018-08-19 19:47:04 +02:00
|
|
|
Box::new(Self {
|
2018-09-18 05:19:48 +02:00
|
|
|
name: self.name.clone(),
|
2018-08-19 19:47:04 +02:00
|
|
|
inner: self.inner.clone(),
|
2018-11-03 17:09:14 +01:00
|
|
|
token: self.token,
|
2019-03-09 16:27:56 +01:00
|
|
|
addr: self.addr,
|
2019-07-18 13:05:40 +02:00
|
|
|
_t: PhantomData,
|
2018-08-19 19:47:04 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-04-29 17:27:08 +02:00
|
|
|
fn create(&self) -> LocalBoxFuture<'static, Result<(usize, BoxedServerService), ()>> {
|
2018-11-03 17:09:14 +01:00
|
|
|
let token = self.token;
|
2020-12-29 00:44:53 +01:00
|
|
|
let fut = self.inner.create().new_service(());
|
|
|
|
Box::pin(async move {
|
|
|
|
match fut.await {
|
|
|
|
Ok(inner) => {
|
|
|
|
let service = Box::new(StreamService::new(inner)) as _;
|
2021-04-28 00:58:02 +02:00
|
|
|
Ok((token, service))
|
2020-12-29 00:44:53 +01:00
|
|
|
}
|
|
|
|
Err(_) => Err(()),
|
|
|
|
}
|
|
|
|
})
|
2018-08-19 19:47:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-18 13:05:40 +02:00
|
|
|
impl<F, T, I> ServiceFactory<I> for F
|
2018-09-08 23:50:16 +02:00
|
|
|
where
|
|
|
|
F: Fn() -> T + Send + Clone + 'static,
|
2020-12-27 05:28:00 +01:00
|
|
|
T: BaseServiceFactory<I, Config = ()>,
|
2019-07-18 13:05:40 +02:00
|
|
|
I: FromStream,
|
2018-09-08 23:50:16 +02:00
|
|
|
{
|
2019-11-20 19:35:44 +01:00
|
|
|
type Factory = T;
|
2018-09-08 23:50:16 +02:00
|
|
|
|
|
|
|
fn create(&self) -> T {
|
|
|
|
(self)()
|
|
|
|
}
|
|
|
|
}
|