1
0
mirror of https://github.com/fafhrd91/actix-net synced 2025-01-19 00:31:50 +01:00

refactor ioframe dispatcher

This commit is contained in:
Nikolay Kim 2019-12-11 14:36:11 +06:00
parent dded482514
commit 2c81c22b3e
8 changed files with 356 additions and 459 deletions

View File

@ -1,39 +0,0 @@
//! Custom cell impl
use std::cell::UnsafeCell;
use std::fmt;
use std::rc::Rc;
pub(crate) struct Cell<T> {
inner: Rc<UnsafeCell<T>>,
}
impl<T> Clone for Cell<T> {
fn clone(&self) -> Self {
Self {
inner: self.inner.clone(),
}
}
}
impl<T: fmt::Debug> fmt::Debug for Cell<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.inner.fmt(f)
}
}
impl<T> Cell<T> {
pub fn new(inner: T) -> Self {
Self {
inner: Rc::new(UnsafeCell::new(inner)),
}
}
pub(crate) unsafe fn get_ref(&mut self) -> &T {
&*self.inner.as_ref().get()
}
pub(crate) unsafe fn get_mut(&mut self) -> &mut T {
&mut *self.inner.as_ref().get()
}
}

View File

@ -3,55 +3,51 @@ use std::pin::Pin;
use std::task::{Context, Poll};
use actix_codec::{AsyncRead, AsyncWrite, Decoder, Encoder, Framed};
use actix_utils::mpsc;
use futures::Stream;
use crate::dispatcher::FramedMessage;
use crate::sink::Sink;
pub struct Connect<Io, St = (), Codec = ()> {
pub struct Connect<Io, Codec, Err, St = ()>
where
Codec: Encoder + Decoder,
{
io: Io,
sink: Sink<<Codec as Encoder>::Item, Err>,
_t: PhantomData<(St, Codec)>,
}
impl<Io> Connect<Io>
impl<Io, Codec, Err> Connect<Io, Codec, Err>
where
Io: AsyncRead + AsyncWrite,
Codec: Encoder + Decoder,
{
pub(crate) fn new(io: Io) -> Self {
pub(crate) fn new(io: Io, sink: Sink<<Codec as Encoder>::Item, Err>) -> Self {
Self {
io,
sink,
_t: PhantomData,
}
}
pub fn codec<Codec>(self, codec: Codec) -> ConnectResult<Io, (), Codec>
where
Codec: Encoder + Decoder,
{
let (tx, rx) = mpsc::channel();
let sink = Sink::new(tx);
pub fn codec(self, codec: Codec) -> ConnectResult<Io, (), Codec, Err> {
ConnectResult {
state: (),
sink: self.sink,
framed: Framed::new(self.io, codec),
rx,
sink,
}
}
}
#[pin_project::pin_project]
pub struct ConnectResult<Io, St, Codec: Encoder + Decoder> {
pub struct ConnectResult<Io, St, Codec: Encoder + Decoder, Err> {
pub(crate) state: St,
pub(crate) framed: Framed<Io, Codec>,
pub(crate) rx: mpsc::Receiver<FramedMessage<<Codec as Encoder>::Item>>,
pub(crate) sink: Sink<<Codec as Encoder>::Item>,
pub(crate) sink: Sink<<Codec as Encoder>::Item, Err>,
}
impl<Io, St, Codec: Encoder + Decoder> ConnectResult<Io, St, Codec> {
impl<Io, St, Codec: Encoder + Decoder, Err> ConnectResult<Io, St, Codec, Err> {
#[inline]
pub fn sink(&self) -> &Sink<<Codec as Encoder>::Item> {
pub fn sink(&self) -> &Sink<<Codec as Encoder>::Item, Err> {
&self.sink
}
@ -66,17 +62,16 @@ impl<Io, St, Codec: Encoder + Decoder> ConnectResult<Io, St, Codec> {
}
#[inline]
pub fn state<S>(self, state: S) -> ConnectResult<Io, S, Codec> {
pub fn state<S>(self, state: S) -> ConnectResult<Io, S, Codec, Err> {
ConnectResult {
state,
framed: self.framed,
rx: self.rx,
sink: self.sink,
}
}
}
impl<Io, St, Codec> Stream for ConnectResult<Io, St, Codec>
impl<Io, St, Codec, Err> Stream for ConnectResult<Io, St, Codec, Err>
where
Io: AsyncRead + AsyncWrite,
Codec: Encoder + Decoder,
@ -88,7 +83,8 @@ where
}
}
impl<Io, St, Codec> futures::Sink<<Codec as Encoder>::Item> for ConnectResult<Io, St, Codec>
impl<Io, St, Codec, Err> futures::Sink<<Codec as Encoder>::Item>
for ConnectResult<Io, St, Codec, Err>
where
Io: AsyncRead + AsyncWrite,
Codec: Encoder + Decoder,

View File

@ -1,39 +1,34 @@
//! Framed dispatcher service and related utilities
use std::collections::VecDeque;
use std::mem;
use std::pin::Pin;
use std::rc::Rc;
use std::task::{Context, Poll};
use actix_codec::{AsyncRead, AsyncWrite, Decoder, Encoder, Framed};
use actix_service::{IntoService, Service};
use actix_utils::task::LocalWaker;
use actix_utils::{mpsc, oneshot};
use futures::future::ready;
use futures::{FutureExt, Sink as FutureSink, Stream};
use futures::{FutureExt, Stream};
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 Request<S, U, E> = Item<S, U, E>;
type Response<U> = <U as Encoder>::Item;
pub(crate) enum FramedMessage<T> {
Message(T),
Close,
pub(crate) enum Message<T> {
Item(T),
WaitClose(oneshot::Sender<()>),
Close,
}
/// FramedTransport - is a future that reads frames from Framed object
/// and pass then to the service.
#[pin_project::pin_project]
pub(crate) struct FramedDispatcher<St, S, T, U>
pub(crate) struct Dispatcher<St, S, T, U, E>
where
St: Clone,
S: Service<Request = Request<St, U>, Response = Option<Response<U>>>,
S: Service<Request = Request<St, U, E>, Response = Option<Response<U>>, Error = E>,
S::Error: 'static,
S::Future: 'static,
T: AsyncRead + AsyncWrite,
@ -42,19 +37,19 @@ where
<U as Encoder>::Error: std::fmt::Debug,
{
service: S,
sink: Sink<<U as Encoder>::Item>,
sink: Sink<<U as Encoder>::Item, E>,
state: St,
dispatch_state: FramedState<S, U>,
framed: Framed<T, U>,
rx: Option<mpsc::Receiver<FramedMessage<<U as Encoder>::Item>>>,
inner: Cell<FramedDispatcherInner<<U as Encoder>::Item, S::Error>>,
rx: mpsc::Receiver<Result<Message<<U as Encoder>::Item>, E>>,
tx: mpsc::Sender<Result<Message<<U as Encoder>::Item>, E>>,
disconnect: Option<Rc<dyn Fn(&mut St, bool)>>,
}
impl<St, S, T, U> FramedDispatcher<St, S, T, U>
impl<St, S, T, U, E> Dispatcher<St, S, T, U, E>
where
St: Clone,
S: Service<Request = Request<St, U>, Response = Option<Response<U>>>,
S: Service<Request = Request<St, U, E>, Response = Option<Response<U>>, Error = E>,
S::Error: 'static,
S::Future: 'static,
T: AsyncRead + AsyncWrite,
@ -66,22 +61,21 @@ where
framed: Framed<T, U>,
state: St,
service: F,
rx: mpsc::Receiver<FramedMessage<<U as Encoder>::Item>>,
sink: Sink<<U as Encoder>::Item>,
sink: Sink<<U as Encoder>::Item, E>,
rx: mpsc::Receiver<Result<Message<<U as Encoder>::Item>, E>>,
disconnect: Option<Rc<dyn Fn(&mut St, bool)>>,
) -> Self {
FramedDispatcher {
let tx = rx.sender();
Dispatcher {
framed,
state,
sink,
disconnect,
rx: Some(rx),
rx,
tx,
service: service.into_service(),
dispatch_state: FramedState::Processing,
inner: Cell::new(FramedDispatcherInner {
buf: VecDeque::new(),
task: LocalWaker::new(),
}),
}
}
}
@ -116,17 +110,26 @@ impl<S: Service, U: Encoder + Decoder> FramedState<S, U> {
}
}
}
fn take_error(&mut self) -> ServiceError<S::Error, U> {
match std::mem::replace(self, FramedState::Processing) {
FramedState::Error(err) => err,
_ => panic!(),
}
}
fn take_framed_error(&mut self) -> ServiceError<S::Error, U> {
match std::mem::replace(self, FramedState::Processing) {
FramedState::FramedError(err) => err,
_ => panic!(),
}
}
}
struct FramedDispatcherInner<I, E> {
buf: VecDeque<Result<I, E>>,
task: LocalWaker,
}
impl<St, S, T, U> FramedDispatcher<St, S, T, U>
impl<St, S, T, U, E> Dispatcher<St, S, T, U, E>
where
St: Clone,
S: Service<Request = Request<St, U>, Response = Option<Response<U>>>,
S: Service<Request = Request<St, U, E>, Response = Option<Response<U>>, Error = E>,
S::Error: 'static,
S::Future: 'static,
T: AsyncRead + AsyncWrite,
@ -134,263 +137,150 @@ where
<U as Encoder>::Item: 'static,
<U as Encoder>::Error: std::fmt::Debug,
{
fn poll_read(&mut self, cx: &mut Context<'_>) -> bool {
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.dispatch_state =
FramedState::FramedError(ServiceError::Decoder(err));
return true;
}
Poll::Pending => return false,
Poll::Ready(None) => {
log::trace!("Client disconnected");
self.dispatch_state = FramedState::Stopping;
return true;
}
};
let tx = self.tx.clone();
actix_rt::spawn(
self.service
.call(Item::new(self.state.clone(), self.sink.clone(), item))
.map(move |item| {
let item = match item {
Ok(Some(item)) => Ok(Message::Item(item)),
Ok(None) => return,
Err(err) => Err(err),
};
let _ = tx.send(item);
}),
);
}
Poll::Pending => return false,
Poll::Ready(Err(err)) => {
self.dispatch_state = FramedState::Error(ServiceError::Service(err));
return true;
}
}
}
}
/// write to framed object
fn poll_write(&mut self, cx: &mut Context<'_>) -> bool {
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.dispatch_state =
FramedState::FramedError(ServiceError::Encoder(err));
return true;
}
}
Poll::Ready(Some(Ok(Message::Close))) => {
self.dispatch_state.stop(None);
return true;
}
Poll::Ready(Some(Ok(Message::WaitClose(tx)))) => {
self.dispatch_state.stop(Some(tx));
return true;
}
Poll::Ready(Some(Err(err))) => {
self.dispatch_state = FramedState::Error(ServiceError::Service(err));
return true;
}
Poll::Ready(None) | Poll::Pending => break,
}
}
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.dispatch_state =
FramedState::FramedError(ServiceError::Encoder(err));
return true;
}
}
} else {
break;
}
}
false
}
pub(crate) fn poll(
&mut self,
cx: &mut Context<'_>,
) -> Poll<Result<(), ServiceError<S::Error, U>>> {
let this = self;
unsafe { this.inner.get_ref().task.register(cx.waker()) };
poll(
cx,
&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,
)
}
}
fn poll<St, S, T, U>(
cx: &mut Context<'_>,
srv: &mut S,
state: &mut 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
St: Clone,
S: Service<Request = Request<St, U>, Response = Option<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,
{
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, true);
match self.dispatch_state {
FramedState::Processing => {
if self.poll_read(cx) || self.poll_write(cx) {
self.poll(cx)
} else {
Poll::Pending
}
Poll::Ready(Err(err))
} else {
*dispatch_state = FramedState::Error(err);
Poll::Pending
}
}
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);
}
Poll::Pending => {
*dispatch_state = FramedState::FlushAndStop(vec);
FramedState::Error(_) => {
// flush write buffer
if !self.framed.is_write_buf_empty() {
if let Poll::Pending = self.framed.flush(cx) {
return Poll::Pending;
}
Poll::Ready(_) => (),
}
};
for tx in vec.drain(..) {
let _ = tx.send(());
if let Some(ref disconnect) = self.disconnect {
(&*disconnect)(&mut self.state, true);
}
Poll::Ready(Err(self.dispatch_state.take_error()))
}
if let Some(ref disconnect) = disconnect {
(&*disconnect)(&mut *state, false);
}
Poll::Ready(Ok(()))
}
FramedState::FramedError(err) => {
if let Some(ref disconnect) = disconnect {
(&*disconnect)(&mut *state, true);
}
Poll::Ready(Err(err))
}
FramedState::Stopping => {
if let Some(ref disconnect) = disconnect {
(&*disconnect)(&mut *state, false);
}
Poll::Ready(Ok(()))
}
}
}
fn poll_read<St, S, T, U>(
cx: &mut Context<'_>,
srv: &mut S,
state: &mut 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
St: Clone,
S: Service<Request = Request<St, U>, Response = Option<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 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));
return true;
}
Poll::Pending => return false,
Poll::Ready(None) => {
log::trace!("Client disconnected");
*dispatch_state = FramedState::Stopping;
return true;
FramedState::FlushAndStop(ref mut vec) => {
if !self.framed.is_write_buf_empty() {
match self.framed.flush(cx) {
Poll::Ready(Err(err)) => {
debug!("Error sending data: {:?}", err);
}
Poll::Pending => {
return Poll::Pending;
}
Poll::Ready(_) => (),
}
};
let mut cell = inner.clone();
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(())
},
));
for tx in vec.drain(..) {
let _ = tx.send(());
}
if let Some(ref disconnect) = self.disconnect {
(&*disconnect)(&mut self.state, false);
}
Poll::Ready(Ok(()))
}
Poll::Pending => return false,
Poll::Ready(Err(err)) => {
*dispatch_state = FramedState::Error(ServiceError::Service(err));
return true;
FramedState::FramedError(_) => {
if let Some(ref disconnect) = self.disconnect {
(&*disconnect)(&mut self.state, true);
}
Poll::Ready(Err(self.dispatch_state.take_framed_error()))
}
FramedState::Stopping => {
if let Some(ref disconnect) = self.disconnect {
(&*disconnect)(&mut self.state, false);
}
Poll::Ready(Ok(()))
}
}
}
}
/// 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
where
S: Service<Request = Request<St, U>, Response = Option<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,
{
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) => {
if let Err(err) = framed.write(msg) {
*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;
}
}
}
if !rx_done && rx.is_some() {
match Pin::new(rx.as_mut().unwrap()).poll_next(cx) {
Poll::Ready(Some(FramedMessage::Message(msg))) => {
if let Err(err) = framed.write(msg) {
*dispatch_state =
FramedState::FramedError(ServiceError::Encoder(err));
return true;
}
}
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,
}
}
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);
*dispatch_state = FramedState::FramedError(ServiceError::Encoder(err));
return true;
}
Poll::Ready(_) => (),
}
} else {
break;
}
}
false
}

