parametrize poll return

This commit is contained in:
Robert Jördens 2019-06-03 17:08:21 +00:00
parent ee15c27dd9
commit a8e1740759

View File

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