Converted to async

This commit is contained in:
Dreaded_X 2023-08-11 03:41:44 +02:00
parent 289d4437f9
commit a982950203
Signed by: Dreaded_X
GPG Key ID: FA5F485356B0D2D4
2 changed files with 8 additions and 5 deletions

View File

@ -15,3 +15,4 @@ categories = ["network-programming"]
[dependencies]
hex = "~0.4"
arrayvec = "0.7.2"
tokio = { version = "1.30.0", features = ["net"] }

View File

@ -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<A: ToSocketAddrs>(&self, src: A, dst: A) -> Result<()> {
let udp_sock = UdpSocket::bind(src)?;
pub async fn send_magic_to<A: ToSocketAddrs>(&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(())
}