|
@@ -9,7 +9,7 @@ these work. */
|
|
|
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
|
|
use tokio::net::TcpListener;
|
|
|
|
|
|
-pub async fn listen(addr: String, fun: fn(Vec<u8>) -> Vec<u8>) {
|
|
|
+pub async fn listen(addr: String, fun: &mut dyn FnMut(Vec<u8>) -> Vec<u8>) {
|
|
|
let listener = TcpListener::bind(&addr)
|
|
|
.await
|
|
|
.expect("Failed to create TcpListener");
|
|
@@ -19,7 +19,7 @@ pub async fn listen(addr: String, fun: fn(Vec<u8>) -> Vec<u8>) {
|
|
|
// asynchronously wait for an inbound socket
|
|
|
let (mut socket, _) = listener.accept().await.expect("Failed to create socket");
|
|
|
|
|
|
- tokio::spawn(async move {
|
|
|
+// tokio::spawn(async move {
|
|
|
loop {
|
|
|
// get number of bytes to receive
|
|
|
let mut nbuf: [u8; 8] = [0; 8];
|
|
@@ -30,7 +30,8 @@ pub async fn listen(addr: String, fun: fn(Vec<u8>) -> Vec<u8>) {
|
|
|
let n = usize::from_be_bytes(nbuf);
|
|
|
|
|
|
if n == 0 {
|
|
|
- return;
|
|
|
+ break;
|
|
|
+// return;
|
|
|
}
|
|
|
|
|
|
let mut buf = vec![0; n];
|
|
@@ -42,7 +43,8 @@ pub async fn listen(addr: String, fun: fn(Vec<u8>) -> Vec<u8>) {
|
|
|
.expect("Failed to read data from socket");
|
|
|
|
|
|
if n == 0 {
|
|
|
- return;
|
|
|
+ break;
|
|
|
+ // return;
|
|
|
}
|
|
|
|
|
|
let response = fun(buf);
|
|
@@ -60,6 +62,6 @@ pub async fn listen(addr: String, fun: fn(Vec<u8>) -> Vec<u8>) {
|
|
|
.await
|
|
|
.expect("Failed to write data to socket");
|
|
|
}
|
|
|
- });
|
|
|
+// });
|
|
|
}
|
|
|
}
|