feat: Make tftp serve callback async
This commit is contained in:
+13
-7
@@ -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");
|
||||
|
||||
Reference in New Issue
Block a user