1
0
mirror of https://github.com/fafhrd91/actix-net synced 2024-11-28 06:32:39 +01:00
actix-net/src/server_service.rs

139 lines
3.8 KiB
Rust
Raw Normal View History

2018-08-21 07:21:23 +02:00
use std::sync::{
atomic::{AtomicUsize, Ordering},
Arc,
};
2018-08-19 19:47:04 +02:00
use std::{fmt, io, net};
use futures::{future, Future, Poll};
use tokio_reactor::Handle;
use tokio_tcp::TcpStream;
2018-08-22 02:08:23 +02:00
use super::{Config, NewService, Service};
2018-08-19 19:47:04 +02:00
pub(crate) type BoxedServerService = Box<
Service<
Request = net::TcpStream,
Response = (),
Error = (),
Future = Box<Future<Item = (), Error = ()>>,
>,
>;
pub(crate) struct ServerService<T> {
inner: T,
2018-08-21 07:21:23 +02:00
counter: Arc<AtomicUsize>,
2018-08-19 19:47:04 +02:00
}
impl<T> Service for ServerService<T>
where
T: Service<Request = TcpStream, Response = ()>,
T::Future: 'static,
T::Error: fmt::Display + 'static,
{
type Request = net::TcpStream;
type Response = ();
type Error = ();
type Future = Box<Future<Item = (), Error = ()>>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
self.inner.poll_ready().map_err(|_| ())
}
fn call(&mut self, stream: net::TcpStream) -> Self::Future {
let stream = TcpStream::from_std(stream, &Handle::default()).map_err(|e| {
error!("Can not convert to an async tcp stream: {}", e);
});
if let Ok(stream) = stream {
2018-08-21 07:21:23 +02:00
let counter = self.counter.clone();
let _ = counter.fetch_add(1, Ordering::Relaxed);
Box::new(self.inner.call(stream).map_err(|_| ()).map(move |_| {
let _ = counter.fetch_sub(1, Ordering::Relaxed);
}))
2018-08-19 19:47:04 +02:00
} else {
Box::new(future::err(()))
}
}
}
pub(crate) struct ServerNewService<F, T, C> where F: Fn() -> T + Send + Clone {
inner: F,
2018-08-22 02:08:23 +02:00
config: C,
2018-08-21 07:21:23 +02:00
counter: Arc<AtomicUsize>,
2018-08-19 19:47:04 +02:00
}
impl<F, T, C: Config> ServerNewService<F, T, C>
2018-08-19 19:47:04 +02:00
where
F: Fn() -> T + Send + Clone + 'static,
T: NewService<Request = TcpStream, Response = (), Config = C, InitError = io::Error> + 'static,
2018-08-19 19:47:04 +02:00
T::Service: 'static,
T::Future: 'static,
T::Error: fmt::Display,
{
pub(crate) fn create(inner: F, config: C) -> Box<ServerServiceFactory<C> + Send> {
2018-08-21 07:21:23 +02:00
Box::new(Self {
inner,
2018-08-22 02:08:23 +02:00
config,
2018-08-21 07:21:23 +02:00
counter: Arc::new(AtomicUsize::new(0)),
})
2018-08-19 19:47:04 +02:00
}
}
2018-08-22 02:08:23 +02:00
pub trait ServerServiceFactory<C> {
2018-08-21 07:21:23 +02:00
fn counter(&self) -> Arc<AtomicUsize>;
2018-08-22 02:08:23 +02:00
fn clone_factory(&self) -> Box<ServerServiceFactory<C> + Send>;
2018-08-19 19:47:04 +02:00
fn create(&self) -> Box<Future<Item = BoxedServerService, Error = ()>>;
}
impl<F, T, C: Config> ServerServiceFactory<C> for ServerNewService<F, T, C>
2018-08-19 19:47:04 +02:00
where
F: Fn() -> T + Send + Clone + 'static,
2018-08-22 02:08:23 +02:00
T: NewService<Request = TcpStream, Response = (), Config = C, InitError = io::Error>
2018-08-19 19:47:04 +02:00
+ 'static,
T::Service: 'static,
T::Future: 'static,
T::Error: fmt::Display,
{
2018-08-21 07:21:23 +02:00
fn counter(&self) -> Arc<AtomicUsize> {
self.counter.clone()
}
2018-08-22 02:08:23 +02:00
fn clone_factory(&self) -> Box<ServerServiceFactory<C> + Send> {
2018-08-19 19:47:04 +02:00
Box::new(Self {
inner: self.inner.clone(),
2018-08-22 02:08:23 +02:00
config: self.config.fork(),
2018-08-21 07:21:23 +02:00
counter: Arc::new(AtomicUsize::new(0)),
2018-08-19 19:47:04 +02:00
})
}
fn create(&self) -> Box<Future<Item = BoxedServerService, Error = ()>> {
2018-08-21 07:21:23 +02:00
let counter = self.counter.clone();
2018-08-22 02:08:23 +02:00
Box::new(
(self.inner)()
2018-08-22 02:08:23 +02:00
.new_service(self.config.clone())
.map_err(|_| ())
.map(move |inner| {
let service: BoxedServerService =
Box::new(ServerService { inner, counter });
service
}),
)
2018-08-19 19:47:04 +02:00
}
}
2018-08-22 02:08:23 +02:00
impl<C> ServerServiceFactory<C> for Box<ServerServiceFactory<C>> {
2018-08-21 07:21:23 +02:00
fn counter(&self) -> Arc<AtomicUsize> {
self.as_ref().counter()
}
2018-08-22 02:08:23 +02:00
fn clone_factory(&self) -> Box<ServerServiceFactory<C> + Send> {
2018-08-19 19:47:04 +02:00
self.as_ref().clone_factory()
}
fn create(&self) -> Box<Future<Item = BoxedServerService, Error = ()>> {
self.as_ref().create()
}
}