commit bf3edd3681ab0feb3f56b7dc5e5db40ec0bd38d5
parent 7b702ee5b1454f9fb64e84e8a8bd4e670132b2af
Author: Jacob R. Edwards <n/a>
Date: Tue, 11 Oct 2022 10:01:08 -0700
Poll server socket together with client sockets
Doing this dramatically increases responsiveness as file activity
is never caught waiting on another poll's timeout to expire.
Efficiency is also increased when there is no activity: previously
the whole event loop was re-executed periodically to check for
activity on different sockets even when there was no activity.
Diffstat:
2 files changed, 15 insertions(+), 13 deletions(-)
diff --git a/aps/aps.c b/aps/aps.c
@@ -64,8 +64,10 @@ aps_open(char *path, char **player)
aps_close(aps);
return NULL;
}
+ aps->pfds[aps->con->sock].fd = aps->con->sock;
+ aps->pfds[aps->con->sock].events = POLLIN;
- aps->timeout = 250;
+ aps->timeout = INFTIM;
return aps;
}
@@ -82,8 +84,8 @@ aps_close(struct aps *aps)
queue_remove(aps, aps->queue);
pfree(aps->player);
- for (i = 0; i < LEN(aps->pfds); ++i)
- if (aps->pfds[i].fd != -1)
+ for (i = 0; i < LEN(aps->clients); ++i)
+ if (aps->clients[i].request)
aps_drop(aps, i);
apclose(aps->con);
@@ -97,7 +99,9 @@ aps_drop(struct aps *aps, int fd)
--aps->nfds;
aps->pfds[fd].fd = -1;
buffree(aps->clients[fd].response);
+ aps->clients[fd].response = NULL;
buffree(aps->clients[fd].request);
+ aps->clients[fd].request = NULL;
return sclose(fd);
}
@@ -121,15 +125,6 @@ int
aps_accept(struct aps *aps)
{
int s;
- struct pollfd pfd;
- int r;
-
- pfd.events = POLLIN;
- pfd.fd = aps->con->sock;
-
- r = poll(&pfd, 1, aps->nfds ? 0 : INFTIM);
- if (r <= 0)
- return r;
while ((s = accept4(aps->con->sock, NULL, NULL, 0)) >= 0) {
if (aps_addfd(aps, s))
@@ -201,6 +196,13 @@ aps_update(struct aps *aps)
if ((re = poll(aps->pfds, LEN(aps->pfds), aps->timeout)) <= 0)
return re;
+ if (aps->pfds[aps->con->sock].revents) {
+ if ((aps->pfds[aps->con->sock].revents & POLLIN) && aps_accept(aps))
+ return 1;
+ aps->pfds[aps->con->sock].revents = 0;
+ --re;
+ }
+
for (i = 0; re > 0 && i < LEN(aps->pfds); ++i) {
if (aps->pfds[i].revents) {
if (aps_handle(aps, i))
diff --git a/aps/main.c b/aps/main.c
@@ -104,7 +104,7 @@ run(struct aps *aps)
if (aps->player->state == OFF)
pplay(aps->player, aps->queue->path);
}
- if ((aps_accept(aps) || aps_update(aps)) && errno != EINTR)
+ if (aps_update(aps) && errno != EINTR)
return 1;
}
return 0;