server.poll(): doc, clean-up, pass smoltcp errors

This commit is contained in:
Astro 2019-03-18 21:16:33 +01:00
parent 38b7e1b871
commit 9af94e7616
1 changed files with 24 additions and 10 deletions

View File

@ -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,21 +75,34 @@ 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 { for handle in &self.handles {
let mut socket = self.sockets.get::<TcpSocket>(*handle); let mut socket = self.sockets.get::<TcpSocket>(*handle);
if ! socket.is_open() { if ! socket.is_open() {
socket.listen(TCP_PORT) let _ = socket.listen(TCP_PORT);
.unwrap();
} }
} }
} }
// Pass some smoltcp errors to the caller
match poll_error {
None => Ok(()),
Some(smoltcp::Error::Malformed) => Ok(()),
Some(smoltcp::Error::Unrecognized) => Ok(()),
Some(e) => Err(e),
}
}
} }
impl<'a, 's> fmt::Write for Server<'a, 's> { impl<'a, 's> fmt::Write for Server<'a, 's> {