diff --git a/src/main.rs b/src/main.rs index 28b19bb..a0ee5e5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -797,10 +797,10 @@ impl Server { Self { data: Vec::new(), discard: false } } - fn poll(&mut self, socket: &mut net::socket::TcpSocket, f: F) + fn poll(&mut self, socket: &mut net::socket::TcpSocket, f: F) -> Option where T: DeserializeOwned, - F: FnOnce(&T), + F: FnOnce(&T) -> R, { while socket.can_recv() { let found = socket.recv(|buf| { @@ -820,14 +820,15 @@ impl Server { if self.discard { self.discard = false; json_reply(socket, &Response { code: 520, message: "command buffer overflow" }); + self.data.clear(); } else { let r = from_slice::(&self.data); self.data.clear(); match r { Ok(res) => { - f(&res); - json_reply(socket, &Response{ code: 200, message: "ok" }); - return; + let r = f(&res); + json_reply(socket, &Response { code: 200, message: "ok" }); + return Some(r); }, Err(err) => { warn!("parse error {:?}", err); @@ -837,6 +838,7 @@ impl Server { } } } + None } }