From 23017d58069822e4fd828f1d1908aa72cad34969 Mon Sep 17 00:00:00 2001 From: Curtis McEnroe Date: Mon, 29 Jul 2019 19:00:48 -0400 Subject: Track only the max client fd --- ingest.c | 53 +++++++++++++++++++++++++++-------------------------- 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/ingest.c b/ingest.c index d0b8bca..fba2435 100644 --- a/ingest.c +++ b/ingest.c @@ -33,22 +33,22 @@ int main(void) { // TODO: Read info from file. const char *path = "example.sock"; - int sock = socket(PF_LOCAL, SOCK_STREAM, 0); - if (sock < 0) err(EX_OSERR, "socket"); + int server = socket(PF_LOCAL, SOCK_STREAM, 0); + if (server < 0) err(EX_OSERR, "socket"); struct sockaddr_un addr = { .sun_family = AF_LOCAL }; strncpy(addr.sun_path, path, sizeof(addr.sun_path)); - error = bind(sock, (struct sockaddr *)&addr, SUN_LEN(&addr)); + error = bind(server, (struct sockaddr *)&addr, SUN_LEN(&addr)); if (error) err(EX_CANTCREAT, "%s", path); - error = listen(sock, 0); + error = listen(server, 0); if (error) err(EX_OSERR, "listen"); - uint32_t clients[8] = {0}; + int maxClient = server; struct pollfd fds[2] = { { .events = POLLIN, .fd = STDIN_FILENO }, - { .events = POLLIN, .fd = sock }, + { .events = POLLIN, .fd = server }, }; while (0 < poll(fds, 2, -1)) { if (fds[0].revents) { @@ -56,35 +56,36 @@ int main(void) { ssize_t rlen = read(STDIN_FILENO, buf, sizeof(buf)); if (rlen < 0) err(EX_IOERR, "read"); - // TODO: Update local terminal. - - for (int i = 0; i < 8; ++i) { - if (!clients[i]) continue; - for (int j = 0; j < 32; ++j) { - if (!(clients[i] & 1UL << j)) continue; - int fd = 32 * i + j; - - ssize_t wlen = write(fd, buf, rlen); - if (wlen < 0) { - close(fd); - clients[i] &= ~(1UL << j); - } + int client = server + 1; + while (client <= maxClient) { + ssize_t wlen = write(client, buf, rlen); + if (wlen == rlen) { + client++; + continue; + } + close(client); + if (client < maxClient) { + int fd = dup(maxClient); + assert(fd == client); + close(maxClient); } + maxClient--; } + + // TODO: Update local terminal. } if (fds[1].revents) { - int fd = accept(sock, NULL, NULL); - if (fd < 0) err(EX_IOERR, "accept"); - fcntl(fd, F_SETFL, O_NONBLOCK); + int client = accept(server, NULL, NULL); + if (client < 0) err(EX_IOERR, "accept"); + maxClient++; + assert(client == maxClient); int yes = 1; - error = setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &yes, sizeof(yes)); + fcntl(client, F_SETFL, O_NONBLOCK); + error = setsockopt(client, SOL_SOCKET, SO_NOSIGPIPE, &yes, sizeof(yes)); if (error) err(EX_IOERR, "setsockopt"); - assert(fd < 256); - clients[fd / 64] |= 1 << (fd % 64); - // TODO: Send snapshot. } } -- cgit 1.4.1