1
0
mirror of https://github.com/fafhrd91/actix-net synced 2024-11-30 22:14:35 +01:00
actix-net/src/server/services.rs

132 lines
3.2 KiB
Rust
Raw Normal View History

use std::net;
2018-08-19 19:47:04 +02:00
2018-09-07 22:06:51 +02:00
use futures::future::{err, ok};
2018-09-08 18:36:38 +02:00
use futures::{Future, Poll};
2018-08-19 19:47:04 +02:00
use tokio_reactor::Handle;
use tokio_tcp::TcpStream;
2018-08-22 02:08:23 +02:00
2018-09-11 18:30:22 +02:00
use service::{NewService, Service};
2018-08-19 19:47:04 +02:00
2018-09-07 22:06:51 +02:00
pub enum ServerMessage {
Connect(net::TcpStream),
Shutdown,
ForceShutdown,
}
2018-08-19 19:47:04 +02:00
pub(crate) type BoxedServerService = Box<
Service<
2018-09-07 22:06:51 +02:00
Request = ServerMessage,
2018-08-19 19:47:04 +02:00
Response = (),
Error = (),
Future = Box<Future<Item = (), Error = ()>>,
>,
>;
pub(crate) struct ServerService<T> {
2018-09-07 20:35:25 +02:00
service: T,
}
impl<T> ServerService<T> {
fn new(service: T) -> Self {
2018-09-08 18:36:38 +02:00
ServerService { service }
2018-09-07 20:35:25 +02:00
}
2018-08-19 19:47:04 +02:00
}
impl<T> Service for ServerService<T>
where
T: Service<Request = TcpStream, Response = (), Error = ()>,
2018-08-19 19:47:04 +02:00
T::Future: 'static,
T::Error: 'static,
2018-08-19 19:47:04 +02:00
{
2018-09-07 22:06:51 +02:00
type Request = ServerMessage;
2018-08-19 19:47:04 +02:00
type Response = ();
type Error = ();
type Future = Box<Future<Item = (), Error = ()>>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
2018-09-08 18:36:38 +02:00
self.service.poll_ready().map_err(|_| ())
2018-08-19 19:47:04 +02:00
}
2018-09-07 22:06:51 +02:00
fn call(&mut self, req: ServerMessage) -> Self::Future {
match req {
ServerMessage::Connect(stream) => {
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-09-08 18:36:38 +02:00
Box::new(self.service.call(stream))
2018-09-07 22:06:51 +02:00
} else {
Box::new(err(()))
}
}
_ => Box::new(ok(())),
2018-08-19 19:47:04 +02:00
}
}
}
2018-09-08 23:50:16 +02:00
pub(crate) struct ServerNewService<F: ServerServiceFactory> {
inner: F,
2018-08-19 19:47:04 +02:00
}
2018-09-08 23:50:16 +02:00
impl<F> ServerNewService<F>
2018-08-19 19:47:04 +02:00
where
2018-09-08 23:50:16 +02:00
F: ServerServiceFactory,
2018-08-19 19:47:04 +02:00
{
2018-09-08 23:50:16 +02:00
pub(crate) fn create(inner: F) -> Box<InternalServerServiceFactory> {
2018-09-07 20:35:25 +02:00
Box::new(Self { inner })
2018-08-19 19:47:04 +02:00
}
}
2018-09-08 23:50:16 +02:00
pub(crate) trait InternalServerServiceFactory: Send {
fn clone_factory(&self) -> Box<InternalServerServiceFactory>;
2018-08-19 19:47:04 +02:00
fn create(&self) -> Box<Future<Item = BoxedServerService, Error = ()>>;
}
2018-09-08 23:50:16 +02:00
impl<F> InternalServerServiceFactory for ServerNewService<F>
2018-08-19 19:47:04 +02:00
where
2018-09-08 23:50:16 +02:00
F: ServerServiceFactory,
2018-08-19 19:47:04 +02:00
{
2018-09-08 23:50:16 +02:00
fn clone_factory(&self) -> Box<InternalServerServiceFactory> {
2018-08-19 19:47:04 +02:00
Box::new(Self {
inner: self.inner.clone(),
})
}
fn create(&self) -> Box<Future<Item = BoxedServerService, Error = ()>> {
2018-09-08 23:50:16 +02:00
Box::new(self.inner.create().new_service().map(move |inner| {
2018-09-07 22:06:51 +02:00
let service: BoxedServerService = Box::new(ServerService::new(inner));
service
}))
2018-08-19 19:47:04 +02:00
}
}
2018-09-08 23:50:16 +02:00
impl InternalServerServiceFactory for Box<InternalServerServiceFactory> {
fn clone_factory(&self) -> Box<InternalServerServiceFactory> {
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()
}
}
2018-09-08 23:50:16 +02:00
pub trait ServerServiceFactory: Send + Clone + 'static {
type NewService: NewService<Request = TcpStream, Response = (), Error = (), InitError = ()>;
fn create(&self) -> Self::NewService;
}
impl<F, T> ServerServiceFactory for F
where
F: Fn() -> T + Send + Clone + 'static,
T: NewService<Request = TcpStream, Response = (), Error = (), InitError = ()>,
{
type NewService = T;
fn create(&self) -> T {
(self)()
}
}