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;
|
|
|
|
use std::task::{Context, Poll};
|
2019-06-26 11:19:40 +02:00
|
|
|
|
|
|
|
use actix_codec::{AsyncRead, AsyncWrite, Decoder, Encoder, Framed};
|
2019-12-29 08:42:42 +01:00
|
|
|
use actix_service::Service;
|
|
|
|
use actix_utils::mpsc;
|
2020-03-11 20:23:04 +01:00
|
|
|
use futures_core::stream::Stream;
|
2020-03-04 17:48:19 +01:00
|
|
|
use pin_project::pin_project;
|
2019-06-26 11:19:40 +02:00
|
|
|
use log::debug;
|
|
|
|
|
|
|
|
use crate::error::ServiceError;
|
|
|
|
|
2019-12-29 08:42:42 +01:00
|
|
|
type Request<U> = <U as Decoder>::Item;
|
2019-06-26 11:19:40 +02:00
|
|
|
type Response<U> = <U as Encoder>::Item;
|
|
|
|
|
|
|
|
/// FramedTransport - is a future that reads frames from Framed object
|
|
|
|
/// and pass then to the service.
|
2020-03-04 17:48:19 +01:00
|
|
|
#[pin_project]
|
2019-12-29 08:42:42 +01:00
|
|
|
pub(crate) struct Dispatcher<S, T, U, Out>
|
2019-06-26 11:19:40 +02:00
|
|
|
where
|
2019-12-29 08:42:42 +01:00
|
|
|
S: Service<Request = Request<U>, Response = Option<Response<U>>>,
|
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,
|
2019-12-29 08:42:42 +01:00
|
|
|
Out: Stream<Item = <U as Encoder>::Item> + Unpin,
|
2019-06-26 11:19:40 +02:00
|
|
|
{
|
|
|
|
service: S,
|
2019-12-29 08:42:42 +01:00
|
|
|
sink: Option<Out>,
|
|
|
|
state: FramedState<S, U>,
|
2020-03-04 17:48:19 +01:00
|
|
|
#[pin]
|
2019-06-26 11:19:40 +02:00
|
|
|
framed: Framed<T, U>,
|
2019-12-29 08:42:42 +01:00
|
|
|
rx: mpsc::Receiver<Result<<U as Encoder>::Item, S::Error>>,
|
2019-06-26 11:19:40 +02:00
|
|
|
}
|
|
|
|
|
2019-12-29 08:42:42 +01:00
|
|
|
impl<S, T, U, Out> Dispatcher<S, T, U, Out>
|
2019-06-26 11:19:40 +02:00
|
|
|
where
|
2019-12-29 08:42:42 +01:00
|
|
|
S: Service<Request = Request<U>, Response = Option<Response<U>>>,
|
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,
|
2019-12-29 08:42:42 +01:00
|
|
|
Out: Stream<Item = <U as Encoder>::Item> + Unpin,
|
2019-06-26 11:19:40 +02:00
|
|
|
{
|
2019-12-29 08:42:42 +01:00
|
|
|
pub(crate) fn new(framed: Framed<T, U>, service: S, sink: Option<Out>) -> Self {
|
2019-12-11 09:36:11 +01:00
|
|
|
Dispatcher {
|
2019-06-26 11:19:40 +02:00
|
|
|
sink,
|
2019-12-29 08:42:42 +01:00
|
|
|
service,
|
|
|
|
framed,
|
|
|
|
rx: mpsc::channel().1,
|
|
|
|
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>),
|
2019-12-29 08:42:42 +01:00
|
|
|
FlushAndStop,
|
2019-06-26 11:19:40 +02:00
|
|
|
Stopping,
|
|
|
|
}
|
|
|
|
|
2019-11-19 09:51:40 +01:00
|
|
|
impl<S: Service, U: Encoder + Decoder> FramedState<S, U> {
|
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-29 08:42:42 +01:00
|
|
|
impl<S, T, U, Out> Dispatcher<S, T, U, Out>
|
2019-11-14 13:38:24 +01:00
|
|
|
where
|
2019-12-29 08:42:42 +01:00
|
|
|
S: Service<Request = Request<U>, Response = Option<Response<U>>>,
|
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-29 08:42:42 +01:00
|
|
|
Out: Stream<Item = <U as Encoder>::Item> + Unpin,
|
2019-11-14 13:38:24 +01:00
|
|
|
{
|
2020-03-04 17:48:19 +01:00
|
|
|
fn poll_read(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> bool {
|
2019-12-11 09:36:11 +01:00
|
|
|
loop {
|
2020-03-04 17:48:19 +01:00
|
|
|
let this = self.as_mut().project();
|
|
|
|
match this.service.poll_ready(cx) {
|
2019-12-11 09:36:11 +01:00
|
|
|
Poll::Ready(Ok(_)) => {
|
2020-03-04 17:48:19 +01:00
|
|
|
let item = match this.framed.next_item(cx) {
|
2019-12-11 09:36:11 +01:00
|
|
|
Poll::Ready(Some(Ok(el))) => el,
|
|
|
|
Poll::Ready(Some(Err(err))) => {
|
2020-03-04 17:48:19 +01:00
|
|
|
*this.state = FramedState::FramedError(ServiceError::Decoder(err));
|
2019-12-11 09:36:11 +01:00
|
|
|
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");
|
2020-03-04 17:48:19 +01:00
|
|
|
*this.state = FramedState::Stopping;
|
2019-11-14 13:38:24 +01:00
|
|
|
return true;
|
|
|
|
}
|
2019-12-11 09:36:11 +01:00
|
|
|
};
|
|
|
|
|
2020-03-04 17:48:19 +01:00
|
|
|
let tx = this.rx.sender();
|
|
|
|
let fut = this.service.call(item);
|
2019-12-29 08:42:42 +01:00
|
|
|
actix_rt::spawn(async move {
|
|
|
|
let item = fut.await;
|
|
|
|
let item = match item {
|
|
|
|
Ok(Some(item)) => Ok(item),
|
|
|
|
Ok(None) => return,
|
|
|
|
Err(err) => Err(err),
|
|
|
|
};
|
|
|
|
let _ = tx.send(item);
|
|
|
|
});
|
2019-12-11 09:36:11 +01:00
|
|
|
}
|
|
|
|
Poll::Pending => return false,
|
|
|
|
Poll::Ready(Err(err)) => {
|
2020-03-04 17:48:19 +01:00
|
|
|
*this.state = FramedState::Error(ServiceError::Service(err));
|
2019-12-11 09:36:11 +01:00
|
|
|
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
|
2020-03-04 17:48:19 +01:00
|
|
|
fn poll_write(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> bool {
|
2019-12-11 09:36:11 +01:00
|
|
|
loop {
|
2020-03-04 17:48:19 +01:00
|
|
|
let mut this = self.as_mut().project();
|
|
|
|
while !this.framed.is_write_buf_full() {
|
|
|
|
match Pin::new(&mut this.rx).poll_next(cx) {
|
2019-12-29 08:42:42 +01:00
|
|
|
Poll::Ready(Some(Ok(msg))) => {
|
2020-03-04 17:48:19 +01:00
|
|
|
if let Err(err) = this.framed.as_mut().write(msg) {
|
|
|
|
*this.state = FramedState::FramedError(ServiceError::Encoder(err));
|
2019-11-14 13:38:24 +01:00
|
|
|
return true;
|
2019-06-26 11:19:40 +02:00
|
|
|
}
|
2019-12-29 08:42:42 +01:00
|
|
|
continue;
|
2019-11-14 13:38:24 +01:00
|
|
|
}
|
2019-12-11 09:36:11 +01:00
|
|
|
Poll::Ready(Some(Err(err))) => {
|
2020-03-04 17:48:19 +01:00
|
|
|
*this.state = FramedState::Error(ServiceError::Service(err));
|
2019-12-11 09:36:11 +01:00
|
|
|
return true;
|
2019-11-14 13:38:24 +01:00
|
|
|
}
|
2019-12-29 08:42:42 +01:00
|
|
|
Poll::Ready(None) | Poll::Pending => (),
|
|
|
|
}
|
|
|
|
|
2020-03-04 17:48:19 +01:00
|
|
|
if this.sink.is_some() {
|
|
|
|
match Pin::new(this.sink.as_mut().unwrap()).poll_next(cx) {
|
2019-12-29 08:42:42 +01:00
|
|
|
Poll::Ready(Some(msg)) => {
|
2020-03-04 17:48:19 +01:00
|
|
|
if let Err(err) = this.framed.as_mut().write(msg) {
|
|
|
|
*this.state =
|
2019-12-29 08:42:42 +01:00
|
|
|
FramedState::FramedError(ServiceError::Encoder(err));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Poll::Ready(None) => {
|
2020-03-04 17:48:19 +01:00
|
|
|
let _ = this.sink.take();
|
|
|
|
*this.state = FramedState::FlushAndStop;
|
2019-12-29 08:42:42 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
Poll::Pending => (),
|
|
|
|
}
|
2019-06-26 11:19:40 +02:00
|
|
|
}
|
2019-12-29 08:42:42 +01:00
|
|
|
break;
|
2019-06-26 11:19:40 +02:00
|
|
|
}
|
2019-11-14 13:38:24 +01:00
|
|
|
|
2020-03-04 17:48:19 +01:00
|
|
|
if !this.framed.is_write_buf_empty() {
|
|
|
|
match this.framed.as_mut().flush(cx) {
|
2019-12-11 09:36:11 +01:00
|
|
|
Poll::Pending => break,
|
|
|
|
Poll::Ready(Ok(_)) => (),
|
|
|
|
Poll::Ready(Err(err)) => {
|
|
|
|
debug!("Error sending data: {:?}", err);
|
2020-03-04 17:48:19 +01:00
|
|
|
*this.state = FramedState::FramedError(ServiceError::Encoder(err));
|
2019-12-11 09:36:11 +01:00
|
|
|
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(
|
2020-03-04 17:48:19 +01:00
|
|
|
mut self: Pin<&mut Self>,
|
2019-12-11 09:36:11 +01:00
|
|
|
cx: &mut Context<'_>,
|
|
|
|
) -> Poll<Result<(), ServiceError<S::Error, U>>> {
|
2020-03-04 17:48:19 +01:00
|
|
|
let mut this = self.as_mut().project();
|
|
|
|
match this.state {
|
2019-12-29 08:42:42 +01:00
|
|
|
FramedState::Processing => loop {
|
2020-03-04 17:48:19 +01:00
|
|
|
let read = self.as_mut().poll_read(cx);
|
|
|
|
let write = self.as_mut().poll_write(cx);
|
2019-12-29 08:42:42 +01:00
|
|
|
if read || write {
|
|
|
|
continue;
|
2019-12-11 09:36:11 +01:00
|
|
|
} else {
|
2019-12-29 08:42:42 +01:00
|
|
|
return Poll::Pending;
|
2019-12-11 09:36:11 +01:00
|
|
|
}
|
2019-12-29 08:42:42 +01:00
|
|
|
},
|
2019-12-11 09:36:11 +01:00
|
|
|
FramedState::Error(_) => {
|
|
|
|
// flush write buffer
|
2020-03-04 17:48:19 +01:00
|
|
|
if !this.framed.is_write_buf_empty() {
|
|
|
|
if let Poll::Pending = this.framed.flush(cx) {
|
2019-12-11 09:36:11 +01:00
|
|
|
return Poll::Pending;
|
|
|
|
}
|
|
|
|
}
|
2020-03-04 17:48:19 +01:00
|
|
|
Poll::Ready(Err(this.state.take_error()))
|
2019-12-11 09:36:11 +01:00
|
|
|
}
|
2019-12-29 08:42:42 +01:00
|
|
|
FramedState::FlushAndStop => {
|
|
|
|
// drain service responses
|
2020-03-04 17:48:19 +01:00
|
|
|
match Pin::new(this.rx).poll_next(cx) {
|
2019-12-29 08:42:42 +01:00
|
|
|
Poll::Ready(Some(Ok(msg))) => {
|
2020-03-04 17:48:19 +01:00
|
|
|
if this.framed.as_mut().write(msg).is_err() {
|
2019-12-29 08:42:42 +01:00
|
|
|
return Poll::Ready(Ok(()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Poll::Ready(Some(Err(_))) => return Poll::Ready(Ok(())),
|
|
|
|
Poll::Ready(None) | Poll::Pending => (),
|
|
|
|
}
|
|
|
|
|
|
|
|
// flush io
|
2020-03-04 17:48:19 +01:00
|
|
|
if !this.framed.is_write_buf_empty() {
|
|
|
|
match this.framed.flush(cx) {
|
2019-12-11 09:36:11 +01:00
|
|
|
Poll::Ready(Err(err)) => {
|
|
|
|
debug!("Error sending data: {:?}", err);
|
|
|
|
}
|
|
|
|
Poll::Pending => {
|
|
|
|
return Poll::Pending;
|
|
|
|
}
|
|
|
|
Poll::Ready(_) => (),
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Poll::Ready(Ok(()))
|
2019-07-03 09:02:03 +02:00
|
|
|
}
|
2020-03-04 17:48:19 +01:00
|
|
|
FramedState::FramedError(_) => Poll::Ready(Err(this.state.take_framed_error())),
|
2019-12-29 08:42:42 +01:00
|
|
|
FramedState::Stopping => Poll::Ready(Ok(())),
|
2019-06-26 11:19:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|