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)]
|
2019-01-26 21:41:28 -08:00
|
|
|
use std::collections::VecDeque;
|
2019-11-14 18:38:24 +06:00
|
|
|
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-11-14 18:38:24 +06:00
|
|
|
use futures::future::{ready, FutureExt};
|
|
|
|
use futures::{Future, Sink, Stream};
|
2018-12-16 16:26:24 -08:00
|
|
|
use log::debug;
|
2018-09-12 18:47:39 -07:00
|
|
|
|
2019-01-26 21:41:28 -08:00
|
|
|
use crate::cell::Cell;
|
2019-11-14 18:38:24 +06:00
|
|
|
use crate::mpsc;
|
|
|
|
use crate::task::LocalWaker;
|
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
|
|
|
|
pub enum FramedTransportError<E, U: Encoder + Decoder> {
|
|
|
|
Service(E),
|
|
|
|
Encoder(<U as Encoder>::Error),
|
|
|
|
Decoder(<U as Decoder>::Error),
|
2018-09-12 18:47:39 -07:00
|
|
|
}
|
|
|
|
|
2019-05-12 06:03:50 -07:00
|
|
|
impl<E, U: Encoder + Decoder> From<E> for FramedTransportError<E, U> {
|
|
|
|
fn from(err: E) -> Self {
|
|
|
|
FramedTransportError::Service(err)
|
2018-09-12 18:47:39 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-12 06:03:50 -07:00
|
|
|
impl<E, U: Encoder + Decoder> fmt::Debug for FramedTransportError<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-05-12 06:03:50 -07:00
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match *self {
|
|
|
|
FramedTransportError::Service(ref e) => {
|
|
|
|
write!(fmt, "FramedTransportError::Service({:?})", e)
|
|
|
|
}
|
|
|
|
FramedTransportError::Encoder(ref e) => {
|
|
|
|
write!(fmt, "FramedTransportError::Encoder({:?})", e)
|
|
|
|
}
|
|
|
|
FramedTransportError::Decoder(ref e) => {
|
|
|
|
write!(fmt, "FramedTransportError::Encoder({:?})", e)
|
|
|
|
}
|
2018-09-12 18:47:39 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-12 06:03:50 -07:00
|
|
|
impl<E, U: Encoder + Decoder> fmt::Display for FramedTransportError<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-05-12 06:03:50 -07:00
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match *self {
|
|
|
|
FramedTransportError::Service(ref e) => write!(fmt, "{}", e),
|
|
|
|
FramedTransportError::Encoder(ref e) => write!(fmt, "{:?}", e),
|
|
|
|
FramedTransportError::Decoder(ref e) => write!(fmt, "{:?}", e),
|
2018-09-12 18:47:39 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-20 09:44:23 -07:00
|
|
|
pub enum FramedMessage<T> {
|
|
|
|
Message(T),
|
|
|
|
Close,
|
|
|
|
}
|
|
|
|
|
2019-11-14 18:38:24 +06:00
|
|
|
type Rx<U> = Option<mpsc::Receiver<FramedMessage<<U as Encoder>::Item>>>;
|
|
|
|
type Inner<S: Service, U> = Cell<FramedTransportInner<<U as Encoder>::Item, S::Error>>;
|
|
|
|
|
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]
|
2018-09-24 20:40:31 -07:00
|
|
|
pub struct FramedTransport<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,
|
|
|
|
state: TransportState<S, U>,
|
|
|
|
framed: Framed<T, U>,
|
2019-11-14 18:38:24 +06:00
|
|
|
rx: Option<mpsc::Receiver<FramedMessage<<U as Encoder>::Item>>>,
|
2019-01-26 22:07:27 -08:00
|
|
|
inner: Cell<FramedTransportInner<<U as Encoder>::Item, S::Error>>,
|
2018-09-12 18:47:39 -07:00
|
|
|
}
|
|
|
|
|
2019-03-09 07:27:35 -08:00
|
|
|
enum TransportState<S: Service, U: Encoder + Decoder> {
|
2018-09-24 20:06:20 -07:00
|
|
|
Processing,
|
2018-09-24 22:31:05 -07:00
|
|
|
Error(FramedTransportError<S::Error, U>),
|
2018-12-21 13:00:26 -08:00
|
|
|
FramedError(FramedTransportError<S::Error, U>),
|
2019-03-20 09:44:23 -07:00
|
|
|
FlushAndStop,
|
2018-09-24 20:06:20 -07:00
|
|
|
Stopping,
|
|
|
|
}
|
|
|
|
|
2019-01-26 22:07:27 -08:00
|
|
|
struct FramedTransportInner<I, E> {
|
|
|
|
buf: VecDeque<Result<I, E>>,
|
2019-11-14 18:38:24 +06:00
|
|
|
task: LocalWaker,
|
2018-09-24 20:06:20 -07:00
|
|
|
}
|
|
|
|
|
2019-01-26 21:41:28 -08:00
|
|
|
impl<S, T, U> FramedTransport<S, T, U>
|
|
|
|
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-01-26 22:07:27 -08:00
|
|
|
FramedTransport {
|
2019-01-26 21:41:28 -08:00
|
|
|
framed,
|
2019-03-20 09:44:23 -07:00
|
|
|
rx: None,
|
2019-01-26 21:41:28 -08:00
|
|
|
service: service.into_service(),
|
|
|
|
state: TransportState::Processing,
|
2019-01-26 22:07:27 -08:00
|
|
|
inner: Cell::new(FramedTransportInner {
|
|
|
|
buf: VecDeque::new(),
|
2019-11-14 18:38:24 +06:00
|
|
|
task: LocalWaker::new(),
|
2019-01-26 22:07:27 -08:00
|
|
|
}),
|
2019-01-26 21:41:28 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-20 09:44:23 -07:00
|
|
|
/// Get Sender
|
|
|
|
pub fn set_receiver(
|
|
|
|
mut self,
|
2019-11-14 18:38:24 +06:00
|
|
|
rx: mpsc::Receiver<FramedMessage<<U as Encoder>::Item>>,
|
2019-03-20 09:44:23 -07:00
|
|
|
) -> Self {
|
|
|
|
self.rx = Some(rx);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-24 20:40:31 -07:00
|
|
|
impl<S, T, U> Future for FramedTransport<S, T, U>
|
2018-09-24 20:06:20 -07:00
|
|
|
where
|
2019-11-19 14:51:40 +06:00
|
|
|
S: Service<Request = Request<U>, Response = Response<U>>,
|
|
|
|
S::Error: 'static,
|
2019-01-26 22:07:27 -08:00
|
|
|
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-11-19 14:51:40 +06:00
|
|
|
<U as Encoder>::Error: std::fmt::Debug,
|
|
|
|
<U as Decoder>::Error: std::fmt::Debug,
|
2018-09-24 20:06:20 -07:00
|
|
|
{
|
2019-11-14 18:38:24 +06:00
|
|
|
type Output = Result<(), FramedTransportError<S::Error, U>>;
|
2018-09-24 20:06:20 -07:00
|
|
|
|
2019-11-14 18:38:24 +06:00
|
|
|
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
|
|
|
|
self.inner.get_ref().task.register(cx.waker());
|
2019-10-14 17:55:52 +06:00
|
|
|
|
2019-11-19 14:51:40 +06:00
|
|
|
let this = self.project();
|
2019-11-14 18:38:24 +06:00
|
|
|
poll(
|
|
|
|
cx,
|
2019-11-19 14:51:40 +06:00
|
|
|
this.service,
|
|
|
|
this.state,
|
|
|
|
this.framed,
|
|
|
|
this.rx,
|
|
|
|
this.inner,
|
2019-11-14 18:38:24 +06:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn poll<S, T, U>(
|
|
|
|
cx: &mut Context,
|
|
|
|
srv: &mut S,
|
|
|
|
state: &mut TransportState<S, U>,
|
|
|
|
framed: &mut Framed<T, U>,
|
|
|
|
rx: &mut Rx<U>,
|
|
|
|
inner: &mut Inner<S, U>,
|
|
|
|
) -> Poll<Result<(), FramedTransportError<S::Error, U>>>
|
|
|
|
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,
|
|
|
|
{
|
|
|
|
match mem::replace(state, TransportState::Processing) {
|
|
|
|
TransportState::Processing => {
|
|
|
|
if poll_read(cx, srv, state, framed, inner)
|
|
|
|
|| poll_write(cx, state, framed, rx, inner)
|
|
|
|
{
|
|
|
|
poll(cx, srv, state, framed, rx, inner)
|
|
|
|
} else {
|
|
|
|
Poll::Pending
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TransportState::Error(err) => {
|
|
|
|
let is_empty = framed.is_write_buf_empty();
|
2019-11-15 00:28:29 +09:00
|
|
|
if is_empty || poll_write(cx, state, framed, rx, inner) {
|
2019-11-14 18:38:24 +06:00
|
|
|
Poll::Ready(Err(err))
|
|
|
|
} else {
|
|
|
|
*state = TransportState::Error(err);
|
|
|
|
Poll::Pending
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TransportState::FlushAndStop => {
|
|
|
|
if !framed.is_write_buf_empty() {
|
|
|
|
match Pin::new(framed).poll_flush(cx) {
|
|
|
|
Poll::Ready(Err(err)) => {
|
|
|
|
debug!("Error sending data: {:?}", err);
|
|
|
|
Poll::Ready(Ok(()))
|
|
|
|
}
|
|
|
|
Poll::Pending => Poll::Pending,
|
|
|
|
Poll::Ready(Ok(_)) => Poll::Ready(Ok(())),
|
2018-09-24 20:06:20 -07:00
|
|
|
}
|
2019-11-14 18:38:24 +06:00
|
|
|
} else {
|
|
|
|
Poll::Ready(Ok(()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TransportState::FramedError(err) => Poll::Ready(Err(err)),
|
|
|
|
TransportState::Stopping => Poll::Ready(Ok(())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn poll_read<S, T, U>(
|
|
|
|
cx: &mut Context,
|
|
|
|
srv: &mut S,
|
|
|
|
state: &mut TransportState<S, U>,
|
|
|
|
framed: &mut Framed<T, U>,
|
|
|
|
inner: &mut Inner<S, U>,
|
|
|
|
) -> bool
|
|
|
|
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,
|
|
|
|
{
|
|
|
|
loop {
|
|
|
|
match srv.poll_ready(cx) {
|
|
|
|
Poll::Ready(Ok(_)) => {
|
|
|
|
let item = match framed.next_item(cx) {
|
|
|
|
Poll::Ready(Some(Ok(el))) => el,
|
|
|
|
Poll::Ready(Some(Err(err))) => {
|
|
|
|
*state =
|
|
|
|
TransportState::FramedError(FramedTransportError::Decoder(err));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
Poll::Pending => return false,
|
|
|
|
Poll::Ready(None) => {
|
|
|
|
*state = TransportState::Stopping;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut cell = inner.clone();
|
|
|
|
let fut = srv.call(item).then(move |item| {
|
|
|
|
let inner = cell.get_mut();
|
|
|
|
inner.buf.push_back(item);
|
|
|
|
inner.task.wake();
|
|
|
|
ready(())
|
|
|
|
});
|
|
|
|
tokio_executor::current_thread::spawn(fut);
|
2018-09-24 20:06:20 -07:00
|
|
|
}
|
2019-11-14 18:38:24 +06:00
|
|
|
Poll::Pending => return false,
|
|
|
|
Poll::Ready(Err(err)) => {
|
|
|
|
*state = TransportState::Error(FramedTransportError::Service(err));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// write to framed object
|
|
|
|
fn poll_write<S, T, U>(
|
|
|
|
cx: &mut Context,
|
|
|
|
state: &mut TransportState<S, U>,
|
|
|
|
framed: &mut Framed<T, U>,
|
|
|
|
rx: &mut Rx<U>,
|
|
|
|
inner: &mut Inner<S, U>,
|
|
|
|
) -> bool
|
|
|
|
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,
|
|
|
|
{
|
|
|
|
// let this = self.project();
|
|
|
|
|
|
|
|
let inner = inner.get_mut();
|
|
|
|
let mut rx_done = rx.is_none();
|
|
|
|
let mut buf_empty = inner.buf.is_empty();
|
|
|
|
loop {
|
|
|
|
while !framed.is_write_buf_full() {
|
|
|
|
if !buf_empty {
|
|
|
|
match inner.buf.pop_front().unwrap() {
|
|
|
|
Ok(msg) => {
|
2019-11-18 14:30:04 +06:00
|
|
|
if let Err(err) = framed.write(msg) {
|
2019-11-14 18:38:24 +06:00
|
|
|
*state =
|
|
|
|
TransportState::FramedError(FramedTransportError::Encoder(err));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
buf_empty = inner.buf.is_empty();
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
*state = TransportState::Error(FramedTransportError::Service(err));
|
|
|
|
return true;
|
|
|
|
}
|
2018-09-24 20:06:20 -07:00
|
|
|
}
|
|
|
|
}
|
2019-11-14 18:38:24 +06:00
|
|
|
|
|
|
|
if !rx_done && rx.is_some() {
|
|
|
|
match Pin::new(rx.as_mut().unwrap()).poll_next(cx) {
|
|
|
|
Poll::Ready(Some(FramedMessage::Message(msg))) => {
|
2019-11-18 14:30:04 +06:00
|
|
|
if let Err(err) = framed.write(msg) {
|
2019-11-14 18:38:24 +06:00
|
|
|
*state =
|
|
|
|
TransportState::FramedError(FramedTransportError::Encoder(err));
|
|
|
|
return true;
|
2019-03-20 09:44:23 -07:00
|
|
|
}
|
|
|
|
}
|
2019-11-14 18:38:24 +06:00
|
|
|
Poll::Ready(Some(FramedMessage::Close)) => {
|
|
|
|
*state = TransportState::FlushAndStop;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
Poll::Ready(None) => {
|
|
|
|
rx_done = true;
|
|
|
|
let _ = rx.take();
|
|
|
|
}
|
|
|
|
Poll::Pending => rx_done = true,
|
2019-03-20 09:44:23 -07:00
|
|
|
}
|
|
|
|
}
|
2019-11-14 18:38:24 +06:00
|
|
|
|
|
|
|
if rx_done && buf_empty {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !framed.is_write_buf_empty() {
|
|
|
|
match framed.flush(cx) {
|
|
|
|
Poll::Pending => break,
|
|
|
|
Poll::Ready(Err(err)) => {
|
|
|
|
debug!("Error sending data: {:?}", err);
|
|
|
|
*state = TransportState::FramedError(FramedTransportError::Encoder(err));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
Poll::Ready(Ok(_)) => (),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
break;
|
2018-09-24 20:40:31 -07:00
|
|
|
}
|
2018-09-12 18:47:39 -07:00
|
|
|
}
|
2019-11-14 18:38:24 +06:00
|
|
|
|
|
|
|
false
|
2018-09-12 18:47:39 -07:00
|
|
|
}
|