commit 8f329af6a91db849190497dd607ada73b45efb4b
parent 5a024de10c823cf05793cec1ad2f4084983e1fa6
Author: Jacob R. Edwards <n/a>
Date: Sun, 20 Feb 2022 23:24:25 -0800
Split aps_accept into two functions
Add a new function, aps_addfd, to add a file descriptor as a client.
This gets used by aps_accept which makes it easier to read, but
could also be used in any other context.
Diffstat:
1 file changed, 21 insertions(+), 6 deletions(-)
diff --git a/aps/aps.c b/aps/aps.c
@@ -100,6 +100,23 @@ aps_drop(struct aps *aps, int fd)
}
int
+aps_addfd(struct aps *aps, int fd)
+{
+ if ((aps->clients[fd].request = bufnew()) == NULL) {
+ buffree(aps->clients[fd].request);
+ return 1;
+ }
+ if (respnew(aps, fd)) {
+ respfree(aps, fd);
+ return 1;
+ }
+
+ ++aps->nfds;
+ aps->pfds[fd].fd = fd;
+ return 0;
+}
+
+int
aps_accept(struct aps *aps)
{
int s;
@@ -114,12 +131,10 @@ aps_accept(struct aps *aps)
return r;
while ((s = accept4(aps->con->sock, NULL, NULL, 0)) >= 0) {
- aps_log(aps, INFO, s, "client accepted");
- ++aps->nfds;
- aps->pfds[s].fd = s;
- if ((aps->clients[s].request = bufnew()) == NULL ||
- respnew(aps, s))
- aps_errordrop(aps, s, errstr);
+ if (aps_addfd(aps, s))
+ aps_log(aps, ERROR, s, "unable to add client");
+ else
+ aps_log(aps, INFO, s, "client added");
}
return errno != EWOULDBLOCK;