#[cfg(feature = "system")]
#[allow(dead_code)]
pub mod woltokio {
use {Deserialize, Serialize};
use std::io;
use tokio::net::UdpSocket;
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct MagicPacket {
pub mac: String,
pub ip: String, }
impl MagicPacket {
fn create_magic_packet(mac: &str) -> io::Result<Vec<u8>> {
let mut packet = Vec::new();
packet.extend([0xFF; 6]);
let mac_bytes = mac
.split(':')
.map(|byte| u8::from_str_radix(byte, 16))
.collect::<Result<Vec<u8>, _>>()
.unwrap();
for _ in 0..16 {
packet.extend(&mac_bytes);
}
Ok(packet)
}
pub async fn send(&self) -> io::Result<()> {
let socket = UdpSocket::bind("0.0.0.0:0").await?;
socket.set_broadcast(true)?;
let broadcast_address = format!("{}:9", self.ip);
let magic_packet = Self::create_magic_packet(&self.mac)?;
socket.send_to(&magic_packet, broadcast_address).await?;
Ok(())
}
}
}