send hello world to the specified host
This commit is contained in:
parent
4a70ba8df7
commit
ff97bcc18c
|
@ -1,53 +1,75 @@
|
||||||
// #![feature(backtrace)]
|
#![feature(backtrace)]
|
||||||
extern crate hptp;
|
extern crate hptp;
|
||||||
extern crate tokio;
|
extern crate tokio;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate thiserror;
|
extern crate thiserror;
|
||||||
|
|
||||||
|
use hptp::log::Logger;
|
||||||
use std::net::SocketAddrV4;
|
use std::net::SocketAddrV4;
|
||||||
|
|
||||||
#[derive(Error, Debug)]
|
#[derive(Error, Debug)]
|
||||||
enum Error {
|
enum Error {
|
||||||
|
#[error("io error: {source}")]
|
||||||
|
Io {
|
||||||
|
#[from]
|
||||||
|
source: tokio::io::Error,
|
||||||
|
backtrace: std::backtrace::Backtrace,
|
||||||
|
},
|
||||||
#[error("invalid command-line arguments")]
|
#[error("invalid command-line arguments")]
|
||||||
InvalidArgs,
|
InvalidArgs,
|
||||||
|
#[error("did not find an available local port")]
|
||||||
|
NoAvailPort,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
match entry() {
|
match entry() {
|
||||||
Err(Error::InvalidArgs) => print_usage(),
|
Err(Error::InvalidArgs) => print_usage(),
|
||||||
/*
|
Err(e) => {
|
||||||
Err(e) => {
|
use std::error::Error;
|
||||||
use std::error::Error;
|
println!("Error: {:?}", e);
|
||||||
println!("Error: {:?}", e);
|
for bt in e.backtrace() {
|
||||||
for bt in e.backtrace() {
|
println!("{}", bt);
|
||||||
println!("{}", bt);
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
*/
|
|
||||||
Ok(()) => (),
|
Ok(()) => (),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn print_usage() {
|
fn print_usage() {
|
||||||
print!("usage:\n./3700send <host-ip>:<host-port>\n")
|
print!("Usage:\n./3700send <host-ip>:<host-port>\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
fn entry() -> Result<(), Error> {
|
fn entry() -> Result<(), Error> {
|
||||||
let mut rt = tokio::runtime::Runtime::new().unwrap();
|
let mut rt = tokio::runtime::Runtime::new().unwrap();
|
||||||
// let mut log = Logger::new();
|
let mut log = Logger::new();
|
||||||
rt.block_on(start())
|
rt.block_on(start(&mut log))
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn start() -> Result<(), Error> {
|
async fn start(log: &mut Logger) -> Result<(), Error> {
|
||||||
let addr = parse_arg()?;
|
let targ_addr = parse_args(std::env::args())?;
|
||||||
println!("ip={}, port={}", addr.ip(), addr.port());
|
let sock = hptp::udp_retrying_bind(16, hptp::random_port)
|
||||||
|
.await
|
||||||
|
.map_err(|_| Error::NoAvailPort)?;
|
||||||
|
log.debug_msg(format!("bound on {}", sock.local_addr()?))
|
||||||
|
.await;
|
||||||
|
upload(log, sock, targ_addr).await?;
|
||||||
|
log.debug_msg("bye!").await;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_arg() -> Result<SocketAddrV4, Error> {
|
fn parse_args(mut args: impl Iterator<Item = String>) -> Result<SocketAddrV4, Error> {
|
||||||
std::env::args()
|
args.nth(1)
|
||||||
.nth(1)
|
|
||||||
.ok_or(Error::InvalidArgs)?
|
.ok_or(Error::InvalidArgs)?
|
||||||
.parse()
|
.parse()
|
||||||
.map_err(|_| Error::InvalidArgs)
|
.map_err(|_| Error::InvalidArgs)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn upload(
|
||||||
|
_log: &mut Logger,
|
||||||
|
mut sock: tokio::net::UdpSocket,
|
||||||
|
targ_addr: SocketAddrV4,
|
||||||
|
) -> Result<(), Error> {
|
||||||
|
sock.send_to("Hello, ".as_bytes(), targ_addr).await?;
|
||||||
|
sock.send_to("world!".as_bytes(), targ_addr).await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue