tokio UDP port open in hptp-recv

This commit is contained in:
Milo Turner 2020-03-07 15:42:24 -05:00
parent 03eff314a7
commit da55e83e3b
3 changed files with 34 additions and 5 deletions

3
Cargo.lock generated
View File

@ -96,6 +96,7 @@ dependencies = [
name = "hptp-recv"
version = "0.1.0"
dependencies = [
"failure",
"hptp",
"tokio",
]
@ -104,9 +105,7 @@ dependencies = [
name = "hptp-send"
version = "0.1.0"
dependencies = [
"failure",
"hptp",
"tokio",
]
[[package]]

View File

@ -8,4 +8,6 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
hptp = { path = "../hptp" }
hptp = { path = "../hptp" }
tokio = {version = "0.2.*", features = ["rt-core", "io-std", "io-util", "udp"]}
failure = "0.1.7"

View File

@ -1,5 +1,33 @@
extern crate failure;
extern crate hptp;
extern crate tokio;
fn main() {
println!("{}", hptp::RECV_GREETING);
use failure::Error;
use hptp::log::Logger;
use std::net::Ipv4Addr;
use tokio::net::UdpSocket;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut rt = tokio::runtime::Runtime::new()?;
let mut log = Logger::new();
let fut = start(&mut log);
Ok(rt.block_on(fut)?)
}
async fn start(log: &mut Logger) -> Result<(), Error> {
let host = (Ipv4Addr::LOCALHOST, 3700);
let sock = UdpSocket::bind(host).await?;
log.bound(sock.local_addr()?.port()).await?;
let (mut rx, _tx) = sock.split();
let mut buf = [0u8; 2000];
loop {
let (n, _who) = rx.recv_from(&mut buf).await?;
log.debug_msg(match String::from_utf8(buf[..n].into()) {
Ok(s) => format!("got: {:?}", s),
Err(e) => format!("got: {:?} (invalid utf8)", e.as_bytes()),
})
.await?;
}
}