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};
|
2019-12-11 12:42:07 +06:00
|
|
|
use futures::{Future, FutureExt, Stream};
|
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
|
|
|
|
2018-09-24 20:06:20 -07:00
|
|
|
type Request<U> = <U as Decoder>::Item;
|
|
|
|
type Response<U> = <U as Encoder>::Item;
|
2018-09-12 18:47:39 -07:00
|
|
|
|
2019-05-12 06:03:50 -07:00
|
|
|
/// Framed transport errors
|
2019-12-11 12:42:07 +06:00
|
|
|
pub enum DispatcherError<E, U: Encoder + Decoder> {
|
2019-05-12 06:03:50 -07:00
|
|
|
Service(E),
|
|
|
|
Encoder(<U as Encoder>::Error),
|
|
|
|
Decoder(<U as Decoder>::Error),
|
2018-09-12 18:47:39 -07:00
|
|
|
}
|
|
|
|
|
2019-12-11 12:42:07 +06:00
|
|
|
impl<E, U: Encoder + Decoder> From<E> for DispatcherError<E, U> {
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-11 12:42:07 +06:00
|
|
|
impl<E, U: Encoder + Decoder> fmt::Debug for DispatcherError<E, U>
|
2018-09-12 18:47:39 -07:00
|
|
|
where
|
2019-05-12 06:03:50 -07:00
|
|
|
E: fmt::Debug,
|
|
|
|
<U as Encoder>::Error: fmt::Debug,
|
|
|
|
<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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-11 12:42:07 +06:00
|
|
|
impl<E, U: Encoder + Decoder> fmt::Display for DispatcherError<E, U>
|
2018-09-12 18:47:39 -07:00
|
|
|
where
|
2019-05-12 06:03:50 -07:00
|
|
|
E: fmt::Display,
|
|
|
|
<U as Encoder>::Error: fmt::Debug,
|
|
|
|
<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,
|
|
|
|
}
|
|
|
|
|
2018-09-24 17:50:29 -07:00
|
|
|
/// FramedTransport - is a future that reads frames from Framed object
|
2018-09-12 18:47:39 -07:00
|
|
|
/// and pass then to the service.
|
2019-11-19 14:51:40 +06:00
|
|
|
#[pin_project::pin_project]
|
2019-12-11 12:42:07 +06:00
|
|
|
pub struct Dispatcher<S, T, U>
|
2018-09-12 18:47:39 -07:00
|
|
|
where
|
2019-03-09 07:27:35 -08:00
|
|
|
S: Service<Request = Request<U>, Response = Response<U>>,
|
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,
|
|
|
|
U: Encoder + Decoder,
|
2019-01-26 22:07:27 -08:00
|
|
|
<U as Encoder>::Item: 'static,
|
2019-01-26 21:41:28 -08:00
|
|
|
<U as Encoder>::Error: std::fmt::Debug,
|
2018-09-12 18:47:39 -07:00
|
|
|
{
|
2019-01-26 22:07:27 -08:00
|
|
|
service: S,
|
2019-12-11 12:42:07 +06:00
|
|
|
state: State<S, U>,
|
2019-01-26 22:07:27 -08:00
|
|
|
framed: Framed<T, U>,
|
2019-12-11 12:42:07 +06:00
|
|
|
rx: mpsc::Receiver<Result<Message<<U as Encoder>::Item>, S::Error>>,
|
|
|
|
tx: mpsc::Sender<Result<Message<<U as Encoder>::Item>, S::Error>>,
|
2018-09-12 18:47:39 -07:00
|
|
|
}
|
|
|
|
|
2019-12-11 12:42:07 +06:00
|
|
|
enum State<S: Service, U: Encoder + Decoder> {
|
2018-09-24 20:06:20 -07:00
|
|
|
Processing,
|
2019-12-11 12:42:07 +06:00
|
|
|
Error(DispatcherError<S::Error, U>),
|
|
|
|
FramedError(DispatcherError<S::Error, U>),
|
2019-03-20 09:44:23 -07:00
|
|
|
FlushAndStop,
|
2018-09-24 20:06:20 -07:00
|
|
|
Stopping,
|
|
|
|
}
|
|
|
|
|
2019-12-11 12:42:07 +06:00
|
|
|
impl<S: Service, U: Encoder + Decoder> State<S, U> {
|
|
|
|
fn take_error(&mut self) -> DispatcherError<S::Error, U> {
|
|
|
|
match mem::replace(self, State::Processing) {
|
|
|
|
State::Error(err) => err,
|
|
|
|
_ => panic!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn take_framed_error(&mut self) -> DispatcherError<S::Error, U> {
|
|
|
|
match mem::replace(self, State::Processing) {
|
|
|
|
State::FramedError(err) => err,
|
|
|
|
_ => panic!(),
|
|
|
|
}
|
|
|
|
}
|
2018-09-24 20:06:20 -07:00
|
|
|
}
|
|
|
|
|
2019-12-11 12:42:07 +06:00
|
|
|
impl<S, T, U> Dispatcher<S, T, U>
|
2019-01-26 21:41:28 -08:00
|
|
|
where
|
2019-11-19 14:51:40 +06:00
|
|
|
S: Service<Request = Request<U>, Response = Response<U>>,
|
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,
|
|
|
|
U: Decoder + Encoder,
|
2019-01-26 22:07:27 -08:00
|
|
|
<U as Encoder>::Item: 'static,
|
2019-01-26 21:41:28 -08:00
|
|
|
<U as Encoder>::Error: std::fmt::Debug,
|
|
|
|
{
|
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 12:42:07 +06:00
|
|
|
/// Get sink
|
|
|
|
pub fn get_sink(&self) -> mpsc::Sender<Result<Message<<U as Encoder>::Item>, S::Error>> {
|
|
|
|
self.tx.clone()
|
2019-03-20 09:44:23 -07:00
|
|
|
}
|
|
|
|
|
2019-01-26 21:41:28 -08:00
|
|
|
/// Get reference to a service wrapped by `FramedTransport` instance.
|
|
|
|
pub fn get_ref(&self) -> &S {
|
2019-01-26 22:07:27 -08:00
|
|
|
&self.service
|
2019-01-26 21:41:28 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get mutable reference to a service wrapped by `FramedTransport`
|
|
|
|
/// instance.
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/// Get reference to a framed instance wrapped by `FramedTransport`
|
|
|
|
/// 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
|
|
|
}
|
|
|
|
|
|
|
|
/// Get mutable reference to a framed instance wrapped by `FramedTransport`
|
|
|
|
/// instance.
|
|
|
|
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
|
|
|
|
2019-12-11 12:42:07 +06:00
|
|
|
fn poll_read(&mut self, cx: &mut Context<'_>) -> bool
|
|
|
|
where
|
|
|
|
S: Service<Request = Request<U>, Response = Response<U>>,
|
|
|
|
S::Error: 'static,
|
|
|
|
S::Future: 'static,
|
|
|
|
T: AsyncRead + AsyncWrite,
|
|
|
|
U: Decoder + Encoder,
|
|
|
|
<U as Encoder>::Item: 'static,
|
|
|
|
<U as Encoder>::Error: std::fmt::Debug,
|
|
|
|
{
|
|
|
|
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.state = State::FramedError(DispatcherError::Decoder(err));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
Poll::Pending => return false,
|
|
|
|
Poll::Ready(None) => {
|
|
|
|
self.state = State::Stopping;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
2019-11-14 18:38:24 +06:00
|
|
|
|
2019-12-11 12:42:07 +06:00
|
|
|
let tx = self.tx.clone();
|
|
|
|
actix_rt::spawn(self.service.call(item).map(move |item| {
|
|
|
|
let _ = tx.send(item.map(Message::Item));
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
Poll::Pending => return false,
|
|
|
|
Poll::Ready(Err(err)) => {
|
|
|
|
self.state = State::Error(DispatcherError::Service(err));
|
|
|
|
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
|
|
|
|
fn poll_write(&mut self, cx: &mut Context<'_>) -> bool
|
|
|
|
where
|
|
|
|
S: Service<Request = Request<U>, Response = Response<U>>,
|
|
|
|
S::Error: 'static,
|
|
|
|
S::Future: 'static,
|
|
|
|
T: AsyncRead + AsyncWrite,
|
|
|
|
U: Decoder + Encoder,
|
|
|
|
<U as Encoder>::Item: 'static,
|
|
|
|
<U as Encoder>::Error: std::fmt::Debug,
|
|
|
|
{
|
|
|
|
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.state = State::FramedError(DispatcherError::Encoder(err));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Poll::Ready(Some(Ok(Message::Close))) => {
|
|
|
|
self.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))) => {
|
|
|
|
self.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
|
|
|
|
|
|
|
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.state = State::FramedError(DispatcherError::Encoder(err));
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-11 12:42:07 +06:00
|
|
|
impl<S, T, U> Future for Dispatcher<S, T, U>
|
2019-11-14 18:38:24 +06:00
|
|
|
where
|
|
|
|
S: Service<Request = Request<U>, Response = Response<U>>,
|
|
|
|
S::Error: 'static,
|
|
|
|
S::Future: 'static,
|
2019-11-19 14:51:40 +06:00
|
|
|
T: AsyncRead + AsyncWrite,
|
|
|
|
U: Decoder + Encoder,
|
2019-11-14 18:38:24 +06:00
|
|
|
<U as Encoder>::Item: 'static,
|
|
|
|
<U as Encoder>::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
|
|
|
{
|
2019-12-11 12:42:07 +06:00
|
|
|
type Output = Result<(), DispatcherError<S::Error, U>>;
|
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 => {
|
|
|
|
if self.poll_read(cx) || self.poll_write(cx) {
|
|
|
|
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
|
|
|
|
if !self.framed.is_write_buf_empty() {
|
|
|
|
match self.framed.flush(cx) {
|
|
|
|
Poll::Pending => Poll::Pending,
|
|
|
|
Poll::Ready(Ok(_)) | Poll::Ready(Err(_)) => {
|
|
|
|
Poll::Ready(Err(self.state.take_error()))
|
|
|
|
}
|
2019-03-20 09:44:23 -07:00
|
|
|
}
|
2019-12-11 12:42:07 +06:00
|
|
|
} else {
|
|
|
|
Poll::Ready(Err(self.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
|
|
|
}
|
|
|
|
}
|