From b620bcfe06239797281f2dffb35b427bae01cf3d Mon Sep 17 00:00:00 2001 From: _ <_@_> Date: Thu, 9 Dec 2021 00:45:18 +0000 Subject: [PATCH] :recycle: refactor: make it a little more idiomatic --- Cargo.lock | 12 ---------- Cargo.toml | 2 +- src/main.rs | 66 +++++++++++++++++++++++++++-------------------------- 3 files changed, 35 insertions(+), 45 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index dec92d8..699b465 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -236,21 +236,9 @@ dependencies = [ "libc", "mio", "pin-project-lite", - "tokio-macros", "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]] name = "unicode-xid" version = "0.2.2" diff --git a/Cargo.toml b/Cargo.toml index aee67cb..a997e45 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,7 +15,7 @@ version = "0.1.4" mac_address = "1.1.2" rand = "0.8.4" 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] codegen-units = 1 diff --git a/src/main.rs b/src/main.rs index bb5e78a..dd3266f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,7 +7,7 @@ use std::{ SocketAddrV4, }, str::FromStr, - time::{Duration, Instant}, + time::{Duration}, }; use mac_address::{ @@ -17,8 +17,10 @@ use mac_address::{ use thiserror::Error; use tokio::{ net::UdpSocket, - select, - time::sleep, + time::{ + sleep, + timeout, + }, }; mod ip; @@ -184,39 +186,12 @@ async fn client > (mut args: I) -> Result <(), AppErr for _ in 0..10 { 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); - while Instant::now () < start_time + Duration::from_secs (2) { - 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); - } + timeout (Duration::from_secs (2), listen_for_responses (&socket, &mut peers)).await.ok (); let mut peers: Vec <_> = peers.into_iter ().collect (); peers.sort_by_key (|(_, v)| v.mac); @@ -245,6 +220,33 @@ async fn client > (mut args: I) -> Result <(), AppErr Ok (()) } +async fn listen_for_responses ( + socket: &UdpSocket, + peers: &mut HashMap +) { + 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 > (mut args: I) -> Result <(), AppError> { let common_params = CommonParams::default ();