aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorXavier Del Campo Romero <xavi.dcr@tutanota.com>2024-08-25 14:46:06 +0200
committerXavier Del Campo Romero <xavi.dcr@tutanota.com>2024-08-25 15:10:11 +0200
commit1eaab0bd503d4ca6d1b62082a0117447171d1bc8 (patch)
tree40aaa380addb17b8e55bee150a95d40143e15af2
parent199d801c835e95b54908b1a34963f65ba6ceee30 (diff)
server.c: Multiplex client events
Future commits would allow user-defined callbacks to write zero, one or more bytes to a file descriptor. If zero bytes were written, server_poll must avoid to always point to the same server_client, so that other requests from other server_client instances can still be handled.
-rw-r--r--server.c19
1 files changed, 15 insertions, 4 deletions
diff --git a/server.c b/server.c
index 4c19439..dec3080 100644
--- a/server.c
+++ b/server.c
@@ -13,7 +13,7 @@
struct server
{
- int fd, cfds[2];
+ int fd, cfds[2], lastfd;
struct server_client
{
@@ -267,6 +267,8 @@ again:
goto end;
}
+ struct server_client *sel = NULL;
+
for (struct {struct server_client *c; size_t j;}
_ = {.c = s->c, .j = CLIENTS}; _.c; _.c = _.c->next, _.j++)
{
@@ -274,12 +276,21 @@ again:
if (p->revents)
{
- *io = true;
- ret = _.c;
- goto end;
+ sel = _.c;
+
+ if (n_clients == 1 || p->fd != s->lastfd)
+ break;
}
}
+ if (sel)
+ {
+ *io = true;
+ ret = sel;
+ s->lastfd = sel->fd;
+ goto end;
+ }
+
fprintf(stderr, "%s: unlisted fd\n", __func__);
end: