forked from M-Labs/thermostat
server.poll(): doc, clean-up, pass smoltcp errors
This commit is contained in:
parent
38b7e1b871
commit
9af94e7616
|
@ -38,6 +38,7 @@ pub struct Server<'a, 'b> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, 'b> Server<'a, 'b> {
|
impl<'a, 'b> Server<'a, 'b> {
|
||||||
|
/// Run a server with stack-allocated sockets
|
||||||
pub fn run<F>(net: EthernetInterface<'a, 'a, 'a, &'a mut stm32_eth::Eth<'static, 'static>>, f: F)
|
pub fn run<F>(net: EthernetInterface<'a, 'a, 'a, &'a mut stm32_eth::Eth<'static, 'static>>, f: F)
|
||||||
where
|
where
|
||||||
F: FnOnce(&mut Server<'a, '_>),
|
F: FnOnce(&mut Server<'a, '_>),
|
||||||
|
@ -74,19 +75,32 @@ impl<'a, 'b> Server<'a, 'b> {
|
||||||
f(&mut server);
|
f(&mut server);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn poll(&mut self, now: Instant) {
|
/// Poll the interface and the sockets
|
||||||
|
pub fn poll(&mut self, now: Instant) -> Result<(), smoltcp::Error> {
|
||||||
|
// Poll smoltcp EthernetInterface
|
||||||
|
let mut poll_error = None;
|
||||||
let activity = self.net.poll(&mut self.sockets, now)
|
let activity = self.net.poll(&mut self.sockets, now)
|
||||||
.unwrap_or(true);
|
.unwrap_or_else(|e| {
|
||||||
if ! activity {
|
poll_error = Some(e);
|
||||||
return;
|
true
|
||||||
|
});
|
||||||
|
|
||||||
|
if activity {
|
||||||
|
// Listen on all sockets
|
||||||
|
for handle in &self.handles {
|
||||||
|
let mut socket = self.sockets.get::<TcpSocket>(*handle);
|
||||||
|
if ! socket.is_open() {
|
||||||
|
let _ = socket.listen(TCP_PORT);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for handle in &self.handles {
|
// Pass some smoltcp errors to the caller
|
||||||
let mut socket = self.sockets.get::<TcpSocket>(*handle);
|
match poll_error {
|
||||||
if ! socket.is_open() {
|
None => Ok(()),
|
||||||
socket.listen(TCP_PORT)
|
Some(smoltcp::Error::Malformed) => Ok(()),
|
||||||
.unwrap();
|
Some(smoltcp::Error::Unrecognized) => Ok(()),
|
||||||
}
|
Some(e) => Err(e),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue