2019-06-26 11:19:40 +02:00
|
|
|
//! Framed dispatcher service and related utilities
|
|
|
|
use std::collections::VecDeque;
|
2019-11-14 13:38:24 +01:00
|
|
|
use std::future::Future;
|
2019-06-26 11:19:40 +02:00
|
|
|
use std::mem;
|
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::task::LocalWaker;
|
|
|
|
use actix_utils::{mpsc, oneshot};
|
|
|
|
use futures::future::ready;
|
|
|
|
use futures::{FutureExt, Sink as FutureSink, Stream};
|
2019-06-26 11:19:40 +02:00
|
|
|
use log::debug;
|
|
|
|
|
|
|
|
use crate::cell::Cell;
|
|
|
|
use crate::error::ServiceError;
|
|
|
|
use crate::item::Item;
|
|
|
|
use crate::sink::Sink;
|
2019-07-01 07:20:24 +02:00
|
|
|
use crate::state::State;
|
2019-06-26 11:19:40 +02:00
|
|
|
|
|
|
|
type Request<S, U> = Item<S, U>;
|
|
|
|
type Response<U> = <U as Encoder>::Item;
|
|
|
|
|
|
|
|
pub(crate) enum FramedMessage<T> {
|
|
|
|
Message(T),
|
|
|
|
Close,
|
|
|
|
WaitClose(oneshot::Sender<()>),
|
|
|
|
}
|
|
|
|
|
|
|
|
/// FramedTransport - is a future that reads frames from Framed object
|
|
|
|
/// and pass then to the service.
|
2019-11-14 13:38:24 +01:00
|
|
|
#[pin_project::pin_project]
|
2019-06-26 11:19:40 +02:00
|
|
|
pub(crate) struct FramedDispatcher<St, S, T, U>
|
|
|
|
where
|
|
|
|
S: Service<Request = Request<St, U>, Response = Option<Response<U>>>,
|
|
|
|
S::Error: 'static,
|
|
|
|
S::Future: 'static,
|
2019-11-14 13:38:24 +01:00
|
|
|
T: AsyncRead + AsyncWrite + Unpin,
|
|
|
|
U: Encoder + Decoder + Unpin,
|
2019-06-26 11:19:40 +02:00
|
|
|
<U as Encoder>::Item: 'static,
|
|
|
|
<U as Encoder>::Error: std::fmt::Debug,
|
|
|
|
{
|
|
|
|
service: S,
|
|
|
|
sink: Sink<<U as Encoder>::Item>,
|
2019-07-01 07:20:24 +02:00
|
|
|
state: State<St>,
|
|
|
|
dispatch_state: FramedState<S, U>,
|
2019-06-26 11:19:40 +02:00
|
|
|
framed: Framed<T, U>,
|
2019-11-14 13:38:24 +01:00
|
|
|
rx: Option<mpsc::Receiver<FramedMessage<<U as Encoder>::Item>>>,
|
2019-06-26 11:19:40 +02:00
|
|
|
inner: Cell<FramedDispatcherInner<<U as Encoder>::Item, S::Error>>,
|
2019-08-16 22:15:51 +02:00
|
|
|
disconnect: Option<Rc<dyn Fn(&mut St, bool)>>,
|
2019-06-26 11:19:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<St, S, T, U> FramedDispatcher<St, S, T, U>
|
|
|
|
where
|
|
|
|
S: Service<Request = Request<St, U>, Response = Option<Response<U>>>,
|
|
|
|
S::Error: 'static,
|
|
|
|
S::Future: 'static,
|
2019-11-14 13:38:24 +01:00
|
|
|
T: AsyncRead + AsyncWrite + Unpin,
|
|
|
|
U: Decoder + Encoder + Unpin,
|
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-07-01 07:20:24 +02:00
|
|
|
state: State<St>,
|
2019-06-26 11:19:40 +02:00
|
|
|
service: F,
|
2019-11-14 13:38:24 +01:00
|
|
|
rx: mpsc::Receiver<FramedMessage<<U as Encoder>::Item>>,
|
2019-06-26 11:19:40 +02:00
|
|
|
sink: Sink<<U as Encoder>::Item>,
|
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 {
|
|
|
|
FramedDispatcher {
|
|
|
|
framed,
|
|
|
|
state,
|
|
|
|
sink,
|
2019-07-02 08:10:05 +02:00
|
|
|
disconnect,
|
2019-06-26 11:19:40 +02:00
|
|
|
rx: Some(rx),
|
|
|
|
service: service.into_service(),
|
2019-07-01 07:20:24 +02:00
|
|
|
dispatch_state: FramedState::Processing,
|
2019-06-26 11:19:40 +02:00
|
|
|
inner: Cell::new(FramedDispatcherInner {
|
|
|
|
buf: VecDeque::new(),
|
2019-11-14 13:38:24 +01:00
|
|
|
task: LocalWaker::new(),
|
2019-06-26 11:19:40 +02:00
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
enum FramedState<S: Service, U: Encoder + Decoder + Unpin> {
|
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-14 13:38:24 +01:00
|
|
|
impl<S: Service, U: Encoder + Decoder + Unpin> 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(());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct FramedDispatcherInner<I, E> {
|
|
|
|
buf: VecDeque<Result<I, E>>,
|
2019-11-14 13:38:24 +01:00
|
|
|
task: LocalWaker,
|
2019-06-26 11:19:40 +02:00
|
|
|
}
|
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
impl<St, S, T, U> Future for FramedDispatcher<St, S, T, U>
|
2019-06-26 11:19:40 +02:00
|
|
|
where
|
|
|
|
S: Service<Request = Request<St, U>, Response = Option<Response<U>>>,
|
|
|
|
S::Error: 'static,
|
|
|
|
S::Future: 'static,
|
2019-11-14 13:38:24 +01:00
|
|
|
T: AsyncRead + AsyncWrite + Unpin,
|
|
|
|
U: Decoder + Encoder + Unpin,
|
2019-06-26 11:19:40 +02:00
|
|
|
<U as Encoder>::Item: 'static,
|
|
|
|
<U as Encoder>::Error: std::fmt::Debug,
|
|
|
|
{
|
2019-11-14 13:38:24 +01:00
|
|
|
type Output = Result<(), ServiceError<S::Error, U>>;
|
2019-07-02 08:10:05 +02:00
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
|
|
|
|
unsafe { self.inner.get_ref().task.register(cx.waker()) };
|
2019-06-26 11:19:40 +02:00
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
let this = self.project();
|
|
|
|
poll(
|
|
|
|
cx,
|
|
|
|
this.service,
|
|
|
|
this.state,
|
|
|
|
this.sink,
|
|
|
|
this.framed,
|
|
|
|
this.dispatch_state,
|
|
|
|
this.rx,
|
|
|
|
this.inner,
|
|
|
|
this.disconnect,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn poll<St, S, T, U>(
|
|
|
|
cx: &mut Context,
|
|
|
|
srv: &mut S,
|
|
|
|
state: &mut State<St>,
|
|
|
|
sink: &mut Sink<<U as Encoder>::Item>,
|
|
|
|
framed: &mut Framed<T, U>,
|
|
|
|
dispatch_state: &mut FramedState<S, U>,
|
|
|
|
rx: &mut Option<mpsc::Receiver<FramedMessage<<U as Encoder>::Item>>>,
|
|
|
|
inner: &mut Cell<FramedDispatcherInner<<U as Encoder>::Item, S::Error>>,
|
|
|
|
disconnect: &mut Option<Rc<dyn Fn(&mut St, bool)>>,
|
|
|
|
) -> Poll<Result<(), ServiceError<S::Error, U>>>
|
|
|
|
where
|
|
|
|
S: Service<Request = Request<St, U>, Response = Option<Response<U>>>,
|
|
|
|
S::Error: 'static,
|
|
|
|
S::Future: 'static,
|
|
|
|
T: AsyncRead + AsyncWrite + Unpin,
|
|
|
|
U: Decoder + Encoder + Unpin,
|
|
|
|
<U as Encoder>::Item: 'static,
|
|
|
|
<U as Encoder>::Error: std::fmt::Debug,
|
|
|
|
{
|
|
|
|
match mem::replace(dispatch_state, FramedState::Processing) {
|
|
|
|
FramedState::Processing => {
|
|
|
|
if poll_read(cx, srv, state, sink, framed, dispatch_state, inner)
|
|
|
|
|| poll_write(cx, framed, dispatch_state, rx, inner)
|
|
|
|
{
|
|
|
|
poll(
|
|
|
|
cx,
|
|
|
|
srv,
|
|
|
|
state,
|
|
|
|
sink,
|
|
|
|
framed,
|
|
|
|
dispatch_state,
|
|
|
|
rx,
|
|
|
|
inner,
|
|
|
|
disconnect,
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
Poll::Pending
|
|
|
|
}
|
|
|
|
}
|
|
|
|
FramedState::Error(err) => {
|
|
|
|
if framed.is_write_buf_empty()
|
|
|
|
|| (poll_write(cx, framed, dispatch_state, rx, inner)
|
|
|
|
|| framed.is_write_buf_empty())
|
|
|
|
{
|
|
|
|
if let Some(ref disconnect) = disconnect {
|
|
|
|
(&*disconnect)(&mut *state.get_mut(), true);
|
2019-06-26 11:19:40 +02:00
|
|
|
}
|
2019-11-14 13:38:24 +01:00
|
|
|
Poll::Ready(Err(err))
|
|
|
|
} else {
|
|
|
|
*dispatch_state = FramedState::Error(err);
|
|
|
|
Poll::Pending
|
2019-06-26 11:19:40 +02:00
|
|
|
}
|
|
|
|
}
|
2019-11-14 13:38:24 +01:00
|
|
|
FramedState::FlushAndStop(mut vec) => {
|
|
|
|
if !framed.is_write_buf_empty() {
|
|
|
|
match Pin::new(framed).poll_flush(cx) {
|
|
|
|
Poll::Ready(Err(err)) => {
|
|
|
|
debug!("Error sending data: {:?}", err);
|
2019-06-26 11:19:40 +02:00
|
|
|
}
|
2019-11-14 13:38:24 +01:00
|
|
|
Poll::Pending => {
|
|
|
|
*dispatch_state = FramedState::FlushAndStop(vec);
|
|
|
|
return Poll::Pending;
|
2019-06-26 11:19:40 +02:00
|
|
|
}
|
2019-11-14 13:38:24 +01:00
|
|
|
Poll::Ready(_) => (),
|
2019-06-26 11:19:40 +02:00
|
|
|
}
|
2019-11-14 13:38:24 +01:00
|
|
|
};
|
|
|
|
for tx in vec.drain(..) {
|
|
|
|
let _ = tx.send(());
|
|
|
|
}
|
|
|
|
if let Some(ref disconnect) = disconnect {
|
|
|
|
(&*disconnect)(&mut *state.get_mut(), false);
|
|
|
|
}
|
|
|
|
Poll::Ready(Ok(()))
|
|
|
|
}
|
|
|
|
FramedState::FramedError(err) => {
|
|
|
|
if let Some(ref disconnect) = disconnect {
|
|
|
|
(&*disconnect)(&mut *state.get_mut(), true);
|
|
|
|
}
|
|
|
|
Poll::Ready(Err(err))
|
|
|
|
}
|
|
|
|
FramedState::Stopping => {
|
|
|
|
if let Some(ref disconnect) = disconnect {
|
|
|
|
(&*disconnect)(&mut *state.get_mut(), false);
|
2019-06-26 11:19:40 +02:00
|
|
|
}
|
2019-11-14 13:38:24 +01:00
|
|
|
Poll::Ready(Ok(()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-26 11:19:40 +02:00
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
fn poll_read<St, S, T, U>(
|
|
|
|
cx: &mut Context,
|
|
|
|
srv: &mut S,
|
|
|
|
state: &mut State<St>,
|
|
|
|
sink: &mut Sink<<U as Encoder>::Item>,
|
|
|
|
framed: &mut Framed<T, U>,
|
|
|
|
dispatch_state: &mut FramedState<S, U>,
|
|
|
|
inner: &mut Cell<FramedDispatcherInner<<U as Encoder>::Item, S::Error>>,
|
|
|
|
) -> bool
|
|
|
|
where
|
|
|
|
S: Service<Request = Request<St, U>, Response = Option<Response<U>>>,
|
|
|
|
S::Error: 'static,
|
|
|
|
S::Future: 'static,
|
|
|
|
T: AsyncRead + AsyncWrite + Unpin,
|
|
|
|
U: Decoder + Encoder + Unpin,
|
|
|
|
<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))) => {
|
|
|
|
*dispatch_state = FramedState::FramedError(ServiceError::Decoder(err));
|
2019-06-26 11:19:40 +02:00
|
|
|
return true;
|
|
|
|
}
|
2019-11-14 13:38:24 +01:00
|
|
|
Poll::Pending => return false,
|
|
|
|
Poll::Ready(None) => {
|
|
|
|
log::trace!("Client disconnected");
|
|
|
|
*dispatch_state = FramedState::Stopping;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut cell = inner.clone();
|
|
|
|
tokio_executor::current_thread::spawn(
|
|
|
|
srv.call(Item::new(state.clone(), sink.clone(), item))
|
|
|
|
.then(move |item| {
|
|
|
|
let item = match item {
|
|
|
|
Ok(Some(item)) => Ok(item),
|
|
|
|
Ok(None) => return ready(()),
|
|
|
|
Err(err) => Err(err),
|
|
|
|
};
|
|
|
|
unsafe {
|
|
|
|
let inner = cell.get_mut();
|
|
|
|
inner.buf.push_back(item);
|
|
|
|
inner.task.wake();
|
|
|
|
}
|
|
|
|
ready(())
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
Poll::Pending => return false,
|
|
|
|
Poll::Ready(Err(err)) => {
|
|
|
|
*dispatch_state = FramedState::Error(ServiceError::Service(err));
|
|
|
|
return true;
|
2019-06-26 11:19:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
/// write to framed object
|
|
|
|
fn poll_write<St, S, T, U>(
|
|
|
|
cx: &mut Context,
|
|
|
|
framed: &mut Framed<T, U>,
|
|
|
|
dispatch_state: &mut FramedState<S, U>,
|
|
|
|
rx: &mut Option<mpsc::Receiver<FramedMessage<<U as Encoder>::Item>>>,
|
|
|
|
inner: &mut Cell<FramedDispatcherInner<<U as Encoder>::Item, S::Error>>,
|
|
|
|
) -> bool
|
2019-06-26 11:19:40 +02:00
|
|
|
where
|
|
|
|
S: Service<Request = Request<St, U>, Response = Option<Response<U>>>,
|
|
|
|
S::Error: 'static,
|
|
|
|
S::Future: 'static,
|
2019-11-14 13:38:24 +01:00
|
|
|
T: AsyncRead + AsyncWrite + Unpin,
|
|
|
|
U: Decoder + Encoder + Unpin,
|
2019-06-26 11:19:40 +02:00
|
|
|
<U as Encoder>::Item: 'static,
|
|
|
|
<U as Encoder>::Error: std::fmt::Debug,
|
|
|
|
{
|
2019-11-14 13:38:24 +01:00
|
|
|
let inner = unsafe { 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 09:30:04 +01:00
|
|
|
if let Err(err) = framed.write(msg) {
|
2019-11-14 13:38:24 +01:00
|
|
|
*dispatch_state =
|
|
|
|
FramedState::FramedError(ServiceError::Encoder(err));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
buf_empty = inner.buf.is_empty();
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
*dispatch_state = FramedState::Error(ServiceError::Service(err));
|
|
|
|
return true;
|
|
|
|
}
|
2019-06-26 11:19:40 +02:00
|
|
|
}
|
|
|
|
}
|
2019-11-14 13:38:24 +01: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 09:30:04 +01:00
|
|
|
if let Err(err) = framed.write(msg) {
|
2019-11-14 13:38:24 +01:00
|
|
|
*dispatch_state =
|
|
|
|
FramedState::FramedError(ServiceError::Encoder(err));
|
|
|
|
return true;
|
2019-06-26 11:19:40 +02:00
|
|
|
}
|
|
|
|
}
|
2019-11-14 13:38:24 +01:00
|
|
|
Poll::Ready(Some(FramedMessage::Close)) => {
|
|
|
|
dispatch_state.stop(None);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
Poll::Ready(Some(FramedMessage::WaitClose(tx))) => {
|
|
|
|
dispatch_state.stop(Some(tx));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
Poll::Ready(None) => {
|
|
|
|
rx_done = true;
|
|
|
|
let _ = rx.take();
|
|
|
|
}
|
|
|
|
Poll::Pending => rx_done = true,
|
2019-06-26 11:19:40 +02:00
|
|
|
}
|
|
|
|
}
|
2019-11-14 13:38:24 +01:00
|
|
|
|
|
|
|
if rx_done && buf_empty {
|
|
|
|
break;
|
2019-07-02 08:10:05 +02:00
|
|
|
}
|
2019-11-14 13:38:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if !framed.is_write_buf_empty() {
|
|
|
|
match framed.flush(cx) {
|
|
|
|
Poll::Pending => break,
|
|
|
|
Poll::Ready(Err(err)) => {
|
|
|
|
debug!("Error sending data: {:?}", err);
|
|
|
|
*dispatch_state = FramedState::FramedError(ServiceError::Encoder(err));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
Poll::Ready(_) => (),
|
2019-07-03 09:02:03 +02:00
|
|
|
}
|
2019-11-14 13:38:24 +01:00
|
|
|
} else {
|
|
|
|
break;
|
2019-06-26 11:19:40 +02:00
|
|
|
}
|
|
|
|
}
|
2019-11-14 13:38:24 +01:00
|
|
|
|
|
|
|
false
|
2019-06-26 11:19:40 +02:00
|
|
|
}
|