V3OPQUNK5UPWNZ7BBESYQ2XDBOGDYS3DMOOVYIM2K7WHEPSIPMGAC
MT3IK66TJ6KVVJLT3JBN5CIJTZVD5WSYRK5WDVRGQ4BPFXACXJKQC
AWPTZQQYEYW2BMXCWYAWSZAVUE55YOMO7IE4VCXP5J4UOKUMKQFAC
SMM42WXFAYFMOMUR7DCVCZCK7QMXIE5KUH4GUOIZPVFDDYB4ZMZAC
42CBEYZTGKPTDFNBTNCBUWWCABZOMBY3FCWWHUILQIGO2M54OJXQC
2JZYL7R4OQIZMHTBGZA6HNAC7KLN7DSGZJRE7YILLMTR3WR2XLHAC
2DG7PVTZAJ4NMGY2BNODJRLYTOV3MPTH3V6EZMK3WSIUZESLPQXQC
KWR2TS2D7YN4LCGMQZ35V4WBLQDHAVSZJV3SZAIR5HQRSJAWLWCQC
K3I54HXNNCTEA7L6UGWIVBFB5TPZVZZXDBRSR2YZDIKREXQVWRIQC
LSQ7OPL7Z5WJDO7ZZS3D4UPMEEWUUF2LNQ3FZUWWFUVZEXA7CIOAC
IG6HG46TPITA2WAGCY3OTNRZS3364F7X3NS4MX3YXO2FPKOVGG7AC
7RMAZDLQ6FEEQGUUAIVEM4X2VME7IUUZMEJMMV3H46U3UKO4BODQC
NRKM62AKJZID7WAF57OJZY2AB5IOJ2OQ4GGIFTDRWYWEDHMS6H3AC
AGPU6SD7V5FQNI6W54HDRJQCVHWYCZIHDHL6IL5LVVMJ7SVJL7UAC
M2FGQSGQITI7FYU3RWY3IGIA4ZLS6JAZMHIO64WNHKS4JIL53OWQC
6WLPW4L4PN6ANYYW3I6AOCN3SXQP37RQTUOM52IVXL3SRZD7UHAAC
YYUB6HZYSVQSCO7DGF6ETPAXTMG6SG2RQQXLP73NKPLDMZEC3PGAC
[[package]]
name = "async-trait"
version = "0.1.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "687c230d85c0a52504709705fc8a53e4a692b83a2184f03dae73e38e1e93a783"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
async fn send_to(&mut self, event: &NetEvent, addr: &Self::Addr) -> io::Result<()>;
async fn send_to_all(&mut self, event: &NetEvent) -> io::Result<()>;
async fn recv_from(&mut self) -> io::Result<(NetEvent, Self::Addr)>;
pub trait NetworkInterface<Addr>
: Stream<Item=bincode::Result<(NetEvent, Addr)>>
+ Sink<(NetEvent, Addr), Error=bincode::Error>
+ Sink<NetEvent, Error=bincode::Error>
{
#[async_trait]
impl NetInterface for IpInterface {
type Addr = SocketAddr;
impl Stream for Network {
type Item = bincode::Result<(NetEvent, SocketAddr)>;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
let item = Pin::new(&mut self.framed).poll_next(cx);
if let Poll::Ready(Some(Ok((event, addr)))) = &item {
debug!("<= {} => {:#?}", addr, event);
}
item
}
}
impl Sink<(NetEvent, SocketAddr)> for Network {
type Error = bincode::Error;
fn poll_ready(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<(), Self::Error>> {
Pin::new(&mut self.framed).poll_ready(cx)
}
fn start_send(mut self: Pin<&mut Self>, item: (NetEvent, SocketAddr)) -> Result<(), Self::Error> {
let (event, addr) = &item;
debug!("=> {} <= {:#?}", addr, event);
Pin::new(&mut self.framed).start_send(item)
}
async fn send_to(&mut self, event: &NetEvent, addr: &Self::Addr) -> io::Result<()> {
let packet = bincode::serialize(event).unwrap();
debug!("=> {} <= ({} bytes) {:#?}", addr, packet.len(), event);
self.socket.send_to(&packet, addr).await?;
Ok(())
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<(), Self::Error>> {
Pin::new(&mut self.framed).poll_flush(cx)
async fn send_to_all(&mut self, event: &NetEvent) -> io::Result<()> {
fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<(), Self::Error>> {
Pin::new(&mut self.framed).poll_close(cx)
}
}
impl Sink<NetEvent> for Network {
type Error = bincode::Error;
fn poll_ready(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<(), Self::Error>> {
<Network as Sink<(NetEvent, SocketAddr)>>::poll_ready(self, cx)
}
fn start_send(self: Pin<&mut Self>, event: NetEvent) -> Result<(), Self::Error> {
self.send_to(event, &addr).await?;
Ok(())
<Network as Sink<(NetEvent, SocketAddr)>>::start_send(self, (event, addr))
}
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<(), Self::Error>> {
<Network as Sink<(NetEvent, SocketAddr)>>::poll_flush(self, cx)
}
fn poll_close(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<(), Self::Error>> {
<Network as Sink<(NetEvent, SocketAddr)>>::poll_close(self, cx)
}
}
struct Codec;
impl Decoder for Codec {
type Item = NetEvent;
type Error = bincode::Error;
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
// Assume that an event will be received in a single packet
Ok(Some(bincode::deserialize_from(src.reader())?))
async fn recv_from(&mut self) -> io::Result<(NetEvent, Self::Addr)> {
let mut buf = [0; 1024];
let (len, addr) = self.socket.recv_from(&mut buf).await?;
let event = bincode::deserialize(&buf[..len]).unwrap();
debug!("<= {} => ({} bytes) {:#?}", addr, len, event);
Ok((event, addr))
impl Encoder<NetEvent> for Codec {
type Error = bincode::Error;
fn encode(&mut self, event: NetEvent, dst: &mut BytesMut) -> Result<(), Self::Error> {
// Assume that a serialized event won't exceed the MTU
bincode::serialize_into(dst.writer(), &event)
impl NetworkInterface<SocketAddr> for Network { }