This commit is contained in:
Milo Turner 2020-03-09 17:36:23 -04:00
parent 5dcda7aec3
commit f0481e350d
2 changed files with 9 additions and 9 deletions

View File

@ -4,10 +4,10 @@ use tokio::net::UdpSocket;
use super::msg::{self, SerDes}; use super::msg::{self, SerDes};
pub struct Peer<F, T> { pub struct Peer<S, R> {
sock: UdpSocket, sock: UdpSocket,
targ: Option<SocketAddr>, targ: Option<SocketAddr>,
_phantom: PhantomData<fn(F) -> T>, _phantom: PhantomData<fn(S) -> R>,
} }
pub type UpPeer = Peer<msg::UpMsg, msg::DownMsg>; pub type UpPeer = Peer<msg::UpMsg, msg::DownMsg>;
@ -38,7 +38,7 @@ pub enum SendError {
NoTarget, NoTarget,
} }
impl<F, T> Peer<F, T> { impl<S, R> Peer<S, R> {
pub fn new(sock: UdpSocket) -> Self { pub fn new(sock: UdpSocket) -> Self {
Peer { Peer {
sock, sock,
@ -51,9 +51,9 @@ impl<F, T> Peer<F, T> {
self.targ = Some(addr); self.targ = Some(addr);
} }
pub async fn send(&mut self, msg: F) -> Result<(), SendError> pub async fn send(&mut self, msg: S) -> Result<(), SendError>
where where
F: SerDes, S: SerDes,
{ {
let mut buf = [0u8; msg::MAX_TOTAL_PACKET_SIZE]; let mut buf = [0u8; msg::MAX_TOTAL_PACKET_SIZE];
let len = msg.ser_to(&mut buf); let len = msg.ser_to(&mut buf);
@ -62,13 +62,13 @@ impl<F, T> Peer<F, T> {
Ok(()) Ok(())
} }
pub async fn recv(&mut self) -> Result<T, RecvError> pub async fn recv(&mut self) -> Result<R, RecvError>
where where
T: SerDes, R: SerDes,
{ {
let mut buf = [0u8; msg::MAX_TOTAL_PACKET_SIZE]; let mut buf = [0u8; msg::MAX_TOTAL_PACKET_SIZE];
let (len, who) = self.sock.recv_from(&mut buf).await?; let (len, who) = self.sock.recv_from(&mut buf).await?;
self.set_known_target(who); self.set_known_target(who);
Ok(T::des(&buf[..len])?) Ok(R::des(&buf[..len])?)
} }
} }

View File

@ -3,7 +3,7 @@ pub const MAX_TOTAL_PACKET_SIZE: usize = 1472;
// TODO: change these based off the decoders // TODO: change these based off the decoders
pub const UP_HEADER_SIZE: usize = 0; pub const UP_HEADER_SIZE: usize = 0;
pub const DOWN_HEADER_SIZE: usize = 0; pub const DOWN_HEADER_SIZE: usize = 1;
/// This is the maximum amount of segment data we can fit into a packet. /// This is the maximum amount of segment data we can fit into a packet.
pub const MAX_SEG_SIZE: usize = MAX_TOTAL_PACKET_SIZE - UP_HEADER_SIZE; pub const MAX_SEG_SIZE: usize = MAX_TOTAL_PACKET_SIZE - UP_HEADER_SIZE;