2019-06-26 11:19:40 +02:00
|
|
|
//! Framed dispatcher service and related utilities
|
2019-11-14 13:38:24 +01:00
|
|
|
use std::pin::Pin;
|
2019-07-02 08:10:05 +02:00
|
|
|
use std::rc::Rc;
|
2019-11-14 13:38:24 +01:00
|
|
|
use std::task::{Context, Poll};
|
2019-06-26 11:19:40 +02:00
|
|
|
|
|
|
|
use actix_codec::{AsyncRead, AsyncWrite, Decoder, Encoder, Framed};
|
|
|
|
use actix_service::{IntoService, Service};
|
2019-11-14 13:38:24 +01:00
|
|
|
use actix_utils::{mpsc, oneshot};
|
2019-12-11 09:36:11 +01:00
|
|
|
use futures::{FutureExt, Stream};
|
2019-06-26 11:19:40 +02:00
|
|
|
use log::debug;
|
|
|
|
|
|
|
|
use crate::error::ServiceError;
|
|
|
|
use crate::item::Item;
|
|
|
|
use crate::sink::Sink;
|
|
|
|
|
2019-12-11 09:36:11 +01:00
|
|
|
type Request<S, U, E> = Item<S, U, E>;
|
2019-06-26 11:19:40 +02:00
|
|
|
type Response<U> = <U as Encoder>::Item;
|
|
|
|
|
2019-12-11 09:36:11 +01:00
|
|
|
pub(crate) enum Message<T> {
|
|
|
|
Item(T),
|
2019-06-26 11:19:40 +02:00
|
|
|
WaitClose(oneshot::Sender<()>),
|
2019-12-11 09:36:11 +01:00
|
|
|
Close,
|
2019-06-26 11:19:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// FramedTransport - is a future that reads frames from Framed object
|
|
|
|
/// and pass then to the service.
|
2019-11-19 09:51:40 +01:00
|
|
|
#[pin_project::pin_project]
|
2019-12-11 09:36:11 +01:00
|
|
|
pub(crate) struct Dispatcher<St, S, T, U, E>
|
2019-06-26 11:19:40 +02:00
|
|
|
where
|
2019-12-02 16:27:48 +01:00
|
|
|
St: Clone,
|
2019-12-11 09:36:11 +01:00
|
|
|
S: Service<Request = Request<St, U, E>, Response = Option<Response<U>>, Error = E>,
|
2019-06-26 11:19:40 +02:00
|
|
|
S::Error: 'static,
|
|
|
|
S::Future: 'static,
|
2019-11-19 09:51:40 +01:00
|
|
|
T: AsyncRead + AsyncWrite,
|
|
|
|
U: Encoder + Decoder,
|
2019-06-26 11:19:40 +02:00
|
|
|
<U as Encoder>::Item: 'static,
|
|
|
|
<U as Encoder>::Error: std::fmt::Debug,
|
|
|
|
{
|
|
|
|
service: S,
|
2019-12-11 09:36:11 +01:00
|
|
|
sink: Sink<<U as Encoder>::Item, E>,
|
2019-12-02 16:27:48 +01:00
|
|
|
state: St,
|
2019-07-01 07:20:24 +02:00
|
|
|
dispatch_state: FramedState<S, U>,
|
2019-06-26 11:19:40 +02:00
|
|
|
framed: Framed<T, U>,
|
2019-12-11 09:36:11 +01:00
|
|
|
rx: mpsc::Receiver<Result<Message<<U as Encoder>::Item>, E>>,
|
|
|
|
tx: mpsc::Sender<Result<Message<<U as Encoder>::Item>, E>>,
|
2019-08-16 22:15:51 +02:00
|
|
|
disconnect: Option<Rc<dyn Fn(&mut St, bool)>>,
|
2019-06-26 11:19:40 +02:00
|
|
|
}
|
|
|
|
|
2019-12-11 09:36:11 +01:00
|
|
|
impl<St, S, T, U, E> Dispatcher<St, S, T, U, E>
|
2019-06-26 11:19:40 +02:00
|
|
|
where
|
2019-12-02 16:27:48 +01:00
|
|
|
St: Clone,
|
2019-12-11 09:36:11 +01:00
|
|
|
S: Service<Request = Request<St, U, E>, Response = Option<Response<U>>, Error = E>,
|
2019-06-26 11:19:40 +02:00
|
|
|
S::Error: 'static,
|
|
|
|
S::Future: 'static,
|
2019-11-19 09:51:40 +01:00
|
|
|
T: AsyncRead + AsyncWrite,
|
|
|
|
U: Decoder + Encoder,
|
2019-06-26 11:19:40 +02:00
|
|
|
<U as Encoder>::Item: 'static,
|
|
|
|
<U as Encoder>::Error: std::fmt::Debug,
|
|
|
|
{
|
|
|
|
pub(crate) fn new<F: IntoService<S>>(
|
|
|
|
framed: Framed<T, U>,
|
2019-12-02 16:27:48 +01:00
|
|
|
state: St,
|
2019-06-26 11:19:40 +02:00
|
|
|
service: F,
|
2019-12-11 09:36:11 +01:00
|
|
|
sink: Sink<<U as Encoder>::Item, E>,
|
|
|
|
rx: mpsc::Receiver<Result<Message<<U as Encoder>::Item>, E>>,
|
2019-08-16 22:15:51 +02:00
|
|
|
disconnect: Option<Rc<dyn Fn(&mut St, bool)>>,
|
2019-06-26 11:19:40 +02:00
|
|
|
) -> Self {
|
2019-12-11 09:36:11 +01:00
|
|
|
let tx = rx.sender();
|
|
|
|
|
|
|
|
Dispatcher {
|
2019-06-26 11:19:40 +02:00
|
|
|
framed,
|
|
|
|
state,
|
|
|
|
sink,
|
2019-07-02 08:10:05 +02:00
|
|
|
disconnect,
|
2019-12-11 09:36:11 +01:00
|
|
|
rx,
|
|
|
|
tx,
|
2019-06-26 11:19:40 +02:00
|
|
|
service: service.into_service(),
|
2019-07-01 07:20:24 +02:00
|
|
|
dispatch_state: FramedState::Processing,
|
2019-06-26 11:19:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-19 09:51:40 +01:00
|
|
|
enum FramedState<S: Service, U: Encoder + Decoder> {
|
2019-06-26 11:19:40 +02:00
|
|
|
Processing,
|
|
|
|
Error(ServiceError<S::Error, U>),
|
|
|
|
FramedError(ServiceError<S::Error, U>),
|
|
|
|
FlushAndStop(Vec<oneshot::Sender<()>>),
|
|
|
|
Stopping,
|
|
|
|
}
|
|
|
|
|
2019-11-19 09:51:40 +01:00
|
|
|
impl<S: Service, U: Encoder + Decoder> FramedState<S, U> {
|
2019-06-26 11:19:40 +02:00
|
|
|
fn stop(&mut self, tx: Option<oneshot::Sender<()>>) {
|
|
|
|
match self {
|
2019-07-01 07:20:24 +02:00
|
|
|
FramedState::FlushAndStop(ref mut vec) => {
|
2019-06-26 11:19:40 +02:00
|
|
|
if let Some(tx) = tx {
|
|
|
|
vec.push(tx)
|
|
|
|
}
|
|
|
|
}
|
2019-07-01 07:20:24 +02:00
|
|
|
FramedState::Processing => {
|
|
|
|
*self = FramedState::FlushAndStop(if let Some(tx) = tx {
|
2019-06-26 11:19:40 +02:00
|
|
|
vec![tx]
|
|
|
|
} else {
|
|
|
|
Vec::new()
|
|
|
|
})
|
|
|
|
}
|
2019-07-01 07:20:24 +02:00
|
|
|
FramedState::Error(_) | FramedState::FramedError(_) | FramedState::Stopping => {
|
2019-06-26 11:19:40 +02:00
|
|
|
if let Some(tx) = tx {
|
|
|
|
let _ = tx.send(());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-11 09:36:11 +01:00
|
|
|
fn take_error(&mut self) -> ServiceError<S::Error, U> {
|
|
|
|
match std::mem::replace(self, FramedState::Processing) {
|
|
|
|
FramedState::Error(err) => err,
|
|
|
|
_ => panic!(),
|
|
|
|
}
|
2019-11-14 13:38:24 +01:00
|
|
|
}
|
|
|
|
|
2019-12-11 09:36:11 +01:00
|
|
|
fn take_framed_error(&mut self) -> ServiceError<S::Error, U> {
|
|
|
|
match std::mem::replace(self, FramedState::Processing) {
|
|
|
|
FramedState::FramedError(err) => err,
|
|
|
|
_ => panic!(),
|
2019-11-14 13:38:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-26 11:19:40 +02:00
|
|
|
|
2019-12-11 09:36:11 +01:00
|
|
|
impl<St, S, T, U, E> Dispatcher<St, S, T, U, E>
|
2019-11-14 13:38:24 +01:00
|
|
|
where
|
2019-12-02 16:27:48 +01:00
|
|
|
St: Clone,
|
2019-12-11 09:36:11 +01:00
|
|
|
S: Service<Request = Request<St, U, E>, Response = Option<Response<U>>, Error = E>,
|
2019-11-14 13:38:24 +01:00
|
|
|
S::Error: 'static,
|
|
|
|
S::Future: 'static,
|
2019-11-19 09:51:40 +01:00
|
|
|
T: AsyncRead + AsyncWrite,
|
|
|
|
U: Decoder + Encoder,
|
2019-11-14 13:38:24 +01:00
|
|
|
<U as Encoder>::Item: 'static,
|
|
|
|
<U as Encoder>::Error: std::fmt::Debug,
|
|
|
|
{
|
2019-12-11 09:36:11 +01:00
|
|
|
fn poll_read(&mut self, cx: &mut Context<'_>) -> bool {
|
|
|
|
loop {
|
|
|
|
match self.service.poll_ready(cx) {
|
|
|
|
Poll::Ready(Ok(_)) => {
|
|
|
|
let item = match self.framed.next_item(cx) {
|
|
|
|
Poll::Ready(Some(Ok(el))) => el,
|
|
|
|
Poll::Ready(Some(Err(err))) => {
|
|
|
|
self.dispatch_state =
|
|
|
|
FramedState::FramedError(ServiceError::Decoder(err));
|
|
|
|
return true;
|
2019-11-26 03:26:22 +01:00
|
|
|
}
|
2019-12-11 09:36:11 +01:00
|
|
|
Poll::Pending => return false,
|
|
|
|
Poll::Ready(None) => {
|
|
|
|
log::trace!("Client disconnected");
|
|
|
|
self.dispatch_state = FramedState::Stopping;
|
2019-11-14 13:38:24 +01:00
|
|
|
return true;
|
|
|
|
}
|
2019-12-11 09:36:11 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
let tx = self.tx.clone();
|
|
|
|
actix_rt::spawn(
|
|
|
|
self.service
|
|
|
|
.call(Item::new(self.state.clone(), self.sink.clone(), item))
|
|
|
|
.map(move |item| {
|
|
|
|
let item = match item {
|
|
|
|
Ok(Some(item)) => Ok(Message::Item(item)),
|
|
|
|
Ok(None) => return,
|
|
|
|
Err(err) => Err(err),
|
|
|
|
};
|
|
|
|
let _ = tx.send(item);
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
Poll::Pending => return false,
|
|
|
|
Poll::Ready(Err(err)) => {
|
|
|
|
self.dispatch_state = FramedState::Error(ServiceError::Service(err));
|
|
|
|
return true;
|
2019-06-26 11:19:40 +02:00
|
|
|
}
|
|
|
|
}
|
2019-12-11 09:36:11 +01:00
|
|
|
}
|
|
|
|
}
|
2019-11-14 13:38:24 +01:00
|
|
|
|
2019-12-11 09:36:11 +01:00
|
|
|
/// write to framed object
|
|
|
|
fn poll_write(&mut self, cx: &mut Context<'_>) -> bool {
|
|
|
|
loop {
|
|
|
|
while !self.framed.is_write_buf_full() {
|
|
|
|
match Pin::new(&mut self.rx).poll_next(cx) {
|
|
|
|
Poll::Ready(Some(Ok(Message::Item(msg)))) => {
|
|
|
|
if let Err(err) = self.framed.write(msg) {
|
|
|
|
self.dispatch_state =
|
2019-11-14 13:38:24 +01:00
|
|
|
FramedState::FramedError(ServiceError::Encoder(err));
|
|
|
|
return true;
|
2019-06-26 11:19:40 +02:00
|
|
|
}
|
|
|
|
}
|
2019-12-11 09:36:11 +01:00
|
|
|
Poll::Ready(Some(Ok(Message::Close))) => {
|
|
|
|
self.dispatch_state.stop(None);
|
2019-11-14 13:38:24 +01:00
|
|
|
return true;
|
|
|
|
}
|
2019-12-11 09:36:11 +01:00
|
|
|
Poll::Ready(Some(Ok(Message::WaitClose(tx)))) => {
|
|
|
|
self.dispatch_state.stop(Some(tx));
|
2019-11-14 13:38:24 +01:00
|
|
|
return true;
|
|
|
|
}
|
2019-12-11 09:36:11 +01:00
|
|
|
Poll::Ready(Some(Err(err))) => {
|
|
|
|
self.dispatch_state = FramedState::Error(ServiceError::Service(err));
|
|
|
|
return true;
|
2019-11-14 13:38:24 +01:00
|
|
|
}
|
2019-12-11 09:36:11 +01:00
|
|
|
Poll::Ready(None) | Poll::Pending => break,
|
2019-06-26 11:19:40 +02:00
|
|
|
}
|
|
|
|
}
|
2019-11-14 13:38:24 +01:00
|
|
|
|
2019-12-11 09:36:11 +01:00
|
|
|
if !self.framed.is_write_buf_empty() {
|
|
|
|
match self.framed.flush(cx) {
|
|
|
|
Poll::Pending => break,
|
|
|
|
Poll::Ready(Ok(_)) => (),
|
|
|
|
Poll::Ready(Err(err)) => {
|
|
|
|
debug!("Error sending data: {:?}", err);
|
|
|
|
self.dispatch_state =
|
|
|
|
FramedState::FramedError(ServiceError::Encoder(err));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2019-11-14 13:38:24 +01:00
|
|
|
break;
|
2019-07-02 08:10:05 +02:00
|
|
|
}
|
2019-11-14 13:38:24 +01:00
|
|
|
}
|
2019-12-11 09:36:11 +01:00
|
|
|
false
|
|
|
|
}
|
2019-11-14 13:38:24 +01:00
|
|
|
|
2019-12-11 09:36:11 +01:00
|
|
|
pub(crate) fn poll(
|
|
|
|
&mut self,
|
|
|
|
cx: &mut Context<'_>,
|
|
|
|
) -> Poll<Result<(), ServiceError<S::Error, U>>> {
|
|
|
|
match self.dispatch_state {
|
|
|
|
FramedState::Processing => {
|
|
|
|
if self.poll_read(cx) || self.poll_write(cx) {
|
|
|
|
self.poll(cx)
|
|
|
|
} else {
|
|
|
|
Poll::Pending
|
|
|
|
}
|
|
|
|
}
|
|
|
|
FramedState::Error(_) => {
|
|
|
|
// flush write buffer
|
|
|
|
if !self.framed.is_write_buf_empty() {
|
|
|
|
if let Poll::Pending = self.framed.flush(cx) {
|
|
|
|
return Poll::Pending;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Some(ref disconnect) = self.disconnect {
|
|
|
|
(&*disconnect)(&mut self.state, true);
|
2019-11-14 13:38:24 +01:00
|
|
|
}
|
2019-12-11 09:36:11 +01:00
|
|
|
Poll::Ready(Err(self.dispatch_state.take_error()))
|
|
|
|
}
|
|
|
|
FramedState::FlushAndStop(ref mut vec) => {
|
|
|
|
if !self.framed.is_write_buf_empty() {
|
|
|
|
match self.framed.flush(cx) {
|
|
|
|
Poll::Ready(Err(err)) => {
|
|
|
|
debug!("Error sending data: {:?}", err);
|
|
|
|
}
|
|
|
|
Poll::Pending => {
|
|
|
|
return Poll::Pending;
|
|
|
|
}
|
|
|
|
Poll::Ready(_) => (),
|
|
|
|
}
|
|
|
|
};
|
|
|
|
for tx in vec.drain(..) {
|
|
|
|
let _ = tx.send(());
|
|
|
|
}
|
|
|
|
if let Some(ref disconnect) = self.disconnect {
|
|
|
|
(&*disconnect)(&mut self.state, false);
|
|
|
|
}
|
|
|
|
Poll::Ready(Ok(()))
|
|
|
|
}
|
|
|
|
FramedState::FramedError(_) => {
|
|
|
|
if let Some(ref disconnect) = self.disconnect {
|
|
|
|
(&*disconnect)(&mut self.state, true);
|
|
|
|
}
|
|
|
|
Poll::Ready(Err(self.dispatch_state.take_framed_error()))
|
|
|
|
}
|
|
|
|
FramedState::Stopping => {
|
|
|
|
if let Some(ref disconnect) = self.disconnect {
|
|
|
|
(&*disconnect)(&mut self.state, false);
|
|
|
|
}
|
|
|
|
Poll::Ready(Ok(()))
|
2019-07-03 09:02:03 +02:00
|
|
|
}
|
2019-06-26 11:19:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|