commit bcce11eed363065c1910e1d65f6659ae1e1a7f29
parent 5e0d92c864eb7cd549c78a4f25e38988c6c719b9
Author: Jacob R. Edwards <jacobouno@protonmail.com>
Date: Mon, 31 May 2021 03:19:44 -0700
Fix server polling
Use a timeout with poll in aps_handle rather than aps_accept which
makes client handling quick and sleeps when inactive.
Diffstat:
M | aps.c | | | 23 | ++++++++++++++--------- |
1 file changed, 14 insertions(+), 9 deletions(-)
diff --git a/aps.c b/aps.c
@@ -80,7 +80,7 @@ aps_accept(struct aps *aps)
pfd.events = POLLIN;
pfd.fd = aps->con.s;
- r = poll(&pfd, 1, aps->nfds ? timeout : INFTIM);
+ r = poll(&pfd, 1, aps->nfds ? 0 : INFTIM);
if (r <= 0)
return r;
@@ -103,13 +103,18 @@ aps_drop(struct aps *aps, int fd)
int
aps_handle(struct aps *aps, int fd)
{
- if (aps->pfds[fd].revents & (POLLERR | POLLHUP | POLLNVAL))
- return 1;
- if (aps->pfds[fd].revents & POLLIN && aps_command(aps, fd))
- return 1;
- if (aps->pfds[fd].revents & POLLOUT && aps->clients[fd].response.lock &&
- resp_send(aps, fd))
+ if (aps->pfds[fd].revents & (POLLERR | POLLHUP | POLLNVAL)) {
return 1;
+ } else if (aps->pfds[fd].revents & POLLIN) {
+ if (aps_command(aps, fd))
+ return 1;
+ } else if (aps->pfds[fd].revents & POLLOUT) {
+ if (aps->clients[fd].response.lock && resp_send(aps, fd))
+ return 1;
+ } else {
+ die("unhandled poll revent");
+ }
+
return 0;
}
@@ -119,9 +124,9 @@ aps_update(struct aps *aps)
int i;
int re;
- if ((re = poll(aps->pfds, LEN(aps->pfds), INFTIM)) == -1)
+ if ((re = poll(aps->pfds, LEN(aps->pfds), timeout)) == -1)
return 1;
-
+
for (i = 0; re > 0 && i < LEN(aps->pfds); ++i) {
if (aps->pfds[i].revents && aps_handle(aps, i)) {
aps_drop(aps, i);