2019-07-01 07:20:24 +02:00
|
|
|
use std::marker::PhantomData;
|
2019-11-14 13:38:24 +01:00
|
|
|
use std::pin::Pin;
|
|
|
|
use std::task::{Context, Poll};
|
2019-07-01 07:20:24 +02:00
|
|
|
|
2019-06-26 11:19:40 +02:00
|
|
|
use actix_codec::{AsyncRead, AsyncWrite, Decoder, Encoder, Framed};
|
2019-11-14 13:38:24 +01:00
|
|
|
use actix_utils::mpsc;
|
|
|
|
use futures::Stream;
|
2019-06-26 11:19:40 +02:00
|
|
|
|
|
|
|
use crate::dispatcher::FramedMessage;
|
|
|
|
use crate::sink::Sink;
|
|
|
|
|
|
|
|
pub struct Connect<Io, St = (), Codec = ()> {
|
2019-07-01 07:20:24 +02:00
|
|
|
io: Io,
|
|
|
|
_t: PhantomData<(St, Codec)>,
|
2019-06-30 18:58:23 +02:00
|
|
|
}
|
|
|
|
|
2019-07-01 07:20:24 +02:00
|
|
|
impl<Io> Connect<Io>
|
2019-06-30 18:58:23 +02:00
|
|
|
where
|
2019-11-19 09:51:40 +01:00
|
|
|
Io: AsyncRead + AsyncWrite,
|
2019-06-30 18:58:23 +02:00
|
|
|
{
|
2019-06-26 11:19:40 +02:00
|
|
|
pub(crate) fn new(io: Io) -> Self {
|
|
|
|
Self {
|
2019-07-01 07:20:24 +02:00
|
|
|
io,
|
|
|
|
_t: PhantomData,
|
2019-06-26 11:19:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-01 07:20:24 +02:00
|
|
|
pub fn codec<Codec>(self, codec: Codec) -> ConnectResult<Io, (), Codec>
|
2019-06-30 18:58:23 +02:00
|
|
|
where
|
2019-11-19 09:51:40 +01:00
|
|
|
Codec: Encoder + Decoder,
|
2019-06-30 18:58:23 +02:00
|
|
|
{
|
2019-11-14 13:38:24 +01:00
|
|
|
let (tx, rx) = mpsc::channel();
|
2019-06-26 11:19:40 +02:00
|
|
|
let sink = Sink::new(tx);
|
|
|
|
|
|
|
|
ConnectResult {
|
2019-07-01 07:20:24 +02:00
|
|
|
state: (),
|
|
|
|
framed: Framed::new(self.io, codec),
|
2019-06-26 11:19:40 +02:00
|
|
|
rx,
|
|
|
|
sink,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-19 09:51:40 +01:00
|
|
|
#[pin_project::pin_project]
|
2019-06-26 11:19:40 +02:00
|
|
|
pub struct ConnectResult<Io, St, Codec: Encoder + Decoder> {
|
2019-07-01 07:20:24 +02:00
|
|
|
pub(crate) state: St,
|
2019-06-26 11:19:40 +02:00
|
|
|
pub(crate) framed: Framed<Io, Codec>,
|
2019-11-14 13:38:24 +01:00
|
|
|
pub(crate) rx: mpsc::Receiver<FramedMessage<<Codec as Encoder>::Item>>,
|
2019-06-26 11:19:40 +02:00
|
|
|
pub(crate) sink: Sink<<Codec as Encoder>::Item>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<Io, St, Codec: Encoder + Decoder> ConnectResult<Io, St, Codec> {
|
|
|
|
#[inline]
|
|
|
|
pub fn sink(&self) -> &Sink<<Codec as Encoder>::Item> {
|
|
|
|
&self.sink
|
|
|
|
}
|
2019-07-01 07:20:24 +02:00
|
|
|
|
2019-07-01 18:37:59 +02:00
|
|
|
#[inline]
|
|
|
|
pub fn get_ref(&self) -> &Io {
|
|
|
|
self.framed.get_ref()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn get_mut(&mut self) -> &mut Io {
|
|
|
|
self.framed.get_mut()
|
|
|
|
}
|
|
|
|
|
2019-07-01 07:20:24 +02:00
|
|
|
#[inline]
|
|
|
|
pub fn state<S>(self, state: S) -> ConnectResult<Io, S, Codec> {
|
|
|
|
ConnectResult {
|
|
|
|
state,
|
|
|
|
framed: self.framed,
|
|
|
|
rx: self.rx,
|
|
|
|
sink: self.sink,
|
|
|
|
}
|
|
|
|
}
|
2019-06-26 11:19:40 +02:00
|
|
|
}
|
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
impl<Io, St, Codec> Stream for ConnectResult<Io, St, Codec>
|
2019-06-26 11:19:40 +02:00
|
|
|
where
|
2019-11-19 09:51:40 +01:00
|
|
|
Io: AsyncRead + AsyncWrite,
|
|
|
|
Codec: Encoder + Decoder,
|
2019-06-26 11:19:40 +02:00
|
|
|
{
|
2019-11-14 13:38:24 +01:00
|
|
|
type Item = Result<<Codec as Decoder>::Item, <Codec as Decoder>::Error>;
|
2019-06-26 11:19:40 +02:00
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
fn poll_next(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
|
2019-11-19 09:51:40 +01:00
|
|
|
self.project().framed.next_item(cx)
|
2019-06-26 11:19:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
impl<Io, St, Codec> futures::Sink<<Codec as Encoder>::Item> for ConnectResult<Io, St, Codec>
|
2019-06-26 11:19:40 +02:00
|
|
|
where
|
2019-11-19 09:51:40 +01:00
|
|
|
Io: AsyncRead + AsyncWrite,
|
|
|
|
Codec: Encoder + Decoder,
|
2019-06-26 11:19:40 +02:00
|
|
|
{
|
2019-11-14 13:38:24 +01:00
|
|
|
type Error = <Codec as Encoder>::Error;
|
|
|
|
|
2019-11-19 09:51:40 +01:00
|
|
|
fn poll_ready(self: Pin<&mut Self>, _: &mut Context) -> Poll<Result<(), Self::Error>> {
|
|
|
|
if self.framed.is_ready() {
|
|
|
|
Poll::Ready(Ok(()))
|
|
|
|
} else {
|
|
|
|
Poll::Pending
|
|
|
|
}
|
2019-11-14 13:38:24 +01:00
|
|
|
}
|
2019-06-26 11:19:40 +02:00
|
|
|
|
|
|
|
fn start_send(
|
2019-11-14 13:38:24 +01:00
|
|
|
self: Pin<&mut Self>,
|
|
|
|
item: <Codec as Encoder>::Item,
|
|
|
|
) -> Result<(), Self::Error> {
|
2019-11-19 09:51:40 +01:00
|
|
|
self.project().framed.write(item)
|
2019-06-26 11:19:40 +02:00
|
|
|
}
|
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
2019-11-18 13:28:54 +01:00
|
|
|
self.get_mut().framed.flush(cx)
|
2019-06-26 11:19:40 +02:00
|
|
|
}
|
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
2019-11-18 13:28:54 +01:00
|
|
|
self.get_mut().framed.close(cx)
|
2019-06-26 11:19:40 +02:00
|
|
|
}
|
|
|
|
}
|