Serve command #27

Merged
Dreaded_X merged 20 commits from feature/tftp_side into main 2026-10-21 05:15:47 +00:00
Showing only changes of commit 5a54283248 - Show all commits
+13 -7
View File
@@ -28,11 +28,15 @@ pub enum Error {
Timeout,
}
pub async fn handle_connection(
pub async fn handle_connection<F, Fut>(
src: SocketAddr,
packet: Packet,
callback: impl (Fn(&str) -> Option<Vec<u8>>) + Send + Clone + 'static,
) -> Result<(), Error> {
callback: F,
) -> Result<(), Error>
where
F: Fn(&str) -> Fut,
Fut: Future<Output = Option<Vec<u8>>>,
{
let socket = UdpSocket::bind("127.0.0.1:0").await?;
println!(
"{src}: Connecting from {}",
@@ -50,7 +54,7 @@ pub async fn handle_connection(
let filename = request.filename();
println!("{src}: {filename} [{:?}]", request.mode());
let Some(data) = callback(filename) else {
let Some(data) = callback(filename).await else {
Packet::error(ErrorCode::FileNotFound, "Unknown file")
.send_to(&socket, &src)
.await?;
@@ -141,9 +145,11 @@ pub async fn handle_connection(
Ok(())
}
pub async fn serve(
callback: impl (Fn(&str) -> Option<Vec<u8>>) + Send + Clone + 'static,
) -> Result<!, Error> {
pub async fn serve<F, Fut>(callback: F) -> Result<!, Error>
where
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?;
println!("Server started");