Last active
November 4, 2017 12:19
-
-
Save jon-zu/157cf9c60673214629d048d692503832 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use futures::{Future, Sink, Stream, Poll, Async}; | |
use tokio_io::{AsyncRead, AsyncWrite}; | |
use tokio_io::codec::Framed; | |
use codec; | |
use codec::{FireCodec, WrappedFireCodec}; | |
pub mod errors; | |
trait ServerService<C: FireCodec>: { | |
fn handle_packet(&mut self, packet: C::PacketItem) | |
-> Box<Future<Item = C::PacketItem, Error = errors::Error>>; | |
fn send_packet(&mut self, packet: C::PacketItem) | |
-> Box<Future<Item = (), Error = errors::Error>>; | |
} | |
trait ClientService<C: FireCodec>: { | |
fn handle_packet(&mut self, packet: C::PacketItem) | |
-> Box<Future<Item = (), Error = errors::Error>>; | |
fn send_packet(&mut self, packet: C::PacketItem) | |
-> Box<Future<Item = (), Error = errors::Error>>; | |
} | |
struct FireProto<C, T> where | |
C: FireCodec { | |
codec: Framed<T, WrappedFireCodec<C>> | |
} | |
impl<C, T> FireProto<C, T> where | |
C: FireCodec, | |
T: AsyncRead + AsyncWrite { | |
pub fn new(io: T) -> Self { | |
let fire_codec = C::default().into_wrapped(); | |
let framed = io.framed(fire_codec); | |
FireProto { | |
codec: framed | |
} | |
} | |
pub fn into_client<S: ClientService<C>>(self, next: S) -> ClientServiceImpl<C, T, S> { | |
ClientServiceImpl { | |
proto: self, | |
next | |
} | |
} | |
} | |
struct ClientServiceImpl<C, T, S> where | |
C: FireCodec, | |
T: AsyncRead + AsyncWrite, | |
S: ClientService<C> { | |
proto: FireProto<C, T>, | |
next: S | |
} | |
impl<C, T, S> ClientService<C> for ClientServiceImpl<C, T, S> where | |
C: FireCodec, | |
T: AsyncRead + AsyncWrite, | |
S: ClientService<C> { | |
fn handle_packet(&mut self, packet: C::PacketItem) -> Box<Future<Item = (), Error = errors::Error>> { | |
self.next.handle_packet(packet) | |
} | |
fn send_packet(&mut self, packet: C::PacketItem) -> Box<Future<Item = (), Error = errors::Error>> { | |
//self.proto.codec.send(packet) | |
//Do I need a channel here to let poll send the packets | |
unimplemented!(); | |
} | |
} | |
impl<C, T, S> Future for ClientServiceImpl<C, T, S> where | |
C: FireCodec, | |
T: AsyncRead + AsyncWrite, | |
S: ClientService<C> { | |
type Item = (); | |
type Error = errors::Error; | |
fn poll(&mut self) -> Poll<Self::Item, Self::Error> { | |
if let Async::Ready(packet) = self.proto.codec.poll()? { | |
//?: self.handle_packet(packet) | |
//Do I need a state machine here | |
} | |
//Ready if dropped/closed connection | |
Ok(Async::NotReady) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment