2018-09-12 18:47:39 -07:00
|
|
|
//! Framed dispatcher service and related utilities
|
2019-11-14 18:38:24 +06:00
|
|
|
#![allow(type_alias_bounds)]
|
|
|
|
use std::pin::Pin;
|
|
|
|
use std::task::{Context, Poll};
|
2019-05-12 06:03:50 -07:00
|
|
|
use std::{fmt, mem};
|
2018-09-12 18:47:39 -07:00
|
|
|
|
2018-12-10 16:16:40 -08:00
|
|
|
use actix_codec::{AsyncRead, AsyncWrite, Decoder, Encoder, Framed};
|
2019-05-12 06:03:50 -07:00
|
|
|
use actix_service::{IntoService, Service};
|
2020-07-19 21:44:26 +01:00
|
|
|
use futures_util::{future::Future, stream::Stream, FutureExt};
|
2018-12-16 16:26:24 -08:00
|
|
|
use log::debug;
|
2018-09-12 18:47:39 -07:00
|
|
|
|
2019-11-14 18:38:24 +06:00
|
|
|
use crate::mpsc;
|
2019-01-26 21:41:28 -08:00
|
|
|
|
2019-05-12 06:03:50 -07:00
|
|
|
/// Framed transport errors
|
2020-07-19 21:44:26 +01:00
|
|
|
pub enum DispatcherError<E, U: Encoder<I> + Decoder, I> {
|
2019-05-12 06:03:50 -07:00
|
|
|
Service(E),
|
2020-07-19 21:44:26 +01:00
|
|
|
Encoder(<U as Encoder<I>>::Error),
|
2019-05-12 06:03:50 -07:00
|
|
|
Decoder(<U as Decoder>::Error),
|
2018-09-12 18:47:39 -07:00
|
|
|
}
|
|
|
|
|
2020-07-19 21:44:26 +01:00
|
|
|
impl<E, U: Encoder<I> + Decoder, I> From<E> for DispatcherError<E, U, I> {
|
2019-05-12 06:03:50 -07:00
|
|
|
fn from(err: E) -> Self {
|
2019-12-11 12:42:07 +06:00
|
|
|
DispatcherError::Service(err)
|
2018-09-12 18:47:39 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-19 21:44:26 +01:00
|
|
|
impl<E, U: Encoder<I> + Decoder, I> fmt::Debug for DispatcherError<E, U, I>
|
2018-09-12 18:47:39 -07:00
|
|
|
where
|
2019-05-12 06:03:50 -07:00
|
|
|
E: fmt::Debug,
|
2020-07-19 21:44:26 +01:00
|
|
|
<U as Encoder<I>>::Error: fmt::Debug,
|
2019-05-12 06:03:50 -07:00
|
|
|
<U as Decoder>::Error: fmt::Debug,
|
2018-09-12 18:47:39 -07:00
|
|
|
{
|
2019-12-02 22:30:09 +06:00
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2019-05-12 06:03:50 -07:00
|
|
|
match *self {
|
2019-12-11 12:42:07 +06:00
|
|
|
DispatcherError::Service(ref e) => write!(fmt, "DispatcherError::Service({:?})", e),
|
|
|
|
DispatcherError::Encoder(ref e) => write!(fmt, "DispatcherError::Encoder({:?})", e),
|
|
|
|
DispatcherError::Decoder(ref e) => write!(fmt, "DispatcherError::Decoder({:?})", e),
|
2018-09-12 18:47:39 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-19 21:44:26 +01:00
|
|
|
impl<E, U: Encoder<I> + Decoder, I> fmt::Display for DispatcherError<E, U, I>
|
2018-09-12 18:47:39 -07:00
|
|
|
where
|
2019-05-12 06:03:50 -07:00
|
|
|
E: fmt::Display,
|
2020-07-19 21:44:26 +01:00
|
|
|
<U as Encoder<I>>::Error: fmt::Debug,
|
2019-05-12 06:03:50 -07:00
|
|
|
<U as Decoder>::Error: fmt::Debug,
|
2018-09-12 18:47:39 -07:00
|
|
|
{
|
2019-12-02 22:30:09 +06:00
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2019-05-12 06:03:50 -07:00
|
|
|
match *self {
|
2019-12-11 12:42:07 +06:00
|
|
|
DispatcherError::Service(ref e) => write!(fmt, "{}", e),
|
|
|
|
DispatcherError::Encoder(ref e) => write!(fmt, "{:?}", e),
|
|
|
|
DispatcherError::Decoder(ref e) => write!(fmt, "{:?}", e),
|
2018-09-12 18:47:39 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-11 12:42:07 +06:00
|
|
|
pub enum Message<T> {
|
|
|
|
Item(T),
|
2019-03-20 09:44:23 -07:00
|
|
|
Close,
|
|
|
|
}
|
|
|
|
|
2020-07-19 21:44:26 +01:00
|
|
|
/// Dispatcher is a future that reads frames from Framed object
|
|
|
|
/// and passes them to the service.
|
2019-11-19 14:51:40 +06:00
|
|
|
#[pin_project::pin_project]
|
2020-07-19 21:44:26 +01:00
|
|
|
pub struct Dispatcher<S, T, U, I>
|
2018-09-12 18:47:39 -07:00
|
|
|
where
|
2020-07-19 21:44:26 +01:00
|
|
|
S: Service<Request = <U as Decoder>::Item, Response = I>,
|
2019-01-26 22:07:27 -08:00
|
|
|
S::Error: 'static,
|
|
|
|
S::Future: 'static,
|
2019-11-19 14:51:40 +06:00
|
|
|
T: AsyncRead + AsyncWrite,
|
2020-07-19 21:44:26 +01:00
|
|
|
U: Encoder<I> + Decoder,
|
|
|
|
I: 'static,
|
|
|
|
<U as Encoder<I>>::Error: std::fmt::Debug,
|
2018-09-12 18:47:39 -07:00
|
|
|
{
|
2019-01-26 22:07:27 -08:00
|
|
|
service: S,
|
2020-07-19 21:44:26 +01:00
|
|
|
state: State<S, U, I>,
|
2020-03-04 11:48:19 -05:00
|
|
|
#[pin]
|
2019-01-26 22:07:27 -08:00
|
|
|
framed: Framed<T, U>,
|
2020-07-19 21:44:26 +01:00
|
|
|
rx: mpsc::Receiver<Result<Message<I>, S::Error>>,
|
|
|
|
tx: mpsc::Sender<Result<Message<I>, S::Error>>,
|
2018-09-12 18:47:39 -07:00
|
|
|
}
|
|
|
|
|
2020-07-19 21:44:26 +01:00
|
|
|
enum State<S: Service, U: Encoder<I> + Decoder, I> {
|
2018-09-24 20:06:20 -07:00
|
|
|
Processing,
|
2020-07-19 21:44:26 +01:00
|
|
|
Error(DispatcherError<S::Error, U, I>),
|
|
|
|
FramedError(DispatcherError<S::Error, U, I>),
|
2019-03-20 09:44:23 -07:00
|
|
|
FlushAndStop,
|
2018-09-24 20:06:20 -07:00
|
|
|
Stopping,
|
|
|
|
}
|
|
|
|
|
2020-07-19 21:44:26 +01:00
|
|
|
impl<S: Service, U: Encoder<I> + Decoder, I> State<S, U, I> {
|
|
|
|
fn take_error(&mut self) -> DispatcherError<S::Error, U, I> {
|
2019-12-11 12:42:07 +06:00
|
|
|
match mem::replace(self, State::Processing) {
|
|
|
|
State::Error(err) => err,
|
|
|
|
_ => panic!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-19 21:44:26 +01:00
|
|
|
fn take_framed_error(&mut self) -> DispatcherError<S::Error, U, I> {
|
2019-12-11 12:42:07 +06:00
|
|
|
match mem::replace(self, State::Processing) {
|
|
|
|
State::FramedError(err) => err,
|
|
|
|
_ => panic!(),
|
|
|
|
}
|
|
|
|
}
|
2018-09-24 20:06:20 -07:00
|
|
|
}
|
|
|
|
|
2020-07-19 21:44:26 +01:00
|
|
|
impl<S, T, U, I> Dispatcher<S, T, U, I>
|
2019-01-26 21:41:28 -08:00
|
|
|
where
|
2020-07-19 21:44:26 +01:00
|
|
|
S: Service<Request = <U as Decoder>::Item, Response = I>,
|
2019-01-26 22:07:27 -08:00
|
|
|
S::Error: 'static,
|
|
|
|
S::Future: 'static,
|
2019-11-19 14:51:40 +06:00
|
|
|
T: AsyncRead + AsyncWrite,
|
2020-07-19 21:44:26 +01:00
|
|
|
U: Decoder + Encoder<I>,
|
|
|
|
I: 'static,
|
|
|
|
<U as Decoder>::Error: std::fmt::Debug,
|
|
|
|
<U as Encoder<I>>::Error: std::fmt::Debug,
|
2019-01-26 21:41:28 -08:00
|
|
|
{
|
2019-03-09 07:27:35 -08:00
|
|
|
pub fn new<F: IntoService<S>>(framed: Framed<T, U>, service: F) -> Self {
|
2019-12-11 12:42:07 +06:00
|
|
|
let (tx, rx) = mpsc::channel();
|
|
|
|
Dispatcher {
|
2019-01-26 21:41:28 -08:00
|
|
|
framed,
|
2019-12-11 12:42:07 +06:00
|
|
|
rx,
|
|
|
|
tx,
|
2019-01-26 21:41:28 -08:00
|
|
|
service: service.into_service(),
|
2019-12-11 12:42:07 +06:00
|
|
|
state: State::Processing,
|
2019-01-26 21:41:28 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-11 20:23:14 +06:00
|
|
|
/// Construct new `Dispatcher` instance with customer `mpsc::Receiver`
|
|
|
|
pub fn with_rx<F: IntoService<S>>(
|
|
|
|
framed: Framed<T, U>,
|
|
|
|
service: F,
|
2020-07-19 21:44:26 +01:00
|
|
|
rx: mpsc::Receiver<Result<Message<I>, S::Error>>,
|
2019-12-11 20:23:14 +06:00
|
|
|
) -> Self {
|
|
|
|
let tx = rx.sender();
|
|
|
|
Dispatcher {
|
|
|
|
framed,
|
|
|
|
rx,
|
|
|
|
tx,
|
|
|
|
service: service.into_service(),
|
|
|
|
state: State::Processing,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-11 12:42:07 +06:00
|
|
|
/// Get sink
|
2020-07-19 21:44:26 +01:00
|
|
|
pub fn get_sink(&self) -> mpsc::Sender<Result<Message<I>, S::Error>> {
|
2019-12-11 12:42:07 +06:00
|
|
|
self.tx.clone()
|
2019-03-20 09:44:23 -07:00
|
|
|
}
|
|
|
|
|
2019-12-11 20:23:14 +06:00
|
|
|
/// Get reference to a service wrapped by `Dispatcher` instance.
|
2019-01-26 21:41:28 -08:00
|
|
|
pub fn get_ref(&self) -> &S {
|
2019-01-26 22:07:27 -08:00
|
|
|
&self.service
|
2019-01-26 21:41:28 -08:00
|
|
|
}
|
|
|
|
|
2019-12-11 20:23:14 +06:00
|
|
|
/// Get mutable reference to a service wrapped by `Dispatcher` instance.
|
2019-01-26 21:41:28 -08:00
|
|
|
pub fn get_mut(&mut self) -> &mut S {
|
2019-01-26 22:07:27 -08:00
|
|
|
&mut self.service
|
2019-01-26 21:41:28 -08:00
|
|
|
}
|
|
|
|
|
2019-12-11 20:23:14 +06:00
|
|
|
/// Get reference to a framed instance wrapped by `Dispatcher`
|
2019-01-26 21:41:28 -08:00
|
|
|
/// instance.
|
|
|
|
pub fn get_framed(&self) -> &Framed<T, U> {
|
2019-01-26 22:07:27 -08:00
|
|
|
&self.framed
|
2019-01-26 21:41:28 -08:00
|
|
|
}
|
|
|
|
|
2019-12-11 20:23:14 +06:00
|
|
|
/// Get mutable reference to a framed instance wrapped by `Dispatcher` instance.
|
2019-01-26 21:41:28 -08:00
|
|
|
pub fn get_framed_mut(&mut self) -> &mut Framed<T, U> {
|
2019-01-26 22:07:27 -08:00
|
|
|
&mut self.framed
|
2019-01-26 21:41:28 -08:00
|
|
|
}
|
2019-10-14 17:55:52 +06:00
|
|
|
|
2020-03-04 11:48:19 -05:00
|
|
|
fn poll_read(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> bool
|
2019-12-11 12:42:07 +06:00
|
|
|
where
|
2020-07-19 21:44:26 +01:00
|
|
|
S: Service<Request = <U as Decoder>::Item, Response = I>,
|
2019-12-11 12:42:07 +06:00
|
|
|
S::Error: 'static,
|
|
|
|
S::Future: 'static,
|
|
|
|
T: AsyncRead + AsyncWrite,
|
2020-07-19 21:44:26 +01:00
|
|
|
U: Decoder + Encoder<I>,
|
|
|
|
I: 'static,
|
|
|
|
<U as Encoder<I>>::Error: std::fmt::Debug,
|
2019-12-11 12:42:07 +06:00
|
|
|
{
|
|
|
|
loop {
|
2020-03-04 11:48:19 -05:00
|
|
|
let this = self.as_mut().project();
|
|
|
|
match this.service.poll_ready(cx) {
|
2019-12-11 12:42:07 +06:00
|
|
|
Poll::Ready(Ok(_)) => {
|
2020-03-04 11:48:19 -05:00
|
|
|
let item = match this.framed.next_item(cx) {
|
2019-12-11 12:42:07 +06:00
|
|
|
Poll::Ready(Some(Ok(el))) => el,
|
|
|
|
Poll::Ready(Some(Err(err))) => {
|
2020-03-04 11:48:19 -05:00
|
|
|
*this.state = State::FramedError(DispatcherError::Decoder(err));
|
2019-12-11 12:42:07 +06:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
Poll::Pending => return false,
|
|
|
|
Poll::Ready(None) => {
|
2020-03-04 11:48:19 -05:00
|
|
|
*this.state = State::Stopping;
|
2019-12-11 12:42:07 +06:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
2019-11-14 18:38:24 +06:00
|
|
|
|
2020-03-04 11:48:19 -05:00
|
|
|
let tx = this.tx.clone();
|
|
|
|
actix_rt::spawn(this.service.call(item).map(move |item| {
|
2019-12-11 12:42:07 +06:00
|
|
|
let _ = tx.send(item.map(Message::Item));
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
Poll::Pending => return false,
|
|
|
|
Poll::Ready(Err(err)) => {
|
2020-03-04 11:48:19 -05:00
|
|
|
*this.state = State::Error(DispatcherError::Service(err));
|
2019-12-11 12:42:07 +06:00
|
|
|
return true;
|
2018-09-24 20:06:20 -07:00
|
|
|
}
|
2019-11-14 18:38:24 +06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-11 12:42:07 +06:00
|
|
|
/// write to framed object
|
2020-03-04 11:48:19 -05:00
|
|
|
fn poll_write(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> bool
|
2019-12-11 12:42:07 +06:00
|
|
|
where
|
2020-07-19 21:44:26 +01:00
|
|
|
S: Service<Request = <U as Decoder>::Item, Response = I>,
|
2019-12-11 12:42:07 +06:00
|
|
|
S::Error: 'static,
|
|
|
|
S::Future: 'static,
|
|
|
|
T: AsyncRead + AsyncWrite,
|
2020-07-19 21:44:26 +01:00
|
|
|
U: Decoder + Encoder<I>,
|
|
|
|
I: 'static,
|
|
|
|
<U as Encoder<I>>::Error: std::fmt::Debug,
|
2019-12-11 12:42:07 +06:00
|
|
|
{
|
|
|
|
loop {
|
2020-03-04 11:48:19 -05: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-11 12:42:07 +06:00
|
|
|
Poll::Ready(Some(Ok(Message::Item(msg)))) => {
|
2020-03-04 11:48:19 -05:00
|
|
|
if let Err(err) = this.framed.as_mut().write(msg) {
|
|
|
|
*this.state = State::FramedError(DispatcherError::Encoder(err));
|
2019-12-11 12:42:07 +06:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Poll::Ready(Some(Ok(Message::Close))) => {
|
2020-03-04 11:48:19 -05:00
|
|
|
*this.state = State::FlushAndStop;
|
2019-11-14 18:38:24 +06:00
|
|
|
return true;
|
|
|
|
}
|
2019-12-11 12:42:07 +06:00
|
|
|
Poll::Ready(Some(Err(err))) => {
|
2020-03-04 11:48:19 -05:00
|
|
|
*this.state = State::Error(DispatcherError::Service(err));
|
2019-11-14 18:38:24 +06:00
|
|
|
return true;
|
|
|
|
}
|
2019-12-11 12:42:07 +06:00
|
|
|
Poll::Ready(None) | Poll::Pending => break,
|
|
|
|
}
|
2018-09-24 20:06:20 -07:00
|
|
|
}
|
2019-12-11 12:42:07 +06:00
|
|
|
|
2020-03-04 11:48:19 -05:00
|
|
|
if !this.framed.is_write_buf_empty() {
|
|
|
|
match this.framed.flush(cx) {
|
2019-12-11 12:42:07 +06:00
|
|
|
Poll::Pending => break,
|
|
|
|
Poll::Ready(Ok(_)) => (),
|
|
|
|
Poll::Ready(Err(err)) => {
|
|
|
|
debug!("Error sending data: {:?}", err);
|
2020-03-04 11:48:19 -05:00
|
|
|
*this.state = State::FramedError(DispatcherError::Encoder(err));
|
2019-12-11 12:42:07 +06:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
break;
|
2019-11-14 18:38:24 +06:00
|
|
|
}
|
|
|
|
}
|
2019-12-11 12:42:07 +06:00
|
|
|
|
|
|
|
false
|
2019-11-14 18:38:24 +06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-19 21:44:26 +01:00
|
|
|
impl<S, T, U, I> Future for Dispatcher<S, T, U, I>
|
2019-11-14 18:38:24 +06:00
|
|
|
where
|
2020-07-19 21:44:26 +01:00
|
|
|
S: Service<Request = <U as Decoder>::Item, Response = I>,
|
2019-11-14 18:38:24 +06:00
|
|
|
S::Error: 'static,
|
|
|
|
S::Future: 'static,
|
2019-11-19 14:51:40 +06:00
|
|
|
T: AsyncRead + AsyncWrite,
|
2020-07-19 21:44:26 +01:00
|
|
|
U: Decoder + Encoder<I>,
|
|
|
|
I: 'static,
|
|
|
|
<U as Encoder<I>>::Error: std::fmt::Debug,
|
2019-12-11 12:42:07 +06:00
|
|
|
<U as Decoder>::Error: std::fmt::Debug,
|
2019-11-14 18:38:24 +06:00
|
|
|
{
|
2020-07-19 21:44:26 +01:00
|
|
|
type Output = Result<(), DispatcherError<S::Error, U, I>>;
|
2019-11-14 18:38:24 +06:00
|
|
|
|
2019-12-11 12:42:07 +06:00
|
|
|
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
|
|
|
loop {
|
|
|
|
let this = self.as_mut().project();
|
|
|
|
|
|
|
|
return match this.state {
|
|
|
|
State::Processing => {
|
2020-03-04 11:48:19 -05:00
|
|
|
if self.as_mut().poll_read(cx) || self.as_mut().poll_write(cx) {
|
2019-12-11 12:42:07 +06:00
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
Poll::Pending
|
2019-11-14 18:38:24 +06:00
|
|
|
}
|
2018-09-24 20:06:20 -07:00
|
|
|
}
|
2019-12-11 12:42:07 +06:00
|
|
|
State::Error(_) => {
|
|
|
|
// flush write buffer
|
2020-03-04 11:48:19 -05:00
|
|
|
if !this.framed.is_write_buf_empty() {
|
|
|
|
if let Poll::Pending = this.framed.flush(cx) {
|
2019-12-11 14:36:00 +06:00
|
|
|
return Poll::Pending;
|
2019-03-20 09:44:23 -07:00
|
|
|
}
|
|
|
|
}
|
2020-03-04 11:48:19 -05:00
|
|
|
Poll::Ready(Err(this.state.take_error()))
|
2019-03-20 09:44:23 -07:00
|
|
|
}
|
2019-12-11 12:42:07 +06:00
|
|
|
State::FlushAndStop => {
|
|
|
|
if !this.framed.is_write_buf_empty() {
|
|
|
|
match this.framed.flush(cx) {
|
|
|
|
Poll::Ready(Err(err)) => {
|
|
|
|
debug!("Error sending data: {:?}", err);
|
|
|
|
Poll::Ready(Ok(()))
|
|
|
|
}
|
|
|
|
Poll::Pending => Poll::Pending,
|
|
|
|
Poll::Ready(Ok(_)) => Poll::Ready(Ok(())),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Poll::Ready(Ok(()))
|
|
|
|
}
|
2019-11-14 18:38:24 +06:00
|
|
|
}
|
2019-12-11 12:42:07 +06:00
|
|
|
State::FramedError(_) => Poll::Ready(Err(this.state.take_framed_error())),
|
|
|
|
State::Stopping => Poll::Ready(Ok(())),
|
|
|
|
};
|
2018-09-24 20:40:31 -07:00
|
|
|
}
|
2018-09-12 18:47:39 -07:00
|
|
|
}
|
|
|
|
}
|