use super::Transport; use crate::encoding::Message; use anyhow::Result; use log::debug; use std::str::FromStr; use tokio::net::UdpSocket; pub struct UdpTransport { sock: UdpSocket, } impl UdpTransport { pub async fn new(sock: UdpSocket) -> Result { Ok(Self { sock }) } } impl Transport for UdpTransport { async fn recv(&self) -> Result { let mut buf = [0; 1024]; let size = self.sock.recv(&mut buf).await?; let message = String::from_utf8(buf[..size].to_vec())?; debug!("<- {message}"); Message::from_str(message.trim_end()) } async fn send(&self, request: Message) -> Result<()> { debug!("-> {request}"); self.sock.send(format!("{request}").as_bytes()).await?; Ok(()) } }