use crate::Packet;
use std::net::{Ipv4Addr, ToSocketAddrs};
use std::{convert::TryInto, net::UdpSocket};
pub trait UdpSocketExt {
fn bind_connect(addr: impl ToSocketAddrs) -> std::io::Result<UdpSocket>;
fn send_command(&self, command: impl TryInto<Packet>) -> Option<()>;
}
impl UdpSocketExt for UdpSocket {
fn bind_connect(addr: impl ToSocketAddrs) -> std::io::Result<UdpSocket> {
let socket = UdpSocket::bind((Ipv4Addr::UNSPECIFIED, 0))?;
socket.connect(addr)?;
Ok(socket)
}
fn send_command(&self, command: impl TryInto<Packet>) -> Option<()> {
let packet = command.try_into().ok()?;
let vec: Vec<_> = packet.into();
self.send(&vec).ok()?;
Some(())
}
}
pub struct FakeConnection;
impl FakeConnection {
pub fn send_command(&self, command: impl TryInto<Packet>) -> Option<()> {
_ = self; let packet = command.try_into().ok()?;
drop(Vec::from(packet));
Some(())
}
}