2019-11-18 13:42:27 +01:00
|
|
|
use std::future::Future;
|
|
|
|
use std::pin::Pin;
|
|
|
|
use std::task::{Context, Poll};
|
|
|
|
|
2018-12-11 03:08:33 +01:00
|
|
|
use actix_codec::{AsyncRead, AsyncWrite, Framed};
|
|
|
|
use actix_service::{IntoService, Service};
|
2020-08-24 11:13:35 +02:00
|
|
|
use actix_utils::dispatcher::{Dispatcher as InnerDispatcher, DispatcherError};
|
2018-10-05 23:30:40 +02:00
|
|
|
|
2018-10-10 22:20:00 +02:00
|
|
|
use super::{Codec, Frame, Message};
|
2018-10-05 23:30:40 +02:00
|
|
|
|
2020-08-24 11:13:35 +02:00
|
|
|
#[pin_project::pin_project]
|
2019-12-11 14:20:20 +01:00
|
|
|
pub struct Dispatcher<S, T>
|
2018-10-05 23:30:40 +02:00
|
|
|
where
|
2019-03-09 16:37:23 +01:00
|
|
|
S: Service<Request = Frame, Response = Message> + 'static,
|
2019-11-19 13:54:19 +01:00
|
|
|
T: AsyncRead + AsyncWrite,
|
2018-10-05 23:30:40 +02:00
|
|
|
{
|
2020-08-24 11:13:35 +02:00
|
|
|
#[pin]
|
|
|
|
inner: InnerDispatcher<S, T, Codec, Message>,
|
2018-10-05 23:30:40 +02:00
|
|
|
}
|
|
|
|
|
2019-12-11 14:20:20 +01:00
|
|
|
impl<S, T> Dispatcher<S, T>
|
2018-10-05 23:30:40 +02:00
|
|
|
where
|
2019-11-19 13:54:19 +01:00
|
|
|
T: AsyncRead + AsyncWrite,
|
|
|
|
S: Service<Request = Frame, Response = Message>,
|
2018-10-05 23:30:40 +02:00
|
|
|
S::Future: 'static,
|
2019-11-19 13:54:19 +01:00
|
|
|
S::Error: 'static,
|
2018-10-05 23:30:40 +02:00
|
|
|
{
|
2019-03-09 16:37:23 +01:00
|
|
|
pub fn new<F: IntoService<S>>(io: T, service: F) -> Self {
|
2019-12-11 14:20:20 +01:00
|
|
|
Dispatcher {
|
2020-08-24 11:13:35 +02:00
|
|
|
inner: InnerDispatcher::new(Framed::new(io, Codec::new()), service),
|
2018-10-05 23:30:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-09 16:37:23 +01:00
|
|
|
pub fn with<F: IntoService<S>>(framed: Framed<T, Codec>, service: F) -> Self {
|
2019-12-11 14:20:20 +01:00
|
|
|
Dispatcher {
|
2020-08-24 11:13:35 +02:00
|
|
|
inner: InnerDispatcher::new(framed, service),
|
2018-10-05 23:30:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-11 14:20:20 +01:00
|
|
|
impl<S, T> Future for Dispatcher<S, T>
|
2018-10-05 23:30:40 +02:00
|
|
|
where
|
2019-11-19 13:54:19 +01:00
|
|
|
T: AsyncRead + AsyncWrite,
|
|
|
|
S: Service<Request = Frame, Response = Message>,
|
2018-10-05 23:30:40 +02:00
|
|
|
S::Future: 'static,
|
2019-11-19 13:54:19 +01:00
|
|
|
S::Error: 'static,
|
2018-10-05 23:30:40 +02:00
|
|
|
{
|
2020-08-24 11:13:35 +02:00
|
|
|
type Output = Result<(), DispatcherError<S::Error, Codec, Message>>;
|
2018-10-05 23:30:40 +02:00
|
|
|
|
2020-08-24 11:13:35 +02:00
|
|
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
|
|
|
self.project().inner.poll(cx)
|
2018-10-05 23:30:40 +02:00
|
|
|
}
|
|
|
|
}
|