2019-06-26 11:19:40 +02:00
|
|
|
//! Framed dispatcher service and related utilities
|
|
|
|
use std::collections::VecDeque;
|
|
|
|
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;
|
|
|
|
|
|
|
|
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-19 09:51:40 +01:00
|
|
|
#[pin_project::pin_project]
|
2019-06-26 11:19:40 +02:00
|
|
|
pub(crate) struct FramedDispatcher<St, S, T, U>
|
|
|
|
where
|
2019-12-02 16:27:48 +01:00
|
|
|
St: Clone,
|
2019-06-26 11:19:40 +02:00
|
|
|
S: Service<Request = Request<St, U>, Response = Option<Response<U>>>,
|
|
|
|
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,
|
|
|
|
{
|
|
|
|
service: S,
|
|
|
|
sink: Sink<<U as Encoder>::Item>,
|
2019-12-02 16:27:48 +01:00
|
|
|
state: St,
|
2019-07-01 07:20:24 +02:00
|
|
|
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
|
2019-12-02 16:27:48 +01:00
|
|
|
St: Clone,
|
2019-06-26 11:19:40 +02:00
|
|
|
S: Service<Request = Request<St, U>, Response = Option<Response<U>>>,
|
|
|
|
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,
|
|
|
|
{
|
|
|
|
pub(crate) fn new<F: IntoService<S>>(
|
|
|
|
framed: Framed<T, U>,
|
2019-12-02 16:27:48 +01:00
|
|
|
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-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>),
|
|
|
|
FlushAndStop(Vec<oneshot::Sender<()>>),
|
|
|
|
Stopping,
|
|
|
|
}
|
|
|
|
|
2019-11-19 09:51:40 +01:00
|
|
|
impl<S: Service, U: Encoder + Decoder> 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-18 13:28:54 +01:00
|
|
|
impl<St, S, T, U> FramedDispatcher<St, S, T, U>
|
|
|
|
where
|
2019-12-02 16:27:48 +01:00
|
|
|
St: Clone,
|
2019-11-19 09:51:40 +01:00
|
|
|
S: Service<Request = Request<St, U>, Response = Option<Response<U>>>,
|
2019-11-18 13:28:54 +01:00
|
|
|
S::Error: 'static,
|
2019-11-19 09:51:40 +01:00
|
|
|
S::Future: 'static,
|
|
|
|
T: AsyncRead + AsyncWrite,
|
|
|
|
U: Decoder + Encoder,
|
2019-11-18 13:28:54 +01:00
|
|
|
<U as Encoder>::Item: 'static,
|
|
|
|
<U as Encoder>::Error: std::fmt::Debug,
|
|
|
|
{
|
|
|
|
pub(crate) fn poll(
|
|
|
|
&mut self,
|
2019-12-02 17:30:09 +01:00
|
|
|
cx: &mut Context<'_>,
|
2019-11-18 13:28:54 +01:00
|
|
|
) -> Poll<Result<(), ServiceError<S::Error, U>>> {
|
|
|
|
let this = self;
|
|
|
|
unsafe { this.inner.get_ref().task.register(cx.waker()) };
|
2019-06-26 11:19:40 +02:00
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
poll(
|
|
|
|
cx,
|
2019-11-18 13:28:54 +01:00
|
|
|
&mut this.service,
|
|
|
|
&mut this.state,
|
|
|
|
&mut this.sink,
|
|
|
|
&mut this.framed,
|
|
|
|
&mut this.dispatch_state,
|
|
|
|
&mut this.rx,
|
|
|
|
&mut this.inner,
|
|
|
|
&mut this.disconnect,
|
2019-11-14 13:38:24 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn poll<St, S, T, U>(
|
2019-12-02 17:30:09 +01:00
|
|
|
cx: &mut Context<'_>,
|
2019-11-14 13:38:24 +01:00
|
|
|
srv: &mut S,
|
2019-12-02 16:27:48 +01:00
|
|
|
state: &mut St,
|
2019-11-14 13:38:24 +01:00
|
|
|
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
|
2019-12-02 16:27:48 +01:00
|
|
|
St: Clone,
|
2019-11-14 13:38:24 +01:00
|
|
|
S: Service<Request = Request<St, U>, Response = Option<Response<U>>>,
|
|
|
|
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,
|
|
|
|
{
|
|
|
|
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 {
|
2019-12-02 16:27:48 +01:00
|
|
|
(&*disconnect)(&mut *state, 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 {
|
2019-12-02 16:27:48 +01:00
|
|
|
(&*disconnect)(&mut *state, false);
|
2019-11-14 13:38:24 +01:00
|
|
|
}
|
|
|
|
Poll::Ready(Ok(()))
|
|
|
|
}
|
|
|
|
FramedState::FramedError(err) => {
|
|
|
|
if let Some(ref disconnect) = disconnect {
|
2019-12-02 16:27:48 +01:00
|
|
|
(&*disconnect)(&mut *state, true);
|
2019-11-14 13:38:24 +01:00
|
|
|
}
|
|
|
|
Poll::Ready(Err(err))
|
|
|
|
}
|
|
|
|
FramedState::Stopping => {
|
|
|
|
if let Some(ref disconnect) = disconnect {
|
2019-12-02 16:27:48 +01:00
|
|
|
(&*disconnect)(&mut *state, 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>(
|
2019-12-02 17:30:09 +01:00
|
|
|
cx: &mut Context<'_>,
|
2019-11-14 13:38:24 +01:00
|
|
|
srv: &mut S,
|
2019-12-02 16:27:48 +01:00
|
|
|
state: &mut St,
|
2019-11-14 13:38:24 +01:00
|
|
|
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
|
2019-12-02 16:27:48 +01:00
|
|
|
St: Clone,
|
2019-11-14 13:38:24 +01:00
|
|
|
S: Service<Request = Request<St, U>, Response = Option<Response<U>>>,
|
|
|
|
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,
|
|
|
|
{
|
|
|
|
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();
|
2019-11-26 03:26:22 +01:00
|
|
|
actix_rt::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(())
|
|
|
|
},
|
|
|
|
));
|
2019-11-14 13:38:24 +01:00
|
|
|
}
|
|
|
|
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>(
|
2019-12-02 17:30:09 +01:00
|
|
|
cx: &mut Context<'_>,
|
2019-11-14 13:38:24 +01:00
|
|
|
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-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-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
|
|
|
}
|