feat: Make tftp serve callback async

This commit is contained in:
2026-10-04 05:53:42 +02:00
parent cae78a31a8
commit 5a54283248
+13 -7
View File
@@ -28,11 +28,15 @@ pub enum Error {
Timeout, Timeout,
} }
pub async fn handle_connection( pub async fn handle_connection<F, Fut>(
src: SocketAddr, src: SocketAddr,
packet: Packet, packet: Packet,
callback: impl (Fn(&str) -> Option<Vec<u8>>) + Send + Clone + 'static, callback: F,
) -> Result<(), Error> { ) -> Result<(), Error>
where
F: Fn(&str) -> Fut,
Fut: Future<Output = Option<Vec<u8>>>,
{
let socket = UdpSocket::bind("127.0.0.1:0").await?; let socket = UdpSocket::bind("127.0.0.1:0").await?;
println!( println!(
"{src}: Connecting from {}", "{src}: Connecting from {}",
@@ -50,7 +54,7 @@ pub async fn handle_connection(
let filename = request.filename(); let filename = request.filename();
println!("{src}: {filename} [{:?}]", request.mode()); println!("{src}: {filename} [{:?}]", request.mode());
let Some(data) = callback(filename) else { let Some(data) = callback(filename).await else {
Packet::error(ErrorCode::FileNotFound, "Unknown file") Packet::error(ErrorCode::FileNotFound, "Unknown file")
.send_to(&socket, &src) .send_to(&socket, &src)
.await?; .await?;
@@ -141,9 +145,11 @@ pub async fn handle_connection(
Ok(()) Ok(())
} }
pub async fn serve( pub async fn serve<F, Fut>(callback: F) -> Result<!, Error>
callback: impl (Fn(&str) -> Option<Vec<u8>>) + Send + Clone + 'static, where
) -> Result<!, Error> { F: Fn(&str) -> Fut + Clone + Send + 'static,
Fut: Future<Output = Option<Vec<u8>>> + Send + 'static,
{
let socket = UdpSocket::bind("127.0.0.1:69").await?; let socket = UdpSocket::bind("127.0.0.1:69").await?;
println!("Server started"); println!("Server started");