put some important constants in the 'msg' module
This commit is contained in:
parent
204aa4998f
commit
6ace49cc88
|
@ -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<Item = String>) -> Result<SocketAddr, Erro
|
|||
.map_err(|_| Error::InvalidArgs)
|
||||
}
|
||||
|
||||
async fn read_some<IN>(inp: &mut IN) -> Result<Option<Vec<u8>>, Error>
|
||||
async fn read_data<IN>(inp: &mut IN) -> Result<Option<Vec<u8>>, 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?;
|
||||
|
|
|
@ -3,6 +3,9 @@ pub enum Msg {
|
|||
Data(Vec<u8>),
|
||||
}
|
||||
|
||||
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())
|
||||
|
|
|
@ -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<SocketAddr>,
|
||||
}
|
||||
|
||||
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<Msg, tokio::io::Error> {
|
||||
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]))
|
||||
|
|
Loading…
Reference in New Issue