1
0
mirror of https://github.com/fafhrd91/actix-net synced 2025-01-31 12:42:09 +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 std::task::{Context, Poll};
use actix_codec::{AsyncRead, AsyncWrite, Decoder, Encoder, Framed}; use actix_codec::{AsyncRead, AsyncWrite, Decoder, Encoder, Framed};
use actix_utils::mpsc;
use futures::Stream; use futures::Stream;
use crate::dispatcher::FramedMessage;
use crate::sink::Sink; use crate::sink::Sink;
pub struct Connect<Io, St = (), Codec = ()> { pub struct Connect<Io, Codec, Err, St = ()>
where
Codec: Encoder + Decoder,
{
io: Io, io: Io,
sink: Sink<<Codec as Encoder>::Item, Err>,
_t: PhantomData<(St, Codec)>, _t: PhantomData<(St, Codec)>,
} }
impl<Io> Connect<Io> impl<Io, Codec, Err> Connect<Io, Codec, Err>
where where
Io: AsyncRead + AsyncWrite, 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 { Self {
io, io,
sink,
_t: PhantomData, _t: PhantomData,
} }
} }
pub fn codec<Codec>(self, codec: Codec) -> ConnectResult<Io, (), Codec> pub fn codec(self, codec: Codec) -> ConnectResult<Io, (), Codec, Err> {
where
Codec: Encoder + Decoder,
{
let (tx, rx) = mpsc::channel();
let sink = Sink::new(tx);
ConnectResult { ConnectResult {
state: (), state: (),
sink: self.sink,
framed: Framed::new(self.io, codec), framed: Framed::new(self.io, codec),
rx,
sink,
} }
} }
} }
#[pin_project::pin_project] #[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) state: St,
pub(crate) framed: Framed<Io, Codec>, pub(crate) framed: Framed<Io, Codec>,
pub(crate) rx: mpsc::Receiver<FramedMessage<<Codec as Encoder>::Item>>, pub(crate) sink: Sink<<Codec as Encoder>::Item, Err>,
pub(crate) sink: Sink<<Codec as Encoder>::Item>,
} }
impl<Io, St, Codec: Encoder + Decoder> ConnectResult<Io, St, Codec> { impl<Io, St, Codec: Encoder + Decoder, Err> ConnectResult<Io, St, Codec, Err> {
#[inline] #[inline]
pub fn sink(&self) -> &Sink<<Codec as Encoder>::Item> { pub fn sink(&self) -> &Sink<<Codec as Encoder>::Item, Err> {
&self.sink &self.sink
} }
@ -66,17 +62,16 @@ impl<Io, St, Codec: Encoder + Decoder> ConnectResult<Io, St, Codec> {
} }
#[inline] #[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 { ConnectResult {
state, state,
framed: self.framed, framed: self.framed,
rx: self.rx,
sink: self.sink, 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 where
Io: AsyncRead + AsyncWrite, Io: AsyncRead + AsyncWrite,
Codec: Encoder + Decoder, 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 where
Io: AsyncRead + AsyncWrite, Io: AsyncRead + AsyncWrite,
Codec: Encoder + Decoder, Codec: Encoder + Decoder,

View File

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

View File

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

View File

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

View File

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