diff --git a/hptp-send/src/main.rs b/hptp-send/src/main.rs index eb41959..11e5eb5 100644 --- a/hptp-send/src/main.rs +++ b/hptp-send/src/main.rs @@ -5,7 +5,7 @@ extern crate tokio; extern crate thiserror; use hptp::log::Logger; -use hptp::msg::Msg; +use hptp::msg::{self, Msg}; use hptp::peer::Peer; use std::net::SocketAddr; use tokio::io::AsyncRead; @@ -69,12 +69,12 @@ fn parse_args(mut args: impl Iterator) -> Result(inp: &mut IN) -> Result>, Error> +async fn read_data(inp: &mut IN) -> Result>, Error> where IN: AsyncRead + Unpin, { use tokio::io::AsyncReadExt; - let mut buf = [0u8; 512]; + let mut buf = [0u8; msg::MAX_DATA_SIZE]; let len = inp.read(&mut buf).await?; Ok(if len > 0 { Some(buf[..len].into()) @@ -89,7 +89,7 @@ where { let mut pos = 0; loop { - match read_some(inp).await? { + match read_data(inp).await? { Some(data) => { let len = data.len(); peer.send(Msg::Data(data)).await?; diff --git a/hptp/src/msg.rs b/hptp/src/msg.rs index 72a2c9f..e6839cc 100644 --- a/hptp/src/msg.rs +++ b/hptp/src/msg.rs @@ -3,6 +3,9 @@ pub enum Msg { Data(Vec), } +pub const MAX_DATA_SIZE: usize = 999; +pub const MAX_SERIALIZED_SIZE: usize = MAX_DATA_SIZE; + impl Msg { pub fn des(data: &[u8]) -> Msg { Msg::Data(data.into()) diff --git a/hptp/src/peer.rs b/hptp/src/peer.rs index cde4a83..a001d9e 100644 --- a/hptp/src/peer.rs +++ b/hptp/src/peer.rs @@ -1,15 +1,13 @@ use std::net::SocketAddr; use tokio::net::UdpSocket; -use super::msg::Msg; +use super::msg::{self, Msg}; pub struct Peer { sock: UdpSocket, targ: Option, } -const BUFFER_SIZE: usize = 1000; - impl Peer { pub fn new(sock: UdpSocket) -> Self { Peer { sock, targ: None } @@ -31,7 +29,7 @@ impl Peer { } pub async fn recv(&mut self) -> Result { - let mut buf = [0u8; BUFFER_SIZE]; + let mut buf = [0u8; msg::MAX_SERIALIZED_SIZE]; let (len, who) = self.sock.recv_from(&mut buf).await?; self.set_known_target(who); Ok(Msg::des(&buf[..len]))