mirror of
https://github.com/fafhrd91/actix-net
synced 2024-12-18 02:13:58 +01:00
refactor service and state manahement
This commit is contained in:
parent
1918c8d4f8
commit
5779da0f49
@ -1,5 +1,13 @@
|
|||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
|
## [0.5.0] - 2019-12-29
|
||||||
|
|
||||||
|
* Simplify state management
|
||||||
|
|
||||||
|
* Allow to set custom output stream
|
||||||
|
|
||||||
|
* Removed disconnect callback
|
||||||
|
|
||||||
## [0.4.1] - 2019-12-11
|
## [0.4.1] - 2019-12-11
|
||||||
|
|
||||||
* Disconnect callback accepts owned state
|
* Disconnect callback accepts owned state
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "actix-ioframe"
|
name = "actix-ioframe"
|
||||||
version = "0.4.1"
|
version = "0.5.0"
|
||||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||||
description = "Actix framed service"
|
description = "Actix framed service"
|
||||||
keywords = ["network", "framework", "async", "futures"]
|
keywords = ["network", "framework", "async", "futures"]
|
||||||
@ -10,19 +10,18 @@ documentation = "https://docs.rs/actix-ioframe/"
|
|||||||
categories = ["network-programming", "asynchronous"]
|
categories = ["network-programming", "asynchronous"]
|
||||||
license = "MIT/Apache-2.0"
|
license = "MIT/Apache-2.0"
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
workspace = ".."
|
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
name = "actix_ioframe"
|
name = "actix_ioframe"
|
||||||
path = "src/lib.rs"
|
path = "src/lib.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-service = "1.0.0"
|
actix-service = "1.0.1"
|
||||||
actix-codec = "0.2.0"
|
actix-codec = "0.2.0"
|
||||||
actix-utils = "1.0.1"
|
actix-utils = "1.0.4"
|
||||||
actix-rt = "1.0.0"
|
actix-rt = "1.0.0"
|
||||||
bytes = "0.5"
|
bytes = "0.5.3"
|
||||||
either = "1.5.2"
|
either = "1.5.3"
|
||||||
futures = "0.3.1"
|
futures = "0.3.1"
|
||||||
pin-project = "0.4.6"
|
pin-project = "0.4.6"
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
|
@ -3,17 +3,15 @@ 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::Receiver;
|
||||||
use futures::Stream;
|
use futures::Stream;
|
||||||
|
|
||||||
use crate::sink::Sink;
|
pub struct Connect<Io, Codec>
|
||||||
|
|
||||||
pub struct Connect<Io, Codec, St = ()>
|
|
||||||
where
|
where
|
||||||
Codec: Encoder + Decoder,
|
Codec: Encoder + Decoder,
|
||||||
{
|
{
|
||||||
io: Io,
|
io: Io,
|
||||||
sink: Sink<<Codec as Encoder>::Item>,
|
_t: PhantomData<Codec>,
|
||||||
_t: PhantomData<(St, Codec)>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Io, Codec> Connect<Io, Codec>
|
impl<Io, Codec> Connect<Io, Codec>
|
||||||
@ -21,36 +19,33 @@ where
|
|||||||
Io: AsyncRead + AsyncWrite,
|
Io: AsyncRead + AsyncWrite,
|
||||||
Codec: Encoder + Decoder,
|
Codec: Encoder + Decoder,
|
||||||
{
|
{
|
||||||
pub(crate) fn new(io: Io, sink: Sink<<Codec as Encoder>::Item>) -> Self {
|
pub(crate) fn new(io: Io) -> Self {
|
||||||
Self {
|
Self {
|
||||||
io,
|
io,
|
||||||
sink,
|
|
||||||
_t: PhantomData,
|
_t: PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn codec(self, codec: Codec) -> ConnectResult<Io, (), Codec> {
|
pub fn codec(
|
||||||
|
self,
|
||||||
|
codec: Codec,
|
||||||
|
) -> ConnectResult<Io, (), Codec, Receiver<<Codec as Encoder>::Item>> {
|
||||||
ConnectResult {
|
ConnectResult {
|
||||||
state: (),
|
state: (),
|
||||||
sink: self.sink,
|
out: None,
|
||||||
framed: Framed::new(self.io, codec),
|
framed: Framed::new(self.io, codec),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[pin_project::pin_project]
|
#[pin_project::pin_project]
|
||||||
pub struct ConnectResult<Io, St, Codec: Encoder + Decoder> {
|
pub struct ConnectResult<Io, St, Codec: Encoder + Decoder, Out> {
|
||||||
pub(crate) state: St,
|
pub(crate) state: St,
|
||||||
|
pub(crate) out: Option<Out>,
|
||||||
pub(crate) framed: Framed<Io, Codec>,
|
pub(crate) framed: Framed<Io, Codec>,
|
||||||
pub(crate) sink: Sink<<Codec as Encoder>::Item>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Io, St, Codec: Encoder + Decoder> ConnectResult<Io, St, Codec> {
|
impl<Io, St, Codec: Encoder + Decoder, Out: Unpin> ConnectResult<Io, St, Codec, Out> {
|
||||||
#[inline]
|
|
||||||
pub fn sink(&self) -> &Sink<<Codec as Encoder>::Item> {
|
|
||||||
&self.sink
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn get_ref(&self) -> &Io {
|
pub fn get_ref(&self) -> &Io {
|
||||||
self.framed.get_ref()
|
self.framed.get_ref()
|
||||||
@ -61,17 +56,28 @@ impl<Io, St, Codec: Encoder + Decoder> ConnectResult<Io, St, Codec> {
|
|||||||
self.framed.get_mut()
|
self.framed.get_mut()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn out<U>(self, out: U) -> ConnectResult<Io, St, Codec, U>
|
||||||
|
where
|
||||||
|
U: Stream<Item = <Codec as Encoder>::Item> + Unpin,
|
||||||
|
{
|
||||||
|
ConnectResult {
|
||||||
|
state: self.state,
|
||||||
|
framed: self.framed,
|
||||||
|
out: Some(out),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn state<S>(self, state: S) -> ConnectResult<Io, S, Codec> {
|
pub fn state<S>(self, state: S) -> ConnectResult<Io, S, Codec, Out> {
|
||||||
ConnectResult {
|
ConnectResult {
|
||||||
state,
|
state,
|
||||||
framed: self.framed,
|
framed: self.framed,
|
||||||
sink: self.sink,
|
out: self.out,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Io, St, Codec> Stream for ConnectResult<Io, St, Codec>
|
impl<Io, St, Codec, Out> Stream for ConnectResult<Io, St, Codec, Out>
|
||||||
where
|
where
|
||||||
Io: AsyncRead + AsyncWrite,
|
Io: AsyncRead + AsyncWrite,
|
||||||
Codec: Encoder + Decoder,
|
Codec: Encoder + Decoder,
|
||||||
@ -83,7 +89,8 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Io, St, Codec> futures::Sink<<Codec as Encoder>::Item> for ConnectResult<Io, St, Codec>
|
impl<Io, St, Codec, Out> futures::Sink<<Codec as Encoder>::Item>
|
||||||
|
for ConnectResult<Io, St, Codec, Out>
|
||||||
where
|
where
|
||||||
Io: AsyncRead + AsyncWrite,
|
Io: AsyncRead + AsyncWrite,
|
||||||
Codec: Encoder + Decoder,
|
Codec: Encoder + Decoder,
|
||||||
|
@ -1,81 +1,56 @@
|
|||||||
//! Framed dispatcher service and related utilities
|
//! Framed dispatcher service and related utilities
|
||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
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::Service;
|
||||||
use actix_utils::{mpsc, oneshot};
|
use actix_utils::mpsc;
|
||||||
use futures::{FutureExt, Stream};
|
use futures::Stream;
|
||||||
use log::debug;
|
use log::debug;
|
||||||
|
|
||||||
use crate::error::ServiceError;
|
use crate::error::ServiceError;
|
||||||
use crate::item::Item;
|
|
||||||
use crate::sink::Sink;
|
|
||||||
|
|
||||||
type Request<S, U> = Item<S, U>;
|
type Request<U> = <U as Decoder>::Item;
|
||||||
type Response<U> = <U as Encoder>::Item;
|
type Response<U> = <U as Encoder>::Item;
|
||||||
|
|
||||||
pub(crate) enum Message<T> {
|
|
||||||
Item(T),
|
|
||||||
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]
|
pub(crate) struct Dispatcher<S, T, U, Out>
|
||||||
pub(crate) struct Dispatcher<St, S, T, U>
|
|
||||||
where
|
where
|
||||||
St: Clone,
|
S: Service<Request = Request<U>, Response = Option<Response<U>>>,
|
||||||
S: Service<Request = Request<St, U>, Response = Option<Response<U>>>,
|
|
||||||
S::Error: 'static,
|
S::Error: 'static,
|
||||||
S::Future: 'static,
|
S::Future: 'static,
|
||||||
T: AsyncRead + AsyncWrite,
|
T: AsyncRead + AsyncWrite,
|
||||||
U: Encoder + Decoder,
|
U: Encoder + Decoder,
|
||||||
<U as Encoder>::Item: 'static,
|
<U as Encoder>::Item: 'static,
|
||||||
<U as Encoder>::Error: std::fmt::Debug,
|
<U as Encoder>::Error: std::fmt::Debug,
|
||||||
|
Out: Stream<Item = <U as Encoder>::Item> + Unpin,
|
||||||
{
|
{
|
||||||
service: S,
|
service: S,
|
||||||
sink: Sink<<U as Encoder>::Item>,
|
sink: Option<Out>,
|
||||||
state: St,
|
state: FramedState<S, U>,
|
||||||
dispatch_state: FramedState<S, U>,
|
|
||||||
framed: Framed<T, U>,
|
framed: Framed<T, U>,
|
||||||
rx: mpsc::Receiver<Result<Message<<U as Encoder>::Item>, S::Error>>,
|
rx: mpsc::Receiver<Result<<U as Encoder>::Item, S::Error>>,
|
||||||
tx: mpsc::Sender<Result<Message<<U as Encoder>::Item>, S::Error>>,
|
|
||||||
disconnect: Option<Rc<dyn Fn(St, bool)>>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<St, S, T, U> Dispatcher<St, S, T, U>
|
impl<S, T, U, Out> Dispatcher<S, T, U, Out>
|
||||||
where
|
where
|
||||||
St: Clone,
|
S: Service<Request = Request<U>, Response = Option<Response<U>>>,
|
||||||
S: Service<Request = Request<St, U>, Response = Option<Response<U>>>,
|
|
||||||
S::Error: 'static,
|
S::Error: 'static,
|
||||||
S::Future: 'static,
|
S::Future: 'static,
|
||||||
T: AsyncRead + AsyncWrite,
|
T: AsyncRead + AsyncWrite,
|
||||||
U: Decoder + Encoder,
|
U: Decoder + Encoder,
|
||||||
<U as Encoder>::Item: 'static,
|
<U as Encoder>::Item: 'static,
|
||||||
<U as Encoder>::Error: std::fmt::Debug,
|
<U as Encoder>::Error: std::fmt::Debug,
|
||||||
|
Out: Stream<Item = <U as Encoder>::Item> + Unpin,
|
||||||
{
|
{
|
||||||
pub(crate) fn new<F: IntoService<S>>(
|
pub(crate) fn new(framed: Framed<T, U>, service: S, sink: Option<Out>) -> Self {
|
||||||
framed: Framed<T, U>,
|
|
||||||
state: St,
|
|
||||||
service: F,
|
|
||||||
sink: Sink<<U as Encoder>::Item>,
|
|
||||||
rx: mpsc::Receiver<Result<Message<<U as Encoder>::Item>, S::Error>>,
|
|
||||||
disconnect: Option<Rc<dyn Fn(St, bool)>>,
|
|
||||||
) -> Self {
|
|
||||||
let tx = rx.sender();
|
|
||||||
|
|
||||||
Dispatcher {
|
Dispatcher {
|
||||||
framed,
|
|
||||||
state,
|
|
||||||
sink,
|
sink,
|
||||||
disconnect,
|
service,
|
||||||
rx,
|
framed,
|
||||||
tx,
|
rx: mpsc::channel().1,
|
||||||
service: service.into_service(),
|
state: FramedState::Processing,
|
||||||
dispatch_state: FramedState::Processing,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -84,33 +59,11 @@ enum FramedState<S: Service, U: Encoder + Decoder> {
|
|||||||
Processing,
|
Processing,
|
||||||
Error(ServiceError<S::Error, U>),
|
Error(ServiceError<S::Error, U>),
|
||||||
FramedError(ServiceError<S::Error, U>),
|
FramedError(ServiceError<S::Error, U>),
|
||||||
FlushAndStop(Vec<oneshot::Sender<()>>),
|
FlushAndStop,
|
||||||
Stopping,
|
Stopping,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S: Service, U: Encoder + Decoder> FramedState<S, U> {
|
impl<S: Service, U: Encoder + Decoder> FramedState<S, U> {
|
||||||
fn stop(&mut self, tx: Option<oneshot::Sender<()>>) {
|
|
||||||
match self {
|
|
||||||
FramedState::FlushAndStop(ref mut vec) => {
|
|
||||||
if let Some(tx) = tx {
|
|
||||||
vec.push(tx)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
FramedState::Processing => {
|
|
||||||
*self = FramedState::FlushAndStop(if let Some(tx) = tx {
|
|
||||||
vec![tx]
|
|
||||||
} else {
|
|
||||||
Vec::new()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
FramedState::Error(_) | FramedState::FramedError(_) | FramedState::Stopping => {
|
|
||||||
if let Some(tx) = tx {
|
|
||||||
let _ = tx.send(());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn take_error(&mut self) -> ServiceError<S::Error, U> {
|
fn take_error(&mut self) -> ServiceError<S::Error, U> {
|
||||||
match std::mem::replace(self, FramedState::Processing) {
|
match std::mem::replace(self, FramedState::Processing) {
|
||||||
FramedState::Error(err) => err,
|
FramedState::Error(err) => err,
|
||||||
@ -126,16 +79,16 @@ impl<S: Service, U: Encoder + Decoder> FramedState<S, U> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<St, S, T, U> Dispatcher<St, S, T, U>
|
impl<S, T, U, Out> Dispatcher<S, T, U, Out>
|
||||||
where
|
where
|
||||||
St: Clone,
|
S: Service<Request = Request<U>, Response = Option<Response<U>>>,
|
||||||
S: Service<Request = Request<St, U>, Response = Option<Response<U>>>,
|
|
||||||
S::Error: 'static,
|
S::Error: 'static,
|
||||||
S::Future: 'static,
|
S::Future: 'static,
|
||||||
T: AsyncRead + AsyncWrite,
|
T: AsyncRead + AsyncWrite,
|
||||||
U: Decoder + Encoder,
|
U: Decoder + Encoder,
|
||||||
<U as Encoder>::Item: 'static,
|
<U as Encoder>::Item: 'static,
|
||||||
<U as Encoder>::Error: std::fmt::Debug,
|
<U as Encoder>::Error: std::fmt::Debug,
|
||||||
|
Out: Stream<Item = <U as Encoder>::Item> + Unpin,
|
||||||
{
|
{
|
||||||
fn poll_read(&mut self, cx: &mut Context<'_>) -> bool {
|
fn poll_read(&mut self, cx: &mut Context<'_>) -> bool {
|
||||||
loop {
|
loop {
|
||||||
@ -144,35 +97,32 @@ where
|
|||||||
let item = match self.framed.next_item(cx) {
|
let item = match self.framed.next_item(cx) {
|
||||||
Poll::Ready(Some(Ok(el))) => el,
|
Poll::Ready(Some(Ok(el))) => el,
|
||||||
Poll::Ready(Some(Err(err))) => {
|
Poll::Ready(Some(Err(err))) => {
|
||||||
self.dispatch_state =
|
self.state = FramedState::FramedError(ServiceError::Decoder(err));
|
||||||
FramedState::FramedError(ServiceError::Decoder(err));
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
Poll::Pending => return false,
|
Poll::Pending => return false,
|
||||||
Poll::Ready(None) => {
|
Poll::Ready(None) => {
|
||||||
log::trace!("Client disconnected");
|
log::trace!("Client disconnected");
|
||||||
self.dispatch_state = FramedState::Stopping;
|
self.state = FramedState::Stopping;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let tx = self.tx.clone();
|
let tx = self.rx.sender();
|
||||||
actix_rt::spawn(
|
let fut = self.service.call(item);
|
||||||
self.service
|
actix_rt::spawn(async move {
|
||||||
.call(Item::new(self.state.clone(), self.sink.clone(), item))
|
let item = fut.await;
|
||||||
.map(move |item| {
|
let item = match item {
|
||||||
let item = match item {
|
Ok(Some(item)) => Ok(item),
|
||||||
Ok(Some(item)) => Ok(Message::Item(item)),
|
Ok(None) => return,
|
||||||
Ok(None) => return,
|
Err(err) => Err(err),
|
||||||
Err(err) => Err(err),
|
};
|
||||||
};
|
let _ = tx.send(item);
|
||||||
let _ = tx.send(item);
|
});
|
||||||
}),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
Poll::Pending => return false,
|
Poll::Pending => return false,
|
||||||
Poll::Ready(Err(err)) => {
|
Poll::Ready(Err(err)) => {
|
||||||
self.dispatch_state = FramedState::Error(ServiceError::Service(err));
|
self.state = FramedState::Error(ServiceError::Service(err));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -184,27 +134,39 @@ where
|
|||||||
loop {
|
loop {
|
||||||
while !self.framed.is_write_buf_full() {
|
while !self.framed.is_write_buf_full() {
|
||||||
match Pin::new(&mut self.rx).poll_next(cx) {
|
match Pin::new(&mut self.rx).poll_next(cx) {
|
||||||
Poll::Ready(Some(Ok(Message::Item(msg)))) => {
|
Poll::Ready(Some(Ok(msg))) => {
|
||||||
if let Err(err) = self.framed.write(msg) {
|
if let Err(err) = self.framed.write(msg) {
|
||||||
self.dispatch_state =
|
self.state = FramedState::FramedError(ServiceError::Encoder(err));
|
||||||
FramedState::FramedError(ServiceError::Encoder(err));
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
continue;
|
||||||
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))) => {
|
Poll::Ready(Some(Err(err))) => {
|
||||||
self.dispatch_state = FramedState::Error(ServiceError::Service(err));
|
self.state = FramedState::Error(ServiceError::Service(err));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
Poll::Ready(None) | Poll::Pending => break,
|
Poll::Ready(None) | Poll::Pending => (),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if self.sink.is_some() {
|
||||||
|
match Pin::new(self.sink.as_mut().unwrap()).poll_next(cx) {
|
||||||
|
Poll::Ready(Some(msg)) => {
|
||||||
|
if let Err(err) = self.framed.write(msg) {
|
||||||
|
self.state =
|
||||||
|
FramedState::FramedError(ServiceError::Encoder(err));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
Poll::Ready(None) => {
|
||||||
|
let _ = self.sink.take();
|
||||||
|
self.state = FramedState::FlushAndStop;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
Poll::Pending => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if !self.framed.is_write_buf_empty() {
|
if !self.framed.is_write_buf_empty() {
|
||||||
@ -213,8 +175,7 @@ where
|
|||||||
Poll::Ready(Ok(_)) => (),
|
Poll::Ready(Ok(_)) => (),
|
||||||
Poll::Ready(Err(err)) => {
|
Poll::Ready(Err(err)) => {
|
||||||
debug!("Error sending data: {:?}", err);
|
debug!("Error sending data: {:?}", err);
|
||||||
self.dispatch_state =
|
self.state = FramedState::FramedError(ServiceError::Encoder(err));
|
||||||
FramedState::FramedError(ServiceError::Encoder(err));
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -229,14 +190,16 @@ where
|
|||||||
&mut self,
|
&mut self,
|
||||||
cx: &mut Context<'_>,
|
cx: &mut Context<'_>,
|
||||||
) -> Poll<Result<(), ServiceError<S::Error, U>>> {
|
) -> Poll<Result<(), ServiceError<S::Error, U>>> {
|
||||||
match self.dispatch_state {
|
match self.state {
|
||||||
FramedState::Processing => {
|
FramedState::Processing => loop {
|
||||||
if self.poll_read(cx) || self.poll_write(cx) {
|
let read = self.poll_read(cx);
|
||||||
self.poll(cx)
|
let write = self.poll_write(cx);
|
||||||
|
if read || write {
|
||||||
|
continue;
|
||||||
} else {
|
} else {
|
||||||
Poll::Pending
|
return Poll::Pending;
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
FramedState::Error(_) => {
|
FramedState::Error(_) => {
|
||||||
// flush write buffer
|
// flush write buffer
|
||||||
if !self.framed.is_write_buf_empty() {
|
if !self.framed.is_write_buf_empty() {
|
||||||
@ -244,12 +207,21 @@ where
|
|||||||
return Poll::Pending;
|
return Poll::Pending;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if let Some(ref disconnect) = self.disconnect {
|
Poll::Ready(Err(self.state.take_error()))
|
||||||
(&*disconnect)(self.state.clone(), true);
|
|
||||||
}
|
|
||||||
Poll::Ready(Err(self.dispatch_state.take_error()))
|
|
||||||
}
|
}
|
||||||
FramedState::FlushAndStop(ref mut vec) => {
|
FramedState::FlushAndStop => {
|
||||||
|
// drain service responses
|
||||||
|
match Pin::new(&mut self.rx).poll_next(cx) {
|
||||||
|
Poll::Ready(Some(Ok(msg))) => {
|
||||||
|
if let Err(_) = self.framed.write(msg) {
|
||||||
|
return Poll::Ready(Ok(()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Poll::Ready(Some(Err(_))) => return Poll::Ready(Ok(())),
|
||||||
|
Poll::Ready(None) | Poll::Pending => (),
|
||||||
|
}
|
||||||
|
|
||||||
|
// flush io
|
||||||
if !self.framed.is_write_buf_empty() {
|
if !self.framed.is_write_buf_empty() {
|
||||||
match self.framed.flush(cx) {
|
match self.framed.flush(cx) {
|
||||||
Poll::Ready(Err(err)) => {
|
Poll::Ready(Err(err)) => {
|
||||||
@ -261,26 +233,10 @@ where
|
|||||||
Poll::Ready(_) => (),
|
Poll::Ready(_) => (),
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
for tx in vec.drain(..) {
|
|
||||||
let _ = tx.send(());
|
|
||||||
}
|
|
||||||
if let Some(ref disconnect) = self.disconnect {
|
|
||||||
(&*disconnect)(self.state.clone(), false);
|
|
||||||
}
|
|
||||||
Poll::Ready(Ok(()))
|
|
||||||
}
|
|
||||||
FramedState::FramedError(_) => {
|
|
||||||
if let Some(ref disconnect) = self.disconnect {
|
|
||||||
(&*disconnect)(self.state.clone(), true);
|
|
||||||
}
|
|
||||||
Poll::Ready(Err(self.dispatch_state.take_framed_error()))
|
|
||||||
}
|
|
||||||
FramedState::Stopping => {
|
|
||||||
if let Some(ref disconnect) = self.disconnect {
|
|
||||||
(&*disconnect)(self.state.clone(), false);
|
|
||||||
}
|
|
||||||
Poll::Ready(Ok(()))
|
Poll::Ready(Ok(()))
|
||||||
}
|
}
|
||||||
|
FramedState::FramedError(_) => Poll::Ready(Err(self.state.take_framed_error())),
|
||||||
|
FramedState::Stopping => Poll::Ready(Ok(())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,82 +0,0 @@
|
|||||||
use std::fmt;
|
|
||||||
use std::ops::{Deref, DerefMut};
|
|
||||||
|
|
||||||
use actix_codec::{Decoder, Encoder};
|
|
||||||
|
|
||||||
use crate::sink::Sink;
|
|
||||||
|
|
||||||
pub struct Item<St, Codec: Encoder + Decoder> {
|
|
||||||
state: St,
|
|
||||||
sink: Sink<<Codec as Encoder>::Item>,
|
|
||||||
item: <Codec as Decoder>::Item,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<St, Codec> Item<St, Codec>
|
|
||||||
where
|
|
||||||
Codec: Encoder + Decoder,
|
|
||||||
{
|
|
||||||
pub(crate) fn new(
|
|
||||||
state: St,
|
|
||||||
sink: Sink<<Codec as Encoder>::Item>,
|
|
||||||
item: <Codec as Decoder>::Item,
|
|
||||||
) -> Self {
|
|
||||||
Item { state, sink, item }
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
pub fn state(&self) -> &St {
|
|
||||||
&self.state
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
pub fn state_mut(&mut self) -> &mut St {
|
|
||||||
&mut self.state
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
pub fn sink(&self) -> &Sink<<Codec as Encoder>::Item> {
|
|
||||||
&self.sink
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
pub fn into_inner(self) -> <Codec as Decoder>::Item {
|
|
||||||
self.item
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
pub fn into_parts(self) -> (St, Sink<<Codec as Encoder>::Item>, <Codec as Decoder>::Item) {
|
|
||||||
(self.state, self.sink, self.item)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<St, Codec> Deref for Item<St, Codec>
|
|
||||||
where
|
|
||||||
Codec: Encoder + Decoder,
|
|
||||||
{
|
|
||||||
type Target = <Codec as Decoder>::Item;
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
fn deref(&self) -> &<Codec as Decoder>::Item {
|
|
||||||
&self.item
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<St, Codec> DerefMut for Item<St, Codec>
|
|
||||||
where
|
|
||||||
Codec: Encoder + Decoder,
|
|
||||||
{
|
|
||||||
#[inline]
|
|
||||||
fn deref_mut(&mut self) -> &mut <Codec as Decoder>::Item {
|
|
||||||
&mut self.item
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<St, Codec> fmt::Debug for Item<St, Codec>
|
|
||||||
where
|
|
||||||
Codec: Encoder + Decoder,
|
|
||||||
<Codec as Decoder>::Item: fmt::Debug,
|
|
||||||
{
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
||||||
f.debug_tuple("Item").field(&self.item).finish()
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,15 +1,11 @@
|
|||||||
#![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 connect;
|
mod connect;
|
||||||
mod dispatcher;
|
mod dispatcher;
|
||||||
mod error;
|
mod error;
|
||||||
mod item;
|
|
||||||
mod service;
|
mod service;
|
||||||
mod sink;
|
|
||||||
|
|
||||||
pub use self::connect::{Connect, ConnectResult};
|
pub use self::connect::{Connect, ConnectResult};
|
||||||
pub use self::error::ServiceError;
|
pub use self::error::ServiceError;
|
||||||
pub use self::item::Item;
|
pub use self::service::{Builder, FactoryBuilder};
|
||||||
pub use self::service::{Builder, NewServiceBuilder, ServiceBuilder};
|
|
||||||
pub use self::sink::Sink;
|
|
||||||
|
@ -4,108 +4,57 @@ 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};
|
use actix_codec::{AsyncRead, AsyncWrite, Decoder, Encoder, Framed};
|
||||||
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::{ready, Stream};
|
||||||
use pin_project::project;
|
use pin_project::project;
|
||||||
|
|
||||||
use crate::connect::{Connect, ConnectResult};
|
use crate::connect::{Connect, ConnectResult};
|
||||||
use crate::dispatcher::{Dispatcher, Message};
|
use crate::dispatcher::Dispatcher;
|
||||||
use crate::error::ServiceError;
|
use crate::error::ServiceError;
|
||||||
use crate::item::Item;
|
|
||||||
use crate::sink::Sink;
|
|
||||||
|
|
||||||
type RequestItem<S, U> = Item<S, U>;
|
type RequestItem<U> = <U as Decoder>::Item;
|
||||||
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.
|
||||||
pub struct Builder<St, Codec>(PhantomData<(St, Codec)>);
|
pub struct Builder<St, C, Io, Codec, Out> {
|
||||||
|
|
||||||
impl<St: Clone, Codec> Default for Builder<St, Codec> {
|
|
||||||
fn default() -> Builder<St, Codec> {
|
|
||||||
Builder::new()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<St: Clone, Codec> Builder<St, Codec> {
|
|
||||||
pub fn new() -> Builder<St, Codec> {
|
|
||||||
Builder(PhantomData)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Construct framed handler service with specified connect service
|
|
||||||
pub fn service<Io, C, F>(self, connect: F) -> ServiceBuilder<St, C, Io, Codec>
|
|
||||||
where
|
|
||||||
F: IntoService<C>,
|
|
||||||
Io: AsyncRead + AsyncWrite,
|
|
||||||
C: Service<Request = Connect<Io, Codec>, Response = ConnectResult<Io, St, Codec>>,
|
|
||||||
Codec: Decoder + Encoder,
|
|
||||||
{
|
|
||||||
ServiceBuilder {
|
|
||||||
connect: connect.into_service(),
|
|
||||||
disconnect: None,
|
|
||||||
_t: PhantomData,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Construct framed handler new service with specified connect service
|
|
||||||
pub fn factory<Io, C, F>(self, connect: F) -> NewServiceBuilder<St, C, Io, Codec>
|
|
||||||
where
|
|
||||||
F: IntoServiceFactory<C>,
|
|
||||||
Io: AsyncRead + AsyncWrite,
|
|
||||||
C: ServiceFactory<
|
|
||||||
Config = (),
|
|
||||||
Request = Connect<Io, Codec>,
|
|
||||||
Response = ConnectResult<Io, St, Codec>,
|
|
||||||
>,
|
|
||||||
C::Error: 'static,
|
|
||||||
C::Future: 'static,
|
|
||||||
Codec: Decoder + Encoder,
|
|
||||||
{
|
|
||||||
NewServiceBuilder {
|
|
||||||
connect: connect.into_factory(),
|
|
||||||
disconnect: None,
|
|
||||||
_t: PhantomData,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct ServiceBuilder<St, C, Io, Codec> {
|
|
||||||
connect: C,
|
connect: C,
|
||||||
disconnect: Option<Rc<dyn Fn(St, bool)>>,
|
_t: PhantomData<(St, Io, Codec, Out)>,
|
||||||
_t: PhantomData<(St, Io, Codec)>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<St, C, Io, Codec> ServiceBuilder<St, C, Io, Codec>
|
impl<St, C, Io, Codec, Out> Builder<St, C, Io, Codec, Out>
|
||||||
where
|
where
|
||||||
St: Clone,
|
C: Service<Request = Connect<Io, Codec>, Response = ConnectResult<Io, St, Codec, Out>>,
|
||||||
C: Service<Request = Connect<Io, Codec>, Response = ConnectResult<Io, St, Codec>>,
|
|
||||||
Io: AsyncRead + AsyncWrite,
|
Io: AsyncRead + AsyncWrite,
|
||||||
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,
|
||||||
|
Out: Stream<Item = <Codec as Encoder>::Item> + Unpin,
|
||||||
{
|
{
|
||||||
/// Callback to execute on disconnect
|
/// Construct framed handler service with specified connect service
|
||||||
///
|
pub fn new<F>(connect: F) -> Builder<St, C, Io, Codec, Out>
|
||||||
/// Second parameter indicates error occured during disconnect.
|
|
||||||
pub fn disconnect<F, Out>(mut self, disconnect: F) -> Self
|
|
||||||
where
|
where
|
||||||
F: Fn(St, bool) + 'static,
|
F: IntoService<C>,
|
||||||
|
Io: AsyncRead + AsyncWrite,
|
||||||
|
C: Service<Request = Connect<Io, Codec>, Response = ConnectResult<Io, St, Codec, Out>>,
|
||||||
|
Codec: Decoder + Encoder,
|
||||||
|
Out: Stream<Item = <Codec as Encoder>::Item>,
|
||||||
{
|
{
|
||||||
self.disconnect = Some(Rc::new(disconnect));
|
Builder {
|
||||||
self
|
connect: connect.into_service(),
|
||||||
|
_t: PhantomData,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 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 build<F, T>(self, service: F) -> FramedServiceImpl<St, C, T, Io, Codec, Out>
|
||||||
where
|
where
|
||||||
F: IntoServiceFactory<T>,
|
F: IntoServiceFactory<T>,
|
||||||
T: ServiceFactory<
|
T: ServiceFactory<
|
||||||
Config = St,
|
Config = St,
|
||||||
Request = RequestItem<St, Codec>,
|
Request = RequestItem<Codec>,
|
||||||
Response = ResponseItem<Codec>,
|
Response = ResponseItem<Codec>,
|
||||||
Error = C::Error,
|
Error = C::Error,
|
||||||
InitError = C::Error,
|
InitError = C::Error,
|
||||||
@ -114,211 +63,261 @@ where
|
|||||||
FramedServiceImpl {
|
FramedServiceImpl {
|
||||||
connect: self.connect,
|
connect: self.connect,
|
||||||
handler: Rc::new(service.into_factory()),
|
handler: Rc::new(service.into_factory()),
|
||||||
disconnect: self.disconnect.clone(),
|
|
||||||
_t: PhantomData,
|
_t: PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct NewServiceBuilder<St, C, Io, Codec> {
|
/// Service builder - structure that follows the builder pattern
|
||||||
|
/// for building instances for framed services.
|
||||||
|
pub struct FactoryBuilder<St, C, Io, Codec, Out> {
|
||||||
connect: C,
|
connect: C,
|
||||||
disconnect: Option<Rc<dyn Fn(St, bool)>>,
|
_t: PhantomData<(St, Io, Codec, Out)>,
|
||||||
_t: PhantomData<(St, Io, Codec)>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<St, C, Io, Codec> NewServiceBuilder<St, C, Io, Codec>
|
impl<St, C, Io, Codec, Out> FactoryBuilder<St, C, Io, Codec, Out>
|
||||||
where
|
where
|
||||||
St: Clone,
|
|
||||||
Io: AsyncRead + AsyncWrite,
|
Io: AsyncRead + AsyncWrite,
|
||||||
C: ServiceFactory<
|
C: ServiceFactory<
|
||||||
Config = (),
|
Config = (),
|
||||||
Request = Connect<Io, Codec>,
|
Request = Connect<Io, Codec>,
|
||||||
Response = ConnectResult<Io, St, Codec>,
|
Response = ConnectResult<Io, St, Codec, Out>,
|
||||||
>,
|
>,
|
||||||
C::Error: 'static,
|
|
||||||
C::Future: 'static,
|
|
||||||
Codec: Decoder + Encoder,
|
Codec: Decoder + Encoder,
|
||||||
<Codec as Encoder>::Item: 'static,
|
|
||||||
<Codec as Encoder>::Error: std::fmt::Debug,
|
<Codec as Encoder>::Error: std::fmt::Debug,
|
||||||
|
Out: Stream<Item = <Codec as Encoder>::Item> + Unpin,
|
||||||
{
|
{
|
||||||
/// Callback to execute on disconnect
|
/// Construct framed handler new service with specified connect service
|
||||||
///
|
pub fn new<F>(connect: F) -> FactoryBuilder<St, C, Io, Codec, Out>
|
||||||
/// Second parameter indicates error occured during disconnect.
|
|
||||||
pub fn disconnect<F>(mut self, disconnect: F) -> Self
|
|
||||||
where
|
where
|
||||||
F: Fn(St, bool) + 'static,
|
F: IntoServiceFactory<C>,
|
||||||
|
Io: AsyncRead + AsyncWrite,
|
||||||
|
C: ServiceFactory<
|
||||||
|
Config = (),
|
||||||
|
Request = Connect<Io, Codec>,
|
||||||
|
Response = ConnectResult<Io, St, Codec, Out>,
|
||||||
|
>,
|
||||||
|
Codec: Decoder + Encoder,
|
||||||
|
Out: Stream<Item = <Codec as Encoder>::Item> + Unpin,
|
||||||
{
|
{
|
||||||
self.disconnect = Some(Rc::new(disconnect));
|
FactoryBuilder {
|
||||||
self
|
connect: connect.into_factory(),
|
||||||
|
_t: PhantomData,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn finish<F, T, Cfg>(self, service: F) -> FramedService<St, C, T, Io, Codec, Cfg>
|
pub fn build<F, T, Cfg>(self, service: F) -> FramedService<St, C, T, Io, Codec, Out, Cfg>
|
||||||
where
|
where
|
||||||
F: IntoServiceFactory<T>,
|
F: IntoServiceFactory<T>,
|
||||||
T: ServiceFactory<
|
T: ServiceFactory<
|
||||||
Config = St,
|
Config = St,
|
||||||
Request = RequestItem<St, Codec>,
|
Request = RequestItem<Codec>,
|
||||||
Response = ResponseItem<Codec>,
|
Response = ResponseItem<Codec>,
|
||||||
Error = C::Error,
|
Error = C::Error,
|
||||||
InitError = C::Error,
|
InitError = C::Error,
|
||||||
> + 'static,
|
>,
|
||||||
{
|
{
|
||||||
FramedService {
|
FramedService {
|
||||||
connect: self.connect,
|
connect: self.connect,
|
||||||
handler: Rc::new(service.into_factory()),
|
handler: Rc::new(service.into_factory()),
|
||||||
disconnect: self.disconnect,
|
|
||||||
_t: PhantomData,
|
_t: PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct FramedService<St, C, T, Io, Codec, Cfg> {
|
pub struct FramedService<St, C, T, Io, Codec, Out, Cfg> {
|
||||||
connect: C,
|
connect: C,
|
||||||
handler: Rc<T>,
|
handler: Rc<T>,
|
||||||
disconnect: Option<Rc<dyn Fn(St, bool)>>,
|
_t: PhantomData<(St, Io, Codec, Out, Cfg)>,
|
||||||
_t: PhantomData<(St, Io, Codec, Cfg)>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<St, C, T, Io, Codec, Cfg> ServiceFactory for FramedService<St, C, T, Io, Codec, Cfg>
|
impl<St, C, T, Io, Codec, Out, Cfg> ServiceFactory
|
||||||
|
for FramedService<St, C, T, Io, Codec, Out, Cfg>
|
||||||
where
|
where
|
||||||
St: Clone + 'static,
|
|
||||||
Io: AsyncRead + AsyncWrite,
|
Io: AsyncRead + AsyncWrite,
|
||||||
C: ServiceFactory<
|
C: ServiceFactory<
|
||||||
Config = (),
|
Config = (),
|
||||||
Request = Connect<Io, Codec>,
|
Request = Connect<Io, Codec>,
|
||||||
Response = ConnectResult<Io, St, Codec>,
|
Response = ConnectResult<Io, St, Codec, Out>,
|
||||||
>,
|
>,
|
||||||
C::Error: 'static,
|
|
||||||
C::Future: 'static,
|
|
||||||
T: ServiceFactory<
|
T: ServiceFactory<
|
||||||
Config = St,
|
Config = St,
|
||||||
Request = RequestItem<St, Codec>,
|
Request = RequestItem<Codec>,
|
||||||
Response = ResponseItem<Codec>,
|
Response = ResponseItem<Codec>,
|
||||||
Error = C::Error,
|
Error = C::Error,
|
||||||
InitError = C::Error,
|
InitError = C::Error,
|
||||||
> + 'static,
|
>,
|
||||||
|
<T::Service as Service>::Error: 'static,
|
||||||
<T::Service as Service>::Future: 'static,
|
<T::Service as Service>::Future: '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,
|
||||||
|
Out: Stream<Item = <Codec as Encoder>::Item> + Unpin,
|
||||||
{
|
{
|
||||||
type Config = Cfg;
|
type Config = Cfg;
|
||||||
type Request = Io;
|
type Request = Io;
|
||||||
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, Out>;
|
||||||
type Future = LocalBoxFuture<'static, Result<Self::Service, Self::InitError>>;
|
type Future = FramedServiceResponse<St, C, T, Io, Codec, Out>;
|
||||||
|
|
||||||
fn new_service(&self, _: Cfg) -> Self::Future {
|
fn new_service(&self, _: Cfg) -> Self::Future {
|
||||||
let handler = self.handler.clone();
|
|
||||||
let disconnect = self.disconnect.clone();
|
|
||||||
|
|
||||||
// create connect service and then create service impl
|
// create connect service and then create service impl
|
||||||
self.connect
|
FramedServiceResponse {
|
||||||
.new_service(())
|
fut: self.connect.new_service(()),
|
||||||
.map(move |result| {
|
handler: self.handler.clone(),
|
||||||
result.map(move |connect| FramedServiceImpl {
|
}
|
||||||
connect,
|
|
||||||
handler,
|
|
||||||
disconnect,
|
|
||||||
_t: PhantomData,
|
|
||||||
})
|
|
||||||
})
|
|
||||||
.boxed_local()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct FramedServiceImpl<St, C, T, Io, Codec> {
|
#[pin_project::pin_project]
|
||||||
connect: C,
|
pub struct FramedServiceResponse<St, C, T, Io, Codec, Out>
|
||||||
handler: Rc<T>,
|
|
||||||
disconnect: Option<Rc<dyn Fn(St, bool)>>,
|
|
||||||
_t: PhantomData<(St, Io, Codec)>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<St, C, T, Io, Codec> Service for FramedServiceImpl<St, C, T, Io, Codec>
|
|
||||||
where
|
where
|
||||||
St: Clone,
|
|
||||||
Io: AsyncRead + AsyncWrite,
|
Io: AsyncRead + AsyncWrite,
|
||||||
C: Service<Request = Connect<Io, Codec>, Response = ConnectResult<Io, St, Codec>>,
|
C: ServiceFactory<
|
||||||
C::Error: 'static,
|
Config = (),
|
||||||
|
Request = Connect<Io, Codec>,
|
||||||
|
Response = ConnectResult<Io, St, Codec, Out>,
|
||||||
|
>,
|
||||||
T: ServiceFactory<
|
T: ServiceFactory<
|
||||||
Config = St,
|
Config = St,
|
||||||
Request = RequestItem<St, Codec>,
|
Request = RequestItem<Codec>,
|
||||||
Response = ResponseItem<Codec>,
|
Response = ResponseItem<Codec>,
|
||||||
Error = C::Error,
|
Error = C::Error,
|
||||||
InitError = C::Error,
|
InitError = C::Error,
|
||||||
>,
|
>,
|
||||||
|
<T::Service as Service>::Error: 'static,
|
||||||
<T::Service as Service>::Future: 'static,
|
<T::Service as Service>::Future: '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,
|
||||||
|
Out: Stream<Item = <Codec as Encoder>::Item> + Unpin,
|
||||||
|
{
|
||||||
|
#[pin]
|
||||||
|
fut: C::Future,
|
||||||
|
handler: Rc<T>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<St, C, T, Io, Codec, Out> Future for FramedServiceResponse<St, C, T, Io, Codec, Out>
|
||||||
|
where
|
||||||
|
Io: AsyncRead + AsyncWrite,
|
||||||
|
C: ServiceFactory<
|
||||||
|
Config = (),
|
||||||
|
Request = Connect<Io, Codec>,
|
||||||
|
Response = ConnectResult<Io, St, Codec, Out>,
|
||||||
|
>,
|
||||||
|
T: ServiceFactory<
|
||||||
|
Config = St,
|
||||||
|
Request = RequestItem<Codec>,
|
||||||
|
Response = ResponseItem<Codec>,
|
||||||
|
Error = C::Error,
|
||||||
|
InitError = C::Error,
|
||||||
|
>,
|
||||||
|
<T::Service as Service>::Error: 'static,
|
||||||
|
<T::Service as Service>::Future: 'static,
|
||||||
|
Codec: Decoder + Encoder,
|
||||||
|
<Codec as Encoder>::Item: 'static,
|
||||||
|
<Codec as Encoder>::Error: std::fmt::Debug,
|
||||||
|
Out: Stream<Item = <Codec as Encoder>::Item> + Unpin,
|
||||||
|
{
|
||||||
|
type Output = Result<FramedServiceImpl<St, C::Service, T, Io, Codec, Out>, C::InitError>;
|
||||||
|
|
||||||
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
|
let this = self.project();
|
||||||
|
let connect = ready!(this.fut.poll(cx))?;
|
||||||
|
|
||||||
|
Poll::Ready(Ok(FramedServiceImpl {
|
||||||
|
connect,
|
||||||
|
handler: this.handler.clone(),
|
||||||
|
_t: PhantomData,
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct FramedServiceImpl<St, C, T, Io, Codec, Out> {
|
||||||
|
connect: C,
|
||||||
|
handler: Rc<T>,
|
||||||
|
_t: PhantomData<(St, Io, Codec, Out)>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<St, C, T, Io, Codec, Out> Service for FramedServiceImpl<St, C, T, Io, Codec, Out>
|
||||||
|
where
|
||||||
|
Io: AsyncRead + AsyncWrite,
|
||||||
|
C: Service<Request = Connect<Io, Codec>, Response = ConnectResult<Io, St, Codec, Out>>,
|
||||||
|
T: ServiceFactory<
|
||||||
|
Config = St,
|
||||||
|
Request = RequestItem<Codec>,
|
||||||
|
Response = ResponseItem<Codec>,
|
||||||
|
Error = C::Error,
|
||||||
|
InitError = C::Error,
|
||||||
|
>,
|
||||||
|
<T::Service as Service>::Error: 'static,
|
||||||
|
<T::Service as Service>::Future: 'static,
|
||||||
|
Codec: Decoder + Encoder,
|
||||||
|
<Codec as Encoder>::Item: 'static,
|
||||||
|
<Codec as Encoder>::Error: std::fmt::Debug,
|
||||||
|
Out: Stream<Item = <Codec as Encoder>::Item> + Unpin,
|
||||||
{
|
{
|
||||||
type Request = Io;
|
type Request = Io;
|
||||||
type Response = ();
|
type Response = ();
|
||||||
type Error = ServiceError<C::Error, Codec>;
|
type Error = ServiceError<C::Error, Codec>;
|
||||||
type Future = FramedServiceImplResponse<St, Io, Codec, C, T>;
|
type Future = FramedServiceImplResponse<St, Io, Codec, Out, 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(Rc::new(move |msg| {
|
|
||||||
let _ = tx.send(Ok(msg));
|
|
||||||
}));
|
|
||||||
FramedServiceImplResponse {
|
FramedServiceImplResponse {
|
||||||
inner: FramedServiceImplResponseInner::Connect(
|
inner: FramedServiceImplResponseInner::Connect(
|
||||||
self.connect.call(Connect::new(req, sink.clone())),
|
self.connect.call(Connect::new(req)),
|
||||||
self.handler.clone(),
|
self.handler.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, Out, C, T>
|
||||||
where
|
where
|
||||||
St: Clone,
|
C: Service<Request = Connect<Io, Codec>, Response = ConnectResult<Io, St, Codec, Out>>,
|
||||||
C: Service<Request = Connect<Io, Codec>, Response = ConnectResult<Io, St, Codec>>,
|
|
||||||
C::Error: 'static,
|
|
||||||
T: ServiceFactory<
|
T: ServiceFactory<
|
||||||
Config = St,
|
Config = St,
|
||||||
Request = RequestItem<St, Codec>,
|
Request = RequestItem<Codec>,
|
||||||
Response = ResponseItem<Codec>,
|
Response = ResponseItem<Codec>,
|
||||||
Error = C::Error,
|
Error = C::Error,
|
||||||
InitError = C::Error,
|
InitError = C::Error,
|
||||||
>,
|
>,
|
||||||
|
<T::Service as Service>::Error: 'static,
|
||||||
<T::Service as Service>::Future: 'static,
|
<T::Service as Service>::Future: 'static,
|
||||||
Io: AsyncRead + AsyncWrite,
|
Io: AsyncRead + AsyncWrite,
|
||||||
Codec: Encoder + Decoder,
|
Codec: Encoder + Decoder,
|
||||||
<Codec as Encoder>::Item: 'static,
|
<Codec as Encoder>::Item: 'static,
|
||||||
<Codec as Encoder>::Error: std::fmt::Debug,
|
<Codec as Encoder>::Error: std::fmt::Debug,
|
||||||
|
Out: Stream<Item = <Codec as Encoder>::Item> + Unpin,
|
||||||
{
|
{
|
||||||
#[pin]
|
#[pin]
|
||||||
inner: FramedServiceImplResponseInner<St, Io, Codec, C, T>,
|
inner: FramedServiceImplResponseInner<St, Io, Codec, Out, C, T>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<St, Io, Codec, C, T> Future for FramedServiceImplResponse<St, Io, Codec, C, T>
|
impl<St, Io, Codec, Out, C, T> Future for FramedServiceImplResponse<St, Io, Codec, Out, C, T>
|
||||||
where
|
where
|
||||||
St: Clone,
|
C: Service<Request = Connect<Io, Codec>, Response = ConnectResult<Io, St, Codec, Out>>,
|
||||||
C: Service<Request = Connect<Io, Codec>, Response = ConnectResult<Io, St, Codec>>,
|
|
||||||
C::Error: 'static,
|
|
||||||
T: ServiceFactory<
|
T: ServiceFactory<
|
||||||
Config = St,
|
Config = St,
|
||||||
Request = RequestItem<St, Codec>,
|
Request = RequestItem<Codec>,
|
||||||
Response = ResponseItem<Codec>,
|
Response = ResponseItem<Codec>,
|
||||||
Error = C::Error,
|
Error = C::Error,
|
||||||
InitError = C::Error,
|
InitError = C::Error,
|
||||||
>,
|
>,
|
||||||
|
<T::Service as Service>::Error: 'static,
|
||||||
<T::Service as Service>::Future: 'static,
|
<T::Service as Service>::Future: 'static,
|
||||||
Io: AsyncRead + AsyncWrite,
|
Io: AsyncRead + AsyncWrite,
|
||||||
Codec: Encoder + Decoder,
|
Codec: Encoder + Decoder,
|
||||||
<Codec as Encoder>::Item: 'static,
|
<Codec as Encoder>::Item: 'static,
|
||||||
<Codec as Encoder>::Error: std::fmt::Debug,
|
<Codec as Encoder>::Error: std::fmt::Debug,
|
||||||
|
Out: Stream<Item = <Codec as Encoder>::Item> + Unpin,
|
||||||
{
|
{
|
||||||
type Output = Result<(), ServiceError<C::Error, Codec>>;
|
type Output = Result<(), ServiceError<C::Error, Codec>>;
|
||||||
|
|
||||||
@ -338,94 +337,71 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[pin_project::pin_project]
|
#[pin_project::pin_project]
|
||||||
enum FramedServiceImplResponseInner<St, Io, Codec, C, T>
|
enum FramedServiceImplResponseInner<St, Io, Codec, Out, C, T>
|
||||||
where
|
where
|
||||||
St: Clone,
|
C: Service<Request = Connect<Io, Codec>, Response = ConnectResult<Io, St, Codec, Out>>,
|
||||||
C: Service<Request = Connect<Io, Codec>, Response = ConnectResult<Io, St, Codec>>,
|
|
||||||
C::Error: 'static,
|
|
||||||
T: ServiceFactory<
|
T: ServiceFactory<
|
||||||
Config = St,
|
Config = St,
|
||||||
Request = RequestItem<St, Codec>,
|
Request = RequestItem<Codec>,
|
||||||
Response = ResponseItem<Codec>,
|
Response = ResponseItem<Codec>,
|
||||||
Error = C::Error,
|
Error = C::Error,
|
||||||
InitError = C::Error,
|
InitError = C::Error,
|
||||||
>,
|
>,
|
||||||
|
<T::Service as Service>::Error: 'static,
|
||||||
<T::Service as Service>::Future: 'static,
|
<T::Service as Service>::Future: 'static,
|
||||||
Io: AsyncRead + AsyncWrite,
|
Io: AsyncRead + AsyncWrite,
|
||||||
Codec: Encoder + Decoder,
|
Codec: Encoder + Decoder,
|
||||||
<Codec as Encoder>::Item: 'static,
|
<Codec as Encoder>::Item: 'static,
|
||||||
<Codec as Encoder>::Error: std::fmt::Debug,
|
<Codec as Encoder>::Error: std::fmt::Debug,
|
||||||
|
Out: Stream<Item = <Codec as Encoder>::Item> + Unpin,
|
||||||
{
|
{
|
||||||
Connect(
|
Connect(#[pin] C::Future, Rc<T>),
|
||||||
#[pin] C::Future,
|
Handler(#[pin] T::Future, Option<Framed<Io, Codec>>, Option<Out>),
|
||||||
Rc<T>,
|
Dispatcher(Dispatcher<T::Service, Io, Codec, Out>),
|
||||||
Option<Rc<dyn Fn(St, bool)>>,
|
|
||||||
Option<mpsc::Receiver<ServiceResult<Codec, C::Error>>>,
|
|
||||||
),
|
|
||||||
Handler(
|
|
||||||
#[pin] T::Future,
|
|
||||||
Option<ConnectResult<Io, St, Codec>>,
|
|
||||||
Option<Rc<dyn Fn(St, bool)>>,
|
|
||||||
Option<mpsc::Receiver<ServiceResult<Codec, C::Error>>>,
|
|
||||||
),
|
|
||||||
Dispatcher(Dispatcher<St, T::Service, Io, Codec>),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<St, Io, Codec, C, T> FramedServiceImplResponseInner<St, Io, Codec, C, T>
|
impl<St, Io, Codec, Out, C, T> FramedServiceImplResponseInner<St, Io, Codec, Out, C, T>
|
||||||
where
|
where
|
||||||
St: Clone,
|
C: Service<Request = Connect<Io, Codec>, Response = ConnectResult<Io, St, Codec, Out>>,
|
||||||
C: Service<Request = Connect<Io, Codec>, Response = ConnectResult<Io, St, Codec>>,
|
|
||||||
C::Error: 'static,
|
|
||||||
T: ServiceFactory<
|
T: ServiceFactory<
|
||||||
Config = St,
|
Config = St,
|
||||||
Request = RequestItem<St, Codec>,
|
Request = RequestItem<Codec>,
|
||||||
Response = ResponseItem<Codec>,
|
Response = ResponseItem<Codec>,
|
||||||
Error = C::Error,
|
Error = C::Error,
|
||||||
InitError = C::Error,
|
InitError = C::Error,
|
||||||
>,
|
>,
|
||||||
|
<T::Service as Service>::Error: 'static,
|
||||||
<T::Service as Service>::Future: 'static,
|
<T::Service as Service>::Future: 'static,
|
||||||
Io: AsyncRead + AsyncWrite,
|
Io: AsyncRead + AsyncWrite,
|
||||||
Codec: Encoder + Decoder,
|
Codec: Encoder + Decoder,
|
||||||
<Codec as Encoder>::Item: 'static,
|
<Codec as Encoder>::Item: 'static,
|
||||||
<Codec as Encoder>::Error: std::fmt::Debug,
|
<Codec as Encoder>::Error: std::fmt::Debug,
|
||||||
|
Out: Stream<Item = <Codec as Encoder>::Item> + Unpin,
|
||||||
{
|
{
|
||||||
#[project]
|
#[project]
|
||||||
fn poll(
|
fn poll(
|
||||||
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, Out, C, T>,
|
||||||
Poll<Result<(), ServiceError<C::Error, Codec>>>,
|
Poll<Result<(), ServiceError<C::Error, Codec>>>,
|
||||||
> {
|
> {
|
||||||
#[project]
|
#[project]
|
||||||
match self.project() {
|
match self.project() {
|
||||||
FramedServiceImplResponseInner::Connect(fut, handler, disconnect, rx) => {
|
FramedServiceImplResponseInner::Connect(fut, handler) => match fut.poll(cx) {
|
||||||
match fut.poll(cx) {
|
Poll::Ready(Ok(res)) => Either::Left(FramedServiceImplResponseInner::Handler(
|
||||||
Poll::Ready(Ok(res)) => {
|
handler.new_service(res.state),
|
||||||
Either::Left(FramedServiceImplResponseInner::Handler(
|
Some(res.framed),
|
||||||
handler.new_service(res.state.clone()),
|
res.out,
|
||||||
Some(res),
|
)),
|
||||||
disconnect.take(),
|
Poll::Pending => Either::Right(Poll::Pending),
|
||||||
rx.take(),
|
Poll::Ready(Err(e)) => Either::Right(Poll::Ready(Err(e.into()))),
|
||||||
))
|
},
|
||||||
}
|
FramedServiceImplResponseInner::Handler(fut, framed, out) => {
|
||||||
Poll::Pending => Either::Right(Poll::Pending),
|
|
||||||
Poll::Ready(Err(e)) => Either::Right(Poll::Ready(Err(e.into()))),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
FramedServiceImplResponseInner::Handler(fut, res, disconnect, rx) => {
|
|
||||||
match fut.poll(cx) {
|
match fut.poll(cx) {
|
||||||
Poll::Ready(Ok(handler)) => {
|
Poll::Ready(Ok(handler)) => {
|
||||||
let res = res.take().unwrap();
|
|
||||||
Either::Left(FramedServiceImplResponseInner::Dispatcher(
|
Either::Left(FramedServiceImplResponseInner::Dispatcher(
|
||||||
Dispatcher::new(
|
Dispatcher::new(framed.take().unwrap(), handler, out.take()),
|
||||||
res.framed,
|
|
||||||
res.state,
|
|
||||||
handler,
|
|
||||||
res.sink,
|
|
||||||
rx.take().unwrap(),
|
|
||||||
disconnect.take(),
|
|
||||||
),
|
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
Poll::Pending => Either::Right(Poll::Pending),
|
Poll::Pending => Either::Right(Poll::Pending),
|
||||||
|
@ -1,45 +0,0 @@
|
|||||||
use std::fmt;
|
|
||||||
use std::rc::Rc;
|
|
||||||
|
|
||||||
use actix_utils::oneshot;
|
|
||||||
use futures::future::{Future, FutureExt};
|
|
||||||
|
|
||||||
use crate::dispatcher::Message;
|
|
||||||
|
|
||||||
pub struct Sink<T>(Rc<dyn Fn(Message<T>)>);
|
|
||||||
|
|
||||||
impl<T> Clone for Sink<T> {
|
|
||||||
fn clone(&self) -> Self {
|
|
||||||
Sink(self.0.clone())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T> Sink<T> {
|
|
||||||
pub(crate) fn new(tx: Rc<dyn Fn(Message<T>)>) -> Self {
|
|
||||||
Sink(tx)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Close connection
|
|
||||||
pub fn close(&self) {
|
|
||||||
(self.0)(Message::Close);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Close connection
|
|
||||||
pub fn wait_close(&self) -> impl Future<Output = ()> {
|
|
||||||
let (tx, rx) = oneshot::channel();
|
|
||||||
(self.0)(Message::WaitClose(tx));
|
|
||||||
|
|
||||||
rx.map(|_| ())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Send item
|
|
||||||
pub fn send(&self, item: T) {
|
|
||||||
(self.0)(Message::Item(item));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T> fmt::Debug for Sink<T> {
|
|
||||||
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
||||||
fmt.debug_struct("Sink").finish()
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,43 +1,49 @@
|
|||||||
use std::sync::atomic::{AtomicBool, Ordering};
|
use std::cell::Cell;
|
||||||
use std::sync::Arc;
|
use std::rc::Rc;
|
||||||
use std::time::Duration;
|
|
||||||
|
|
||||||
use actix_codec::BytesCodec;
|
use actix_codec::BytesCodec;
|
||||||
use actix_rt::time::delay_for;
|
use actix_service::{fn_factory_with_config, fn_service, IntoService, Service};
|
||||||
use actix_service::{fn_service, Service};
|
|
||||||
use actix_testing::TestServer;
|
use actix_testing::TestServer;
|
||||||
|
use actix_utils::mpsc;
|
||||||
|
use bytes::{Bytes, BytesMut};
|
||||||
use futures::future::ok;
|
use futures::future::ok;
|
||||||
|
|
||||||
use actix_ioframe::{Builder, Connect};
|
use actix_ioframe::{Builder, Connect, FactoryBuilder};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
struct State;
|
struct State(Option<mpsc::Sender<Bytes>>);
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_disconnect() -> std::io::Result<()> {
|
async fn test_basic() {
|
||||||
let disconnect = Arc::new(AtomicBool::new(false));
|
let client_item = Rc::new(Cell::new(false));
|
||||||
let disconnect1 = disconnect.clone();
|
|
||||||
|
|
||||||
let srv = TestServer::with(move || {
|
let srv = TestServer::with(move || {
|
||||||
let disconnect1 = disconnect1.clone();
|
FactoryBuilder::new(fn_service(|conn: Connect<_, _>| {
|
||||||
|
ok(conn.codec(BytesCodec).state(State(None)))
|
||||||
Builder::new()
|
}))
|
||||||
.factory(fn_service(|conn: Connect<_, _>| {
|
// echo
|
||||||
ok(conn.codec(BytesCodec).state(State))
|
.build(fn_service(|t: BytesMut| ok(Some(t.freeze()))))
|
||||||
}))
|
|
||||||
.disconnect(move |_, _| {
|
|
||||||
disconnect1.store(true, Ordering::Relaxed);
|
|
||||||
})
|
|
||||||
.finish(fn_service(|_t| ok(None)))
|
|
||||||
});
|
});
|
||||||
|
|
||||||
let mut client = Builder::new()
|
let item = client_item.clone();
|
||||||
.service(|conn: Connect<_, _>| {
|
let mut client = Builder::new(fn_service(move |conn: Connect<_, _>| {
|
||||||
let conn = conn.codec(BytesCodec).state(State);
|
async move {
|
||||||
conn.sink().close();
|
let (tx, rx) = mpsc::channel();
|
||||||
ok(conn)
|
let _ = tx.send(Bytes::from_static(b"Hello"));
|
||||||
|
Ok(conn.codec(BytesCodec).out(rx).state(State(Some(tx))))
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
.build(fn_factory_with_config(move |mut cfg: State| {
|
||||||
|
let item = item.clone();
|
||||||
|
ok((move |t: BytesMut| {
|
||||||
|
assert_eq!(t.freeze(), Bytes::from_static(b"Hello"));
|
||||||
|
item.set(true);
|
||||||
|
// drop Sender, which will close connection
|
||||||
|
cfg.0.take();
|
||||||
|
ok::<_, ()>(None)
|
||||||
})
|
})
|
||||||
.finish(fn_service(|_t| ok(None)));
|
.into_service())
|
||||||
|
}));
|
||||||
|
|
||||||
let conn = actix_connect::default_connector()
|
let conn = actix_connect::default_connector()
|
||||||
.call(actix_connect::Connect::with(String::new(), srv.addr()))
|
.call(actix_connect::Connect::with(String::new(), srv.addr()))
|
||||||
@ -45,8 +51,5 @@ async fn test_disconnect() -> std::io::Result<()> {
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
client.call(conn.into_parts().0).await.unwrap();
|
client.call(conn.into_parts().0).await.unwrap();
|
||||||
let _ = delay_for(Duration::from_millis(100)).await;
|
assert!(client_item.get());
|
||||||
assert!(disconnect.load(Ordering::Relaxed));
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user