From a982950203aa56a93a7ba7a63062eccdd24046c5 Mon Sep 17 00:00:00 2001 From: Dreaded_X Date: Fri, 11 Aug 2023 03:41:44 +0200 Subject: [PATCH] Converted to async --- wakey/Cargo.toml | 1 + wakey/src/lib.rs | 12 +++++++----- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/wakey/Cargo.toml b/wakey/Cargo.toml index 05e413e..cab72c4 100644 --- a/wakey/Cargo.toml +++ b/wakey/Cargo.toml @@ -15,3 +15,4 @@ categories = ["network-programming"] [dependencies] hex = "~0.4" arrayvec = "0.7.2" +tokio = { version = "1.30.0", features = ["net"] } diff --git a/wakey/src/lib.rs b/wakey/src/lib.rs index a2f7fb0..c75c93a 100644 --- a/wakey/src/lib.rs +++ b/wakey/src/lib.rs @@ -10,10 +10,11 @@ //! ``` use std::error::Error; -use std::net::{SocketAddr, ToSocketAddrs, UdpSocket}; +use std::net::SocketAddr; use std::{fmt, iter}; use arrayvec::ArrayVec; +use tokio::net::{ToSocketAddrs, UdpSocket}; const MAC_SIZE: usize = 6; const MAC_PER_MAGIC: usize = 16; @@ -95,11 +96,12 @@ impl WolPacket { /// let wol = wakey::WolPacket::from_bytes(&vec![0x00, 0x01, 0x02, 0x03, 0x04, 0x05]).unwrap(); /// wol.send_magic(); /// ``` - pub fn send_magic(&self) -> Result<()> { + pub async fn send_magic(&self) -> Result<()> { self.send_magic_to( SocketAddr::from(([0, 0, 0, 0], 0)), SocketAddr::from(([255, 255, 255, 255], 9)), ) + .await } /// Broadcasts the magic packet from / to specified address. @@ -111,10 +113,10 @@ impl WolPacket { /// let dst = SocketAddr::from(([255,255,255,255], 9)); /// wol.send_magic_to(src, dst); /// ``` - pub fn send_magic_to(&self, src: A, dst: A) -> Result<()> { - let udp_sock = UdpSocket::bind(src)?; + pub async fn send_magic_to(&self, src: A, dst: A) -> Result<()> { + let udp_sock = UdpSocket::bind(src).await?; udp_sock.set_broadcast(true)?; - udp_sock.send_to(&self.packet, dst)?; + udp_sock.send_to(&self.packet, dst).await?; Ok(()) }