switch to thiserror and optimize release builds for size
parent
73b93d99af
commit
c356ed9cc3
|
@ -2,15 +2,64 @@
|
|||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "anyhow"
|
||||
version = "1.0.48"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "62e1f47f7dc0422027a4e370dd4548d4d66b26782e513e98dca1e689e058a80e"
|
||||
|
||||
[[package]]
|
||||
name = "lookaround"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"thiserror",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.32"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ba508cc11742c0dc5c1659771673afbab7a0efab23aa17e854cbab0837ed0b43"
|
||||
dependencies = [
|
||||
"unicode-xid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "38bc8cc6a5f2e3655e0899c1b848643b2562f853f114bfec7be120678e3ace05"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "1.0.82"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8daf5dd0bb60cbd4137b1b587d2fc0ae729bc07cf01cd70b36a1ed5ade3b9d59"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-xid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thiserror"
|
||||
version = "1.0.30"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "854babe52e4df1653706b98fcfc05843010039b406875930a70e4d9644e5c417"
|
||||
dependencies = [
|
||||
"thiserror-impl",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thiserror-impl"
|
||||
version = "1.0.30"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "aa32fd3f627f367fe16f893e2597ae3c05020f8bba2666a4e6ea73d377e5714b"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-xid"
|
||||
version = "0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
|
||||
|
|
|
@ -6,4 +6,10 @@ edition = "2021"
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1.0.48"
|
||||
thiserror = "1.0.30"
|
||||
|
||||
[profile.release]
|
||||
codegen-units = 1
|
||||
lto = true
|
||||
opt-level = "z"
|
||||
panic = "abort"
|
||||
|
|
61
src/main.rs
61
src/main.rs
|
@ -2,46 +2,75 @@ use std::{
|
|||
env,
|
||||
net::{
|
||||
Ipv4Addr,
|
||||
SocketAddrV4,
|
||||
UdpSocket,
|
||||
},
|
||||
str::FromStr,
|
||||
};
|
||||
|
||||
use anyhow::{
|
||||
self,
|
||||
Result,
|
||||
bail,
|
||||
};
|
||||
use thiserror::Error;
|
||||
|
||||
fn main () -> Result <()> {
|
||||
#[derive (Debug, Error)]
|
||||
enum AppError {
|
||||
#[error (transparent)]
|
||||
CliArgs (#[from] CliArgError),
|
||||
}
|
||||
|
||||
#[derive (Debug, Error)]
|
||||
enum CliArgError {
|
||||
#[error ("First argument should be a subcommand")]
|
||||
MissingSubcommand,
|
||||
#[error ("Unknown subcommand `{0}`")]
|
||||
UnknownSubcommand (String),
|
||||
}
|
||||
|
||||
struct CommonParams {
|
||||
// Servers bind on this port, clients must send to the port
|
||||
server_port: u16,
|
||||
|
||||
// Clients and servers will all join the same multicast addr
|
||||
multicast_addr: Ipv4Addr,
|
||||
}
|
||||
|
||||
impl Default for CommonParams {
|
||||
fn default () -> Self {
|
||||
Self {
|
||||
server_port: 9040,
|
||||
multicast_addr: Ipv4Addr::new (225, 100, 99, 98),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main () -> Result <(), AppError> {
|
||||
let mut args = env::args ();
|
||||
|
||||
let _exe_name = args.next ();
|
||||
|
||||
match args.next ().as_ref ().map (|s| &s[..]) {
|
||||
None => bail! ("First argument must be a subcommand"),
|
||||
None => return Err (CliArgError::MissingSubcommand.into ()),
|
||||
Some ("client") => client ()?,
|
||||
Some ("server") => server ()?,
|
||||
Some (x) => bail! ("Unknown subcommand {}", x),
|
||||
Some (x) => return Err (CliArgError::UnknownSubcommand (x.to_string ()).into ()),
|
||||
}
|
||||
|
||||
Ok (())
|
||||
}
|
||||
|
||||
fn client () -> Result <()> {
|
||||
let socket = UdpSocket::bind ("0.0.0.0:9041").unwrap ();
|
||||
fn client () -> Result <(), AppError> {
|
||||
let params = CommonParams::default ();
|
||||
let socket = UdpSocket::bind ("0.0.0.0:0").unwrap ();
|
||||
|
||||
socket.join_multicast_v4 (&(Ipv4Addr::from_str ("225.100.99.98").unwrap ()), &([0u8, 0, 0, 0].into ())).unwrap ();
|
||||
socket.join_multicast_v4 (¶ms.multicast_addr, &([0u8, 0, 0, 0].into ())).unwrap ();
|
||||
|
||||
socket.send_to ("hi there".as_bytes (), ("225.100.99.98", 9040)).unwrap ();
|
||||
socket.send_to ("hi there".as_bytes (), (params.multicast_addr, params.server_port)).unwrap ();
|
||||
|
||||
Ok (())
|
||||
}
|
||||
|
||||
fn server () -> Result <()> {
|
||||
let socket = UdpSocket::bind ("0.0.0.0:9040").unwrap ();
|
||||
fn server () -> Result <(), AppError> {
|
||||
let params = CommonParams::default ();
|
||||
let socket = UdpSocket::bind (SocketAddrV4::new (Ipv4Addr::UNSPECIFIED, params.server_port)).unwrap ();
|
||||
|
||||
socket.join_multicast_v4 (&(Ipv4Addr::from_str ("225.100.99.98").unwrap ()), &([0u8, 0, 0, 0].into ())).unwrap ();
|
||||
socket.join_multicast_v4 (¶ms.multicast_addr, &([0u8, 0, 0, 0].into ())).unwrap ();
|
||||
|
||||
let mut buf = vec! [0u8; 4096];
|
||||
let (bytes_recved, remote_addr) = socket.recv_from (&mut buf).unwrap ();
|
||||
|
|
Loading…
Reference in New Issue