♻️ refactor: make it a little more idiomatic

main
_ 2021-12-09 00:45:18 +00:00
parent 5d8bb3282b
commit b620bcfe06
3 changed files with 35 additions and 45 deletions

12
Cargo.lock generated
View File

@ -236,21 +236,9 @@ dependencies = [
"libc", "libc",
"mio", "mio",
"pin-project-lite", "pin-project-lite",
"tokio-macros",
"winapi", "winapi",
] ]
[[package]]
name = "tokio-macros"
version = "1.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c9efc1aba077437943f7515666aa2b882dfabfbfdf89c819ea75a8d6e9eaba5e"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "unicode-xid" name = "unicode-xid"
version = "0.2.2" version = "0.2.2"

View File

@ -15,7 +15,7 @@ version = "0.1.4"
mac_address = "1.1.2" mac_address = "1.1.2"
rand = "0.8.4" rand = "0.8.4"
thiserror = "1.0.30" thiserror = "1.0.30"
tokio = { version = "1.14.0", features = ["fs", "macros", "net", "rt", "time"] } tokio = { version = "1.14.0", features = ["fs", "net", "rt", "time"] }
[profile.release] [profile.release]
codegen-units = 1 codegen-units = 1

View File

@ -7,7 +7,7 @@ use std::{
SocketAddrV4, SocketAddrV4,
}, },
str::FromStr, str::FromStr,
time::{Duration, Instant}, time::{Duration},
}; };
use mac_address::{ use mac_address::{
@ -17,8 +17,10 @@ use mac_address::{
use thiserror::Error; use thiserror::Error;
use tokio::{ use tokio::{
net::UdpSocket, net::UdpSocket,
select, time::{
time::sleep, sleep,
timeout,
},
}; };
mod ip; mod ip;
@ -184,39 +186,12 @@ async fn client <I : Iterator <Item=String>> (mut args: I) -> Result <(), AppErr
for _ in 0..10 { for _ in 0..10 {
socket.send_to (&msg, (common_params.multicast_addr, common_params.server_port)).await?; socket.send_to (&msg, (common_params.multicast_addr, common_params.server_port)).await?;
std::thread::sleep (Duration::from_millis (100)); sleep (Duration::from_millis (100)).await;
} }
let start_time = Instant::now ();
let mut peers = HashMap::with_capacity (10); let mut peers = HashMap::with_capacity (10);
while Instant::now () < start_time + Duration::from_secs (2) { timeout (Duration::from_secs (2), listen_for_responses (&socket, &mut peers)).await.ok ();
let x = select! {
x = recv_msg_from (&socket) => x,
() = sleep (Duration::from_millis (1_000)) => continue,
};
let (msgs, remote_addr) = match x {
Err (_) => continue,
Ok (x) => x,
};
let mut resp = ServerResponse {
mac: None,
nickname: None,
};
for msg in msgs.into_iter () {
match msg {
Message::Response1 (x) => resp.mac = x,
Message::Response2 (x) => resp.nickname = Some (x.nickname),
_ => (),
}
}
peers.insert (remote_addr, resp);
}
let mut peers: Vec <_> = peers.into_iter ().collect (); let mut peers: Vec <_> = peers.into_iter ().collect ();
peers.sort_by_key (|(_, v)| v.mac); peers.sort_by_key (|(_, v)| v.mac);
@ -245,6 +220,33 @@ async fn client <I : Iterator <Item=String>> (mut args: I) -> Result <(), AppErr
Ok (()) Ok (())
} }
async fn listen_for_responses (
socket: &UdpSocket,
peers: &mut HashMap <SocketAddr, ServerResponse>
) {
loop {
let (msgs, remote_addr) = match recv_msg_from (socket).await {
Err (_) => continue,
Ok (x) => x,
};
let mut resp = ServerResponse {
mac: None,
nickname: None,
};
for msg in msgs.into_iter () {
match msg {
Message::Response1 (x) => resp.mac = x,
Message::Response2 (x) => resp.nickname = Some (x.nickname),
_ => (),
}
}
peers.insert (remote_addr, resp);
}
}
async fn server <I: Iterator <Item=String>> (mut args: I) -> Result <(), AppError> async fn server <I: Iterator <Item=String>> (mut args: I) -> Result <(), AppError>
{ {
let common_params = CommonParams::default (); let common_params = CommonParams::default ();