View File

@ -5,19 +5,19 @@ use actix_codec::{Decoder, Encoder};
use crate::sink::Sink;
pub struct Item<St, Codec: Encoder + Decoder> {
pub struct Item<St, Codec: Encoder + Decoder, E> {
state: St,
sink: Sink<<Codec as Encoder>::Item>,
sink: Sink<<Codec as Encoder>::Item, E>,
item: <Codec as Decoder>::Item,
}
impl<St, Codec> Item<St, Codec>
impl<St, Codec, E> Item<St, Codec, E>
where
Codec: Encoder + Decoder,
{
pub(crate) fn new(
state: St,
sink: Sink<<Codec as Encoder>::Item>,
sink: Sink<<Codec as Encoder>::Item, E>,
item: <Codec as Decoder>::Item,
) -> Self {
Item { state, sink, item }
@ -34,7 +34,7 @@ where
}
#[inline]
pub fn sink(&self) -> &Sink<<Codec as Encoder>::Item> {
pub fn sink(&self) -> &Sink<<Codec as Encoder>::Item, E> {
&self.sink
}
@ -44,12 +44,18 @@ where
}
#[inline]
pub fn into_parts(self) -> (St, Sink<<Codec as Encoder>::Item>, <Codec as Decoder>::Item) {
pub fn into_parts(
self,
) -> (
St,
Sink<<Codec as Encoder>::Item, E>,
<Codec as Decoder>::Item,
) {
(self.state, self.sink, self.item)
}
}
impl<St, Codec> Deref for Item<St, Codec>
impl<St, Codec, E> Deref for Item<St, Codec, E>
where
Codec: Encoder + Decoder,
{
@ -61,7 +67,7 @@ where
}
}
impl<St, Codec> DerefMut for Item<St, Codec>
impl<St, Codec, E> DerefMut for Item<St, Codec, E>
where
Codec: Encoder + Decoder,
{
@ -71,12 +77,12 @@ where
}
}
impl<St, Codec> fmt::Debug for Item<St, Codec>
impl<St, Codec, E> fmt::Debug for Item<St, Codec, E>
where
Codec: Encoder + Decoder,
<Codec as Decoder>::Item: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("FramedItem").field(&self.item).finish()
f.debug_tuple("Item").field(&self.item).finish()
}
}

