mirror of
https://github.com/fafhrd91/actix-net
synced 2024-11-27 21:22:57 +01:00
impl Stream and Sink for Connect
This commit is contained in:
parent
27baf03f64
commit
9d8b3e6275
@ -6,49 +6,75 @@ use crate::dispatcher::FramedMessage;
|
|||||||
use crate::sink::Sink;
|
use crate::sink::Sink;
|
||||||
|
|
||||||
pub struct Connect<Io, St = (), Codec = ()> {
|
pub struct Connect<Io, St = (), Codec = ()> {
|
||||||
io: Io,
|
io: IoItem<Io, Codec>,
|
||||||
codec: Codec,
|
|
||||||
state: St,
|
state: St,
|
||||||
// rx: mpsc::UnboundedReceiver<FramedMessage<<Codec as Encoder>::Item>>,
|
}
|
||||||
// sink: Sink<<Codec as Encoder>::Item>,
|
|
||||||
|
enum IoItem<Io, Codec> {
|
||||||
|
Io(Io),
|
||||||
|
Framed(Framed<Io, Codec>),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<Io, C> IoItem<Io, C>
|
||||||
|
where
|
||||||
|
Io: AsyncRead + AsyncWrite,
|
||||||
|
{
|
||||||
|
fn into_codec<Codec>(self, codec: Codec) -> IoItem<Io, Codec>
|
||||||
|
where
|
||||||
|
Codec: Encoder + Decoder,
|
||||||
|
{
|
||||||
|
match self {
|
||||||
|
IoItem::Io(io) => IoItem::Framed(Framed::new(io, codec)),
|
||||||
|
IoItem::Framed(framed) => IoItem::Framed(framed.into_framed(codec)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn as_framed(&mut self) -> &mut Framed<Io, C>
|
||||||
|
where
|
||||||
|
C: Encoder + Decoder,
|
||||||
|
{
|
||||||
|
match self {
|
||||||
|
IoItem::Io(_) => panic!("Codec is not set"),
|
||||||
|
IoItem::Framed(ref mut framed) => framed,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn into_framed(self) -> Framed<Io, C>
|
||||||
|
where
|
||||||
|
C: Encoder + Decoder,
|
||||||
|
{
|
||||||
|
match self {
|
||||||
|
IoItem::Io(_) => panic!("Codec is not set"),
|
||||||
|
IoItem::Framed(framed) => framed,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Io> Connect<Io> {
|
impl<Io> Connect<Io> {
|
||||||
pub(crate) fn new(io: Io) -> Self {
|
pub(crate) fn new(io: Io) -> Self {
|
||||||
Self {
|
Self {
|
||||||
io,
|
io: IoItem::Io(io),
|
||||||
codec: (),
|
|
||||||
state: (),
|
state: (),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Io, S, C> Connect<Io, S, C> {
|
impl<Io, S, C> Connect<Io, S, C>
|
||||||
pub fn codec<Codec>(self, codec: Codec) -> Connect<Io, S, Codec> {
|
where
|
||||||
|
Io: AsyncRead + AsyncWrite,
|
||||||
|
{
|
||||||
|
pub fn codec<Codec>(self, codec: Codec) -> Connect<Io, S, Codec>
|
||||||
|
where
|
||||||
|
Codec: Encoder + Decoder,
|
||||||
|
{
|
||||||
Connect {
|
Connect {
|
||||||
codec,
|
io: self.io.into_codec(codec),
|
||||||
io: self.io,
|
|
||||||
state: self.state,
|
state: self.state,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn state<St>(self, state: St) -> Connect<Io, St, C> {
|
pub fn state<St>(self, state: St) -> Connect<Io, St, C> {
|
||||||
Connect {
|
Connect { state, io: self.io }
|
||||||
state,
|
|
||||||
io: self.io,
|
|
||||||
codec: self.codec,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn state_fn<St, F>(self, f: F) -> Connect<Io, St, C>
|
|
||||||
where
|
|
||||||
F: FnOnce(&Connect<Io, S, C>) -> St,
|
|
||||||
{
|
|
||||||
Connect {
|
|
||||||
state: f(&self),
|
|
||||||
io: self.io,
|
|
||||||
codec: self.codec,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -63,13 +89,50 @@ where
|
|||||||
|
|
||||||
ConnectResult {
|
ConnectResult {
|
||||||
state: Cell::new(self.state),
|
state: Cell::new(self.state),
|
||||||
framed: Framed::new(self.io, self.codec),
|
framed: self.io.into_framed(),
|
||||||
rx,
|
rx,
|
||||||
sink,
|
sink,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<Io, St, Codec> futures::Stream for Connect<Io, St, Codec>
|
||||||
|
where
|
||||||
|
Codec: Encoder + Decoder,
|
||||||
|
Io: AsyncRead + AsyncWrite,
|
||||||
|
{
|
||||||
|
type Item = <Codec as Decoder>::Item;
|
||||||
|
type Error = <Codec as Decoder>::Error;
|
||||||
|
|
||||||
|
fn poll(&mut self) -> futures::Poll<Option<Self::Item>, Self::Error> {
|
||||||
|
self.io.as_framed().poll()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<Io, St, Codec> futures::Sink for Connect<Io, St, Codec>
|
||||||
|
where
|
||||||
|
Codec: Encoder + Decoder,
|
||||||
|
Io: AsyncRead + AsyncWrite,
|
||||||
|
{
|
||||||
|
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.io.as_framed().start_send(item)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn poll_complete(&mut self) -> futures::Poll<(), Self::SinkError> {
|
||||||
|
self.io.as_framed().poll_complete()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn close(&mut self) -> futures::Poll<(), Self::SinkError> {
|
||||||
|
self.io.as_framed().close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub struct ConnectResult<Io, St, Codec: Encoder + Decoder> {
|
pub struct ConnectResult<Io, St, Codec: Encoder + Decoder> {
|
||||||
pub(crate) state: Cell<St>,
|
pub(crate) state: Cell<St>,
|
||||||
pub(crate) framed: Framed<Io, Codec>,
|
pub(crate) framed: Framed<Io, Codec>,
|
||||||
|
@ -193,7 +193,6 @@ pub struct FramedServiceImpl<St, C, T, Io, Codec> {
|
|||||||
|
|
||||||
impl<St, C, T, Io, Codec> Service for FramedServiceImpl<St, C, T, Io, Codec>
|
impl<St, C, T, Io, Codec> Service for FramedServiceImpl<St, C, T, Io, Codec>
|
||||||
where
|
where
|
||||||
// St: 'static,
|
|
||||||
Io: AsyncRead + AsyncWrite,
|
Io: AsyncRead + AsyncWrite,
|
||||||
C: Service<Request = Connect<Io>, Response = ConnectResult<Io, St, Codec>>,
|
C: Service<Request = Connect<Io>, Response = ConnectResult<Io, St, Codec>>,
|
||||||
C::Error: 'static,
|
C::Error: 'static,
|
||||||
|
Loading…
Reference in New Issue
Block a user