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;
|
extern crate thiserror;
|
||||||
|
|
||||||
use hptp::log::Logger;
|
use hptp::log::Logger;
|
||||||
use hptp::msg::Msg;
|
use hptp::msg::{self, Msg};
|
||||||
use hptp::peer::Peer;
|
use hptp::peer::Peer;
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
use tokio::io::AsyncRead;
|
use tokio::io::AsyncRead;
|
||||||
|
@ -69,12 +69,12 @@ fn parse_args(mut args: impl Iterator<Item = String>) -> Result<SocketAddr, Erro
|
||||||
.map_err(|_| Error::InvalidArgs)
|
.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
|
where
|
||||||
IN: AsyncRead + Unpin,
|
IN: AsyncRead + Unpin,
|
||||||
{
|
{
|
||||||
use tokio::io::AsyncReadExt;
|
use tokio::io::AsyncReadExt;
|
||||||
let mut buf = [0u8; 512];
|
let mut buf = [0u8; msg::MAX_DATA_SIZE];
|
||||||
let len = inp.read(&mut buf).await?;
|
let len = inp.read(&mut buf).await?;
|
||||||
Ok(if len > 0 {
|
Ok(if len > 0 {
|
||||||
Some(buf[..len].into())
|
Some(buf[..len].into())
|
||||||
|
@ -89,7 +89,7 @@ where
|
||||||
{
|
{
|
||||||
let mut pos = 0;
|
let mut pos = 0;
|
||||||
loop {
|
loop {
|
||||||
match read_some(inp).await? {
|
match read_data(inp).await? {
|
||||||
Some(data) => {
|
Some(data) => {
|
||||||
let len = data.len();
|
let len = data.len();
|
||||||
peer.send(Msg::Data(data)).await?;
|
peer.send(Msg::Data(data)).await?;
|
||||||
|
|
|
@ -3,6 +3,9 @@ pub enum Msg {
|
||||||
Data(Vec<u8>),
|
Data(Vec<u8>),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub const MAX_DATA_SIZE: usize = 999;
|
||||||
|
pub const MAX_SERIALIZED_SIZE: usize = MAX_DATA_SIZE;
|
||||||
|
|
||||||
impl Msg {
|
impl Msg {
|
||||||
pub fn des(data: &[u8]) -> Msg {
|
pub fn des(data: &[u8]) -> Msg {
|
||||||
Msg::Data(data.into())
|
Msg::Data(data.into())
|
||||||
|
|
|
@ -1,15 +1,13 @@
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
use tokio::net::UdpSocket;
|
use tokio::net::UdpSocket;
|
||||||
|
|
||||||
use super::msg::Msg;
|
use super::msg::{self, Msg};
|
||||||
|
|
||||||
pub struct Peer {
|
pub struct Peer {
|
||||||
sock: UdpSocket,
|
sock: UdpSocket,
|
||||||
targ: Option<SocketAddr>,
|
targ: Option<SocketAddr>,
|
||||||
}
|
}
|
||||||
|
|
||||||
const BUFFER_SIZE: usize = 1000;
|
|
||||||
|
|
||||||
impl Peer {
|
impl Peer {
|
||||||
pub fn new(sock: UdpSocket) -> Self {
|
pub fn new(sock: UdpSocket) -> Self {
|
||||||
Peer { sock, targ: None }
|
Peer { sock, targ: None }
|
||||||
|
@ -31,7 +29,7 @@ impl Peer {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn recv(&mut self) -> Result<Msg, tokio::io::Error> {
|
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?;
|
let (len, who) = self.sock.recv_from(&mut buf).await?;
|
||||||
self.set_known_target(who);
|
self.set_known_target(who);
|
||||||
Ok(Msg::des(&buf[..len]))
|
Ok(Msg::des(&buf[..len]))
|
||||||
|
|
Loading…
Reference in New Issue