View File

@ -1,7 +1,6 @@
#![deny(rust_2018_idioms, warnings)]
#![allow(clippy::type_complexity, clippy::too_many_arguments)]
mod cell;
mod connect;
mod dispatcher;
mod error;

View File

@ -6,17 +6,20 @@ use std::task::{Context, Poll};
use actix_codec::{AsyncRead, AsyncWrite, Decoder, Encoder};
use actix_service::{IntoService, IntoServiceFactory, Service, ServiceFactory};
use actix_utils::mpsc;
use either::Either;
use futures::future::{FutureExt, LocalBoxFuture};
use pin_project::project;
use crate::connect::{Connect, ConnectResult};
use crate::dispatcher::FramedDispatcher;
use crate::dispatcher::{Dispatcher, Message};
use crate::error::ServiceError;
use crate::item::Item;
use crate::sink::Sink;
type RequestItem<S, U> = Item<S, U>;
type RequestItem<S, U, E> = Item<S, U, E>;
type ResponseItem<U> = Option<<U as Encoder>::Item>;
type ServiceResult<U, E> = Result<Message<<U as Encoder>::Item>, E>;
/// Service builder - structure that follows the builder pattern
/// for building instances for framed services.
@ -34,11 +37,15 @@ impl<St: Clone, Codec> Builder<St, Codec> {
}
/// Construct framed handler service with specified connect service
pub fn service<Io, C, F>(self, connect: F) -> ServiceBuilder<St, C, Io, Codec>
pub fn service<Io, C, F, E>(self, connect: F) -> ServiceBuilder<St, C, Io, Codec, E>
where
F: IntoService<C>,
Io: AsyncRead + AsyncWrite,
C: Service<Request = Connect<Io>, Response = ConnectResult<Io, St, Codec>>,
C: Service<
Request = Connect<Io, Codec, E>,
Response = ConnectResult<Io, St, Codec, E>,
Error = E,
>,
Codec: Decoder + Encoder,
{
ServiceBuilder {
@ -49,16 +56,17 @@ impl<St: Clone, Codec> Builder<St, Codec> {
}
/// Construct framed handler new service with specified connect service
pub fn factory<Io, C, F>(self, connect: F) -> NewServiceBuilder<St, C, Io, Codec>
pub fn factory<Io, C, F, E>(self, connect: F) -> NewServiceBuilder<St, C, Io, Codec, E>
where
F: IntoServiceFactory<C>,
E: 'static,
Io: AsyncRead + AsyncWrite,
C: ServiceFactory<
Config = (),
Request = Connect<Io>,
Response = ConnectResult<Io, St, Codec>,
Request = Connect<Io, Codec, E>,
Response = ConnectResult<Io, St, Codec, E>,
Error = E,
>,
C::Error: 'static,
C::Future: 'static,
Codec: Decoder + Encoder,
{
@ -70,17 +78,20 @@ impl<St: Clone, Codec> Builder<St, Codec> {
}
}
pub struct ServiceBuilder<St, C, Io, Codec> {
pub struct ServiceBuilder<St, C, Io, Codec, Err> {
connect: C,
disconnect: Option<Rc<dyn Fn(&mut St, bool)>>,
_t: PhantomData<(St, Io, Codec)>,
_t: PhantomData<(St, Io, Codec, Err)>,
}
impl<St, C, Io, Codec> ServiceBuilder<St, C, Io, Codec>
impl<St, C, Io, Codec, Err> ServiceBuilder<St, C, Io, Codec, Err>
where
St: Clone,
C: Service<Request = Connect<Io>, Response = ConnectResult<Io, St, Codec>>,
C::Error: 'static,
C: Service<
Request = Connect<Io, Codec, Err>,
Response = ConnectResult<Io, St, Codec, Err>,
Error = Err,
>,
Io: AsyncRead + AsyncWrite,
Codec: Decoder + Encoder,
<Codec as Encoder>::Item: 'static,
@ -98,16 +109,16 @@ where
}
/// Provide stream items handler service and construct service factory.
pub fn finish<F, T>(self, service: F) -> FramedServiceImpl<St, C, T, Io, Codec>
pub fn finish<F, T>(self, service: F) -> FramedServiceImpl<St, C, T, Io, Codec, Err>
where
F: IntoServiceFactory<T>,
T: ServiceFactory<
Config = St,
Request = RequestItem<St, Codec>,
Response = ResponseItem<Codec>,
Error = C::Error,
InitError = C::Error,
> + 'static,
Config = St,
Request = RequestItem<St, Codec, Err>,
Response = ResponseItem<Codec>,
Error = Err,
InitError = Err,
>,
{
FramedServiceImpl {
connect: self.connect,
@ -118,22 +129,23 @@ where
}
}
pub struct NewServiceBuilder<St, C, Io, Codec> {
pub struct NewServiceBuilder<St, C, Io, Codec, Err> {
connect: C,
disconnect: Option<Rc<dyn Fn(&mut St, bool)>>,
_t: PhantomData<(St, Io, Codec)>,
_t: PhantomData<(St, Io, Codec, Err)>,
}
impl<St, C, Io, Codec> NewServiceBuilder<St, C, Io, Codec>
impl<St, C, Io, Codec, Err> NewServiceBuilder<St, C, Io, Codec, Err>
where
St: Clone,
Io: AsyncRead + AsyncWrite,
Err: 'static,
C: ServiceFactory<
Config = (),
Request = Connect<Io>,
Response = ConnectResult<Io, St, Codec>,
Request = Connect<Io, Codec, Err>,
Response = ConnectResult<Io, St, Codec, Err>,
Error = Err,
>,
C::Error: 'static,
C::Future: 'static,
Codec: Decoder + Encoder,
<Codec as Encoder>::Item: 'static,
@ -150,15 +162,15 @@ where
self
}
pub fn finish<F, T, Cfg>(self, service: F) -> FramedService<St, C, T, Io, Codec, Cfg>
pub fn finish<F, T, Cfg>(self, service: F) -> FramedService<St, C, T, Io, Codec, Err, Cfg>
where
F: IntoServiceFactory<T>,
T: ServiceFactory<
Config = St,
Request = RequestItem<St, Codec>,
Request = RequestItem<St, Codec, Err>,
Response = ResponseItem<Codec>,
Error = C::Error,
InitError = C::Error,
Error = Err,
InitError = Err,
> + 'static,
{
FramedService {
@ -170,32 +182,34 @@ where
}
}
pub struct FramedService<St, C, T, Io, Codec, Cfg> {
pub struct FramedService<St, C, T, Io, Codec, Err, Cfg> {
connect: C,
handler: Rc<T>,
disconnect: Option<Rc<dyn Fn(&mut St, bool)>>,
_t: PhantomData<(St, Io, Codec, Cfg)>,
_t: PhantomData<(St, Io, Codec, Err, Cfg)>,
}
impl<St, C, T, Io, Codec, Cfg> ServiceFactory for FramedService<St, C, T, Io, Codec, Cfg>
impl<St, C, T, Io, Codec, Err, Cfg> ServiceFactory
for FramedService<St, C, T, Io, Codec, Err, Cfg>
where
St: Clone + 'static,
Io: AsyncRead + AsyncWrite,
C: ServiceFactory<
Config = (),
Request = Connect<Io>,
Response = ConnectResult<Io, St, Codec>,
Request = Connect<Io, Codec, Err>,
Response = ConnectResult<Io, St, Codec, Err>,
Error = Err,
>,
C::Error: 'static,
C::Future: 'static,
T: ServiceFactory<
Config = St,
Request = RequestItem<St, Codec>,
Request = RequestItem<St, Codec, Err>,
Response = ResponseItem<Codec>,
Error = C::Error,
InitError = C::Error,
Error = Err,
InitError = Err,
> + 'static,
<T::Service as Service>::Future: 'static,
Err: 'static,
Codec: Decoder + Encoder,
<Codec as Encoder>::Item: 'static,
<Codec as Encoder>::Error: std::fmt::Debug,
@ -205,7 +219,7 @@ where
type Response = ();
type Error = ServiceError<C::Error, Codec>;
type InitError = C::InitError;
type Service = FramedServiceImpl<St, C::Service, T, Io, Codec>;
type Service = FramedServiceImpl<St, C::Service, T, Io, Codec, Err>;
type Future = LocalBoxFuture<'static, Result<Self::Service, Self::InitError>>;
fn new_service(&self, _: Cfg) -> Self::Future {
@ -227,25 +241,29 @@ where
}
}
pub struct FramedServiceImpl<St, C, T, Io, Codec> {
pub struct FramedServiceImpl<St, C, T, Io, Codec, Err> {
connect: C,
handler: Rc<T>,
disconnect: Option<Rc<dyn Fn(&mut St, bool)>>,
_t: PhantomData<(St, Io, Codec)>,
_t: PhantomData<(St, Io, Codec, Err)>,
}
impl<St, C, T, Io, Codec> Service for FramedServiceImpl<St, C, T, Io, Codec>
impl<St, C, T, Io, Codec, Err> Service for FramedServiceImpl<St, C, T, Io, Codec, Err>
where
St: Clone,
Io: AsyncRead + AsyncWrite,
C: Service<Request = Connect<Io>, Response = ConnectResult<Io, St, Codec>>,
C::Error: 'static,
C: Service<
Request = Connect<Io, Codec, Err>,
Response = ConnectResult<Io, St, Codec, Err>,
Error = Err,
>,
Err: 'static,
T: ServiceFactory<
Config = St,
Request = RequestItem<St, Codec>,
Request = RequestItem<St, Codec, Err>,
Response = ResponseItem<Codec>,
Error = C::Error,
InitError = C::Error,
Error = Err,
InitError = Err,
>,
<T::Service as Service>::Future: 'static,
Codec: Decoder + Encoder,
@ -254,36 +272,43 @@ where
{
type Request = Io;
type Response = ();
type Error = ServiceError<C::Error, Codec>;
type Future = FramedServiceImplResponse<St, Io, Codec, C, T>;
type Error = ServiceError<Err, Codec>;
type Future = FramedServiceImplResponse<St, Io, Codec, Err, C, T>;
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.connect.poll_ready(cx).map_err(|e| e.into())
}
fn call(&mut self, req: Io) -> Self::Future {
let (tx, rx) = mpsc::channel();
let sink = Sink::new(tx);
FramedServiceImplResponse {
inner: FramedServiceImplResponseInner::Connect(
self.connect.call(Connect::new(req)),
self.connect.call(Connect::new(req, sink.clone())),
self.handler.clone(),
self.disconnect.clone(),
Some(rx),
),
}
}
}
#[pin_project::pin_project]
pub struct FramedServiceImplResponse<St, Io, Codec, C, T>
pub struct FramedServiceImplResponse<St, Io, Codec, Err, C, T>
where
St: Clone,
C: Service<Request = Connect<Io>, Response = ConnectResult<Io, St, Codec>>,
C::Error: 'static,
C: Service<
Request = Connect<Io, Codec, Err>,
Response = ConnectResult<Io, St, Codec, Err>,
Error = Err,
>,
Err: 'static,
T: ServiceFactory<
Config = St,
Request = RequestItem<St, Codec>,
Request = RequestItem<St, Codec, Err>,
Response = ResponseItem<Codec>,
Error = C::Error,
InitError = C::Error,
Error = Err,
InitError = Err,
>,
<T::Service as Service>::Future: 'static,
Io: AsyncRead + AsyncWrite,
@ -292,20 +317,24 @@ where
<Codec as Encoder>::Error: std::fmt::Debug,
{
#[pin]
inner: FramedServiceImplResponseInner<St, Io, Codec, C, T>,
inner: FramedServiceImplResponseInner<St, Io, Codec, Err, C, T>,
}
impl<St, Io, Codec, C, T> Future for FramedServiceImplResponse<St, Io, Codec, C, T>
impl<St, Io, Codec, Err, C, T> Future for FramedServiceImplResponse<St, Io, Codec, Err, C, T>
where
St: Clone,
C: Service<Request = Connect<Io>, Response = ConnectResult<Io, St, Codec>>,
C::Error: 'static,
C: Service<
Request = Connect<Io, Codec, Err>,
Response = ConnectResult<Io, St, Codec, Err>,
Error = Err,
>,
Err: 'static,
T: ServiceFactory<
Config = St,
Request = RequestItem<St, Codec>,
Request = RequestItem<St, Codec, Err>,
Response = ResponseItem<Codec>,
Error = C::Error,
InitError = C::Error,
Error = Err,
InitError = Err,
>,
<T::Service as Service>::Future: 'static,
Io: AsyncRead + AsyncWrite,
@ -313,7 +342,7 @@ where
<Codec as Encoder>::Item: 'static,
<Codec as Encoder>::Error: std::fmt::Debug,
{
type Output = Result<(), ServiceError<C::Error, Codec>>;
type Output = Result<(), ServiceError<Err, Codec>>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let mut this = self.as_mut().project();
@ -331,17 +360,21 @@ where
}
#[pin_project::pin_project]
enum FramedServiceImplResponseInner<St, Io, Codec, C, T>
enum FramedServiceImplResponseInner<St, Io, Codec, Err, C, T>
where
St: Clone,
C: Service<Request = Connect<Io>, Response = ConnectResult<Io, St, Codec>>,
C::Error: 'static,
C: Service<
Request = Connect<Io, Codec, Err>,
Response = ConnectResult<Io, St, Codec, Err>,
Error = Err,
>,
Err: 'static,
T: ServiceFactory<
Config = St,
Request = RequestItem<St, Codec>,
Request = RequestItem<St, Codec, Err>,
Response = ResponseItem<Codec>,
Error = C::Error,
InitError = C::Error,
Error = Err,
InitError = Err,
>,
<T::Service as Service>::Future: 'static,
Io: AsyncRead + AsyncWrite,
@ -349,26 +382,36 @@ where
<Codec as Encoder>::Item: 'static,
<Codec as Encoder>::Error: std::fmt::Debug,
{
Connect(#[pin] C::Future, Rc<T>, Option<Rc<dyn Fn(&mut St, bool)>>),
Connect(
#[pin] C::Future,
Rc<T>,
Option<Rc<dyn Fn(&mut St, bool)>>,
Option<mpsc::Receiver<ServiceResult<Codec, Err>>>,
),
Handler(
#[pin] T::Future,
Option<ConnectResult<Io, St, Codec>>,
Option<ConnectResult<Io, St, Codec, Err>>,
Option<Rc<dyn Fn(&mut St, bool)>>,
Option<mpsc::Receiver<ServiceResult<Codec, Err>>>,
),
Dispatcher(#[pin] FramedDispatcher<St, T::Service, Io, Codec>),
Dispatcher(Dispatcher<St, T::Service, Io, Codec, Err>),
}
impl<St, Io, Codec, C, T> FramedServiceImplResponseInner<St, Io, Codec, C, T>
impl<St, Io, Codec, Err, C, T> FramedServiceImplResponseInner<St, Io, Codec, Err, C, T>
where
St: Clone,
C: Service<Request = Connect<Io>, Response = ConnectResult<Io, St, Codec>>,
C::Error: 'static,
C: Service<
Request = Connect<Io, Codec, Err>,
Response = ConnectResult<Io, St, Codec, Err>,
Error = Err,
>,
Err: 'static,
T: ServiceFactory<
Config = St,
Request = RequestItem<St, Codec>,
Request = RequestItem<St, Codec, Err>,
Response = ResponseItem<Codec>,
Error = C::Error,
InitError = C::Error,
Error = Err,
InitError = Err,
>,
<T::Service as Service>::Future: 'static,
Io: AsyncRead + AsyncWrite,
@ -381,42 +424,44 @@ where
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Either<
FramedServiceImplResponseInner<St, Io, Codec, C, T>,
Poll<Result<(), ServiceError<C::Error, Codec>>>,
FramedServiceImplResponseInner<St, Io, Codec, Err, C, T>,
Poll<Result<(), ServiceError<Err, Codec>>>,
> {
#[project]
match self.project() {
FramedServiceImplResponseInner::Connect(fut, handler, disconnect) => {
FramedServiceImplResponseInner::Connect(fut, handler, disconnect, rx) => {
match fut.poll(cx) {
Poll::Ready(Ok(res)) => {
Either::Left(FramedServiceImplResponseInner::Handler(
handler.new_service(res.state.clone()),
Some(res),
disconnect.take(),
rx.take(),
))
}
Poll::Pending => Either::Right(Poll::Pending),
Poll::Ready(Err(e)) => Either::Right(Poll::Ready(Err(e.into()))),
}
}
FramedServiceImplResponseInner::Handler(fut, res, disconnect) => match fut.poll(cx)
{
Poll::Ready(Ok(handler)) => {
let res = res.take().unwrap();
Either::Left(FramedServiceImplResponseInner::Dispatcher(
FramedDispatcher::new(
res.framed,
res.state,
handler,
res.rx,
res.sink,
disconnect.take(),
),
))
FramedServiceImplResponseInner::Handler(fut, res, disconnect, rx) => {
match fut.poll(cx) {
Poll::Ready(Ok(handler)) => {
let res = res.take().unwrap();
Either::Left(FramedServiceImplResponseInner::Dispatcher(
Dispatcher::new(
res.framed,
res.state,
handler,
res.sink,
rx.take().unwrap(),
disconnect.take(),
),
))
}
Poll::Pending => Either::Right(Poll::Pending),
Poll::Ready(Err(e)) => Either::Right(Poll::Ready(Err(e.into()))),
}
Poll::Pending => Either::Right(Poll::Pending),
Poll::Ready(Err(e)) => Either::Right(Poll::Ready(Err(e.into()))),
},
}
FramedServiceImplResponseInner::Dispatcher(ref mut fut) => {
Either::Right(fut.poll(cx))
}

View File

@ -3,41 +3,41 @@ use std::fmt;
use actix_utils::{mpsc, oneshot};
use futures::future::{Future, FutureExt};
use crate::dispatcher::FramedMessage;
use crate::dispatcher::Message;
pub struct Sink<T>(mpsc::Sender<FramedMessage<T>>);
pub struct Sink<T, E>(mpsc::Sender<Result<Message<T>, E>>);
impl<T> Clone for Sink<T> {
impl<T, E> Clone for Sink<T, E> {
fn clone(&self) -> Self {
Sink(self.0.clone())
}
}
impl<T> Sink<T> {
pub(crate) fn new(tx: mpsc::Sender<FramedMessage<T>>) -> Self {
impl<T, E> Sink<T, E> {
pub(crate) fn new(tx: mpsc::Sender<Result<Message<T>, E>>) -> Self {
Sink(tx)
}
/// Close connection
pub fn close(&self) {
let _ = self.0.send(FramedMessage::Close);
let _ = self.0.send(Ok(Message::Close));
}
/// Close connection
pub fn wait_close(&self) -> impl Future<Output = ()> {
let (tx, rx) = oneshot::channel();
let _ = self.0.send(FramedMessage::WaitClose(tx));
let _ = self.0.send(Ok(Message::WaitClose(tx)));
rx.map(|_| ())
}
/// Send item
pub fn send(&self, item: T) {
let _ = self.0.send(FramedMessage::Message(item));
let _ = self.0.send(Ok(Message::Item(item)));
}
}
impl<T> fmt::Debug for Sink<T> {
impl<T, E> fmt::Debug for Sink<T, E> {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.debug_struct("Sink").finish()
}

View File

@ -22,7 +22,7 @@ async fn test_disconnect() -> std::io::Result<()> {
let disconnect1 = disconnect1.clone();
Builder::new()
.factory(fn_service(|conn: Connect<_>| {
.factory(fn_service(|conn: Connect<_, _, _>| {
ok(conn.codec(BytesCodec).state(State))
}))
.disconnect(move |_, _| {
@ -32,7 +32,7 @@ async fn test_disconnect() -> std::io::Result<()> {
});
let mut client = Builder::new()
.service(|conn: Connect<_>| {
.service(|conn: Connect<_, _, _>| {
let conn = conn.codec(BytesCodec).state(State);
conn.sink().close();
ok(conn)