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-09-27 05:40:45 +02:00
|
|
|
use std::time::Duration;
|
2018-08-19 19:47:04 +02:00
|
|
|
|
2018-12-10 06:51:35 +01:00
|
|
|
use actix_rt::spawn;
|
2019-11-14 13:38:24 +01:00
|
|
|
use actix_service::{self as actix, Service, ServiceFactory as ActixServiceFactory};
|
2019-12-02 17:30:09 +01:00
|
|
|
use actix_utils::counter::CounterGuard;
|
2020-03-11 20:27:26 +01:00
|
|
|
use futures_util::future::{err, ok, LocalBoxFuture, Ready};
|
|
|
|
use futures_util::{FutureExt, TryFutureExt};
|
2018-12-06 23:04:42 +01:00
|
|
|
use log::error;
|
2018-08-22 02:08:23 +02:00
|
|
|
|
2018-11-03 17:09:14 +01:00
|
|
|
use super::Token;
|
2019-07-18 13:05:40 +02:00
|
|
|
use crate::socket::{FromStream, StdStream};
|
2018-08-19 19:47:04 +02:00
|
|
|
|
2018-09-27 05:40:45 +02:00
|
|
|
/// Server message
|
2019-02-22 21:44:37 +01:00
|
|
|
pub(crate) enum ServerMessage {
|
2018-09-28 02:05:48 +02:00
|
|
|
/// New stream
|
2019-07-18 13:05:40 +02:00
|
|
|
Connect(StdStream),
|
2020-09-13 11:12:07 +02:00
|
|
|
|
|
|
|
/// Gracefully shutdown
|
2018-09-27 05:40:45 +02:00
|
|
|
Shutdown(Duration),
|
2020-09-13 11:12:07 +02:00
|
|
|
|
2018-09-28 02:05:48 +02:00
|
|
|
/// Force shutdown
|
2018-09-07 22:06:51 +02:00
|
|
|
ForceShutdown,
|
|
|
|
}
|
|
|
|
|
2019-07-18 13:05:40 +02:00
|
|
|
pub trait ServiceFactory<Stream: FromStream>: Send + Clone + 'static {
|
2019-12-02 06:30:27 +01:00
|
|
|
type Factory: actix::ServiceFactory<Config = (), Request = Stream>;
|
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 {
|
2018-11-03 17:09:14 +01:00
|
|
|
fn name(&self, token: Token) -> &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
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
fn create(&self) -> LocalBoxFuture<'static, Result<Vec<(Token, 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<
|
2019-03-09 16:27:56 +01:00
|
|
|
Request = (Option<CounterGuard>, ServerMessage),
|
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
|
|
|
>,
|
|
|
|
>;
|
|
|
|
|
2018-09-27 05:40:45 +02:00
|
|
|
pub(crate) struct StreamService<T> {
|
2018-09-07 20:35:25 +02:00
|
|
|
service: T,
|
|
|
|
}
|
|
|
|
|
2018-09-27 05:40:45 +02:00
|
|
|
impl<T> StreamService<T> {
|
2018-11-03 17:09:14 +01:00
|
|
|
pub(crate) fn new(service: T) -> Self {
|
2018-09-27 05:40:45 +02:00
|
|
|
StreamService { service }
|
2018-09-07 20:35:25 +02:00
|
|
|
}
|
2018-08-19 19:47:04 +02:00
|
|
|
}
|
|
|
|
|
2019-07-18 13:05:40 +02:00
|
|
|
impl<T, I> Service for StreamService<T>
|
2018-08-19 19:47:04 +02:00
|
|
|
where
|
2019-12-02 06:30:27 +01:00
|
|
|
T: Service<Request = I>,
|
2018-08-19 19:47:04 +02:00
|
|
|
T::Future: 'static,
|
2018-09-07 23:34:31 +02:00
|
|
|
T::Error: 'static,
|
2019-07-18 13:05:40 +02:00
|
|
|
I: FromStream,
|
2018-08-19 19:47:04 +02:00
|
|
|
{
|
2019-03-09 16:27:56 +01:00
|
|
|
type Request = (Option<CounterGuard>, ServerMessage);
|
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
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
fn poll_ready(&mut self, ctx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
|
|
|
self.service.poll_ready(ctx).map_err(|_| ())
|
2018-08-19 19:47:04 +02:00
|
|
|
}
|
|
|
|
|
2018-09-27 05:40:45 +02:00
|
|
|
fn call(&mut self, (guard, req): (Option<CounterGuard>, ServerMessage)) -> Self::Future {
|
2018-09-07 22:06:51 +02:00
|
|
|
match req {
|
|
|
|
ServerMessage::Connect(stream) => {
|
2019-07-18 13:05:40 +02:00
|
|
|
let stream = FromStream::from_stdstream(stream).map_err(|e| {
|
2019-03-11 20:01:55 +01:00
|
|
|
error!("Can not convert to an async tcp stream: {}", e);
|
|
|
|
});
|
2018-09-07 22:06:51 +02:00
|
|
|
|
|
|
|
if let Ok(stream) = stream {
|
2019-12-02 06:30:27 +01:00
|
|
|
let f = self.service.call(stream);
|
2019-12-04 16:34:48 +01:00
|
|
|
spawn(async move {
|
|
|
|
let _ = f.await;
|
|
|
|
drop(guard);
|
|
|
|
});
|
2018-09-27 05:40:45 +02:00
|
|
|
ok(())
|
2018-09-07 22:06:51 +02:00
|
|
|
} else {
|
2018-09-27 05:40:45 +02:00
|
|
|
err(())
|
2018-09-07 22:06:51 +02:00
|
|
|
}
|
|
|
|
}
|
2018-09-27 05:40:45 +02:00
|
|
|
_ => ok(()),
|
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,
|
2018-11-03 17:09:14 +01:00
|
|
|
token: Token,
|
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,
|
|
|
|
token: Token,
|
|
|
|
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
|
|
|
{
|
2018-11-03 17:09:14 +01:00
|
|
|
fn name(&self, _: Token) -> &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
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
fn create(&self) -> LocalBoxFuture<'static, Result<Vec<(Token, BoxedServerService)>, ()>> {
|
2018-11-03 17:09:14 +01:00
|
|
|
let token = self.token;
|
2019-11-14 13:38:24 +01:00
|
|
|
self.inner
|
|
|
|
.create()
|
2019-12-02 16:27:48 +01:00
|
|
|
.new_service(())
|
2019-11-14 13:38:24 +01:00
|
|
|
.map_err(|_| ())
|
|
|
|
.map_ok(move |inner| {
|
|
|
|
let service: BoxedServerService = Box::new(StreamService::new(inner));
|
|
|
|
vec![(token, service)]
|
|
|
|
})
|
|
|
|
.boxed_local()
|
2018-08-19 19:47:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-18 13:05:40 +02:00
|
|
|
impl InternalServiceFactory for Box<dyn InternalServiceFactory> {
|
2018-11-03 17:09:14 +01:00
|
|
|
fn name(&self, token: Token) -> &str {
|
|
|
|
self.as_ref().name(token)
|
2018-09-18 05:19:48 +02:00
|
|
|
}
|
|
|
|
|
2019-07-18 13:05:40 +02:00
|
|
|
fn clone_factory(&self) -> Box<dyn InternalServiceFactory> {
|
2018-08-19 19:47:04 +02:00
|
|
|
self.as_ref().clone_factory()
|
|
|
|
}
|
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
fn create(&self) -> LocalBoxFuture<'static, Result<Vec<(Token, BoxedServerService)>, ()>> {
|
2018-08-19 19:47:04 +02:00
|
|
|
self.as_ref().create()
|
|
|
|
}
|
|
|
|
}
|
2018-09-08 23:50:16 +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,
|
2019-12-02 06:30:27 +01:00
|
|
|
T: actix::ServiceFactory<Config = (), Request = I>,
|
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)()
|
|
|
|
}
|
|
|
|
}
|