1
0
mirror of https://github.com/fafhrd91/actix-net synced 2024-11-28 01:52:57 +01:00
actix-net/actix-ioframe/src/connect.rs

111 lines
2.6 KiB
Rust
Raw Normal View History

2019-07-01 07:20:24 +02:00
use std::marker::PhantomData;
2019-06-26 11:19:40 +02:00
use actix_codec::{AsyncRead, AsyncWrite, Decoder, Encoder, Framed};
use futures::unsync::mpsc;
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
Io: AsyncRead + AsyncWrite,
{
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
Codec: Encoder + Decoder,
{
2019-06-26 11:19:40 +02:00
let (tx, rx) = mpsc::unbounded();
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,
}
}
}
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>,
pub(crate) rx: mpsc::UnboundedReceiver<FramedMessage<<Codec as Encoder>::Item>>,
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
#[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
}
impl<Io, St, Codec> futures::Stream for ConnectResult<Io, St, Codec>
where
Io: AsyncRead + AsyncWrite,
Codec: Encoder + Decoder,
{
type Item = <Codec as Decoder>::Item;
type Error = <Codec as Decoder>::Error;
fn poll(&mut self) -> futures::Poll<Option<Self::Item>, Self::Error> {
self.framed.poll()
}
}
impl<Io, St, Codec> futures::Sink for ConnectResult<Io, St, Codec>
where
Io: AsyncRead + AsyncWrite,
Codec: Encoder + Decoder,
{
type SinkItem = <Codec as Encoder>::Item;
type SinkError = <Codec as Encoder>::Error;
fn start_send(
&mut self,
item: Self::SinkItem,
) -> futures::StartSend<Self::SinkItem, Self::SinkError> {
self.framed.start_send(item)
}
fn poll_complete(&mut self) -> futures::Poll<(), Self::SinkError> {
self.framed.poll_complete()
}
fn close(&mut self) -> futures::Poll<(), Self::SinkError> {
self.framed.close()
}
}