diff options
| -rw-r--r-- | src/event_loop.rs | 58 | ||||
| -rw-r--r-- | src/main.rs | 5 |
2 files changed, 30 insertions, 33 deletions
diff --git a/src/event_loop.rs b/src/event_loop.rs index 3b02624..ec0d65c 100644 --- a/src/event_loop.rs +++ b/src/event_loop.rs @@ -14,6 +14,7 @@ pub struct EventLoopCtx<'a> { pub state: &'a mut State, } +/// Simple callback-based event loop. Implemented using `poll`. pub struct EventLoop { pollfds: Vec<libc::pollfd>, cbs: HashMap<RawFd, Callback>, @@ -40,44 +41,41 @@ impl EventLoop { } pub fn run(&mut self, conn: &mut Connection<State>, state: &mut State) -> Result<()> { - if self.cbs.is_empty() { - return Ok(()); - } - - self.pollfds.clear(); - for &fd in self.cbs.keys() { - self.pollfds.push(libc::pollfd { - fd, - events: libc::POLLIN, - revents: 0, - }); - } + while !self.cbs.is_empty() { + self.pollfds.clear(); + for &fd in self.cbs.keys() { + self.pollfds.push(libc::pollfd { + fd, + events: libc::POLLIN, + revents: 0, + }); + } - loop { - let result = - unsafe { libc::poll(self.pollfds.as_mut_ptr(), self.pollfds.len() as _, -1) }; - if result == -1 { - let err = io::Error::last_os_error(); - if err.kind() == io::ErrorKind::Interrupted { - continue; + loop { + let result = + unsafe { libc::poll(self.pollfds.as_mut_ptr(), self.pollfds.len() as _, -1) }; + if result == -1 { + let err = io::Error::last_os_error(); + if err.kind() == io::ErrorKind::Interrupted { + continue; + } + return Err(err.into()); } - return Err(err.into()); + break; } - break; - } - for fd in &self.pollfds { - if fd.revents != 0 { - let mut cb = self.cbs.remove(&fd.fd).unwrap(); - match cb(EventLoopCtx { conn, state })? { - Action::Keep => { - self.cbs.insert(fd.fd, cb); + for fd in &self.pollfds { + if fd.revents != 0 { + let mut cb = self.cbs.remove(&fd.fd).unwrap(); + match cb(EventLoopCtx { conn, state })? { + Action::Keep => { + self.cbs.insert(fd.fd, cb); + } + Action::Unregister => (), } - Action::Unregister => (), } } } - Ok(()) } } diff --git a/src/main.rs b/src/main.rs index f27e076..e2395d7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -100,7 +100,6 @@ fn main() -> anyhow::Result<()> { }); } - loop { - el.run(&mut conn, &mut state)?; - } + el.run(&mut conn, &mut state)?; + unreachable!(); } |
