From: Lizzy Fleckenstein Date: Sat, 18 Feb 2023 02:22:00 +0000 (+0100) Subject: mt_rudp changes X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=680aa10c294d43c438248842246c32f250991c1d;p=mt_net.git mt_rudp changes --- diff --git a/src/conn.rs b/src/conn.rs index b5224d1..2fc9916 100644 --- a/src/conn.rs +++ b/src/conn.rs @@ -4,8 +4,8 @@ use mt_ser::{DefCfg, MtDeserialize, MtSerialize}; use std::{borrow::Cow, io}; use thiserror::Error; -pub trait Remote { - type UdpSender: mt_rudp::UdpSender; +pub trait Peer { + type UdpPeer: mt_rudp::UdpPeer; type PktFrom: MtDeserialize; type PktTo: MtSerialize + PktInfo; } @@ -14,8 +14,8 @@ pub trait Remote { pub struct RemoteSrv; #[cfg(feature = "client")] -impl Remote for RemoteSrv { - type UdpSender = mt_rudp::ToSrv; +impl Peer for RemoteSrv { + type UdpPeer = mt_rudp::RemoteSrv; type PktTo = crate::ToSrvPkt; type PktFrom = crate::ToCltPkt; } @@ -32,19 +32,16 @@ pub async fn connect(addr: &str) -> io::Result<(MtSender, MtReceiver< pub struct RemoteClt; #[cfg(feature = "server")] -impl Remote for RemoteClt { - type Sender = mt_rudp::ToClt; +impl Peer for RemoteClt { + type UdpPeer = mt_rudp::RemoteClt; type To = crate::ToCltPkt; type From = crate::ToSrvPkt; } */ -#[derive(Debug)] -pub struct MtSender(pub mt_rudp::RudpSender); - -#[derive(Debug)] -pub struct MtReceiver(pub mt_rudp::RudpReceiver); +pub struct MtSender(pub mt_rudp::RudpSender); +pub struct MtReceiver(pub mt_rudp::RudpReceiver); #[derive(Error, Debug)] pub enum RecvError { @@ -64,7 +61,7 @@ pub enum SendError { macro_rules! impl_delegate { ($T:ident) => { - impl $T { + impl $T

{ delegate! { to self.0 { pub async fn peer_id(&self) -> u16; @@ -79,20 +76,20 @@ macro_rules! impl_delegate { impl_delegate!(MtSender); impl_delegate!(MtReceiver); -impl MtReceiver { - pub async fn recv(&mut self) -> Option> { +impl MtReceiver

{ + pub async fn recv(&mut self) -> Option> { self.0.recv().await.map(|res| { res.map_err(RecvError::from).and_then(|pkt| { // TODO: warn on trailing data - R::PktFrom::mt_deserialize::(&mut io::Cursor::new(pkt.data)) + P::PktFrom::mt_deserialize::(&mut io::Cursor::new(pkt.data)) .map_err(RecvError::from) }) }) } } -impl MtSender { - pub async fn send(&self, pkt: &R::PktTo) -> Result<(), SendError> { +impl MtSender

{ + pub async fn send(&self, pkt: &P::PktTo) -> Result<(), SendError> { let mut writer = Vec::new(); pkt.mt_serialize::(&mut writer)?; @@ -109,8 +106,8 @@ impl MtSender { } } -// derive(Clone) adds unwanted trait bound to R -impl Clone for MtSender { +// derive(Clone) adds unwanted trait bound to P +impl Clone for MtSender

{ fn clone(&self) -> Self { Self(self.0.clone()) }