Very primitive UDP receive

main
_ 2020-10-27 02:00:09 +00:00
parent a8caae68c3
commit 6bb2678eb5
1 changed files with 18 additions and 0 deletions

View File

@ -69,6 +69,21 @@ async fn handle_udp_send () -> Response <Body>
status_reply (StatusCode::OK, "ok\n")
}
async fn handle_udp_recv () -> Response <Body>
{
use tokio::net::UdpSocket;
let mut sock = UdpSocket::bind (SocketAddr::from (([0,0,0,0], 4001))).await.unwrap ();
let mut buffer = vec! [0u8; 4096];
let (bytes_received, _addr) = sock.recv_from (&mut buffer [..]).await.unwrap ();
buffer.truncate (bytes_received);
status_reply (StatusCode::OK, buffer)
}
async fn handle_all (req: Request <Body>, state: Arc <ServerState>)
-> Result <Response <Body>, Infallible>
{
@ -84,6 +99,9 @@ async fn handle_all (req: Request <Body>, state: Arc <ServerState>)
else if uri == "/udp_send" {
Ok (handle_udp_send ().await)
}
else if uri == "/udp_recv" {
Ok (handle_udp_recv ().await)
}
else {
Ok (status_reply (StatusCode::OK, "Hi\n"))
}