summary refs log tree commit diff
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2019-07-29 19:00:48 -0400
committerJune McEnroe <june@causal.agency>2019-07-29 19:00:48 -0400
commit23017d58069822e4fd828f1d1908aa72cad34969 (patch)
tree564c7ae0693a3de05d3ad6e618ad64f50d385d06
parentDo basic multiplexing in ingest (diff)
downloadstream-23017d58069822e4fd828f1d1908aa72cad34969.tar.gz
stream-23017d58069822e4fd828f1d1908aa72cad34969.zip
Track only the max client fd
-rw-r--r--ingest.c53
1 files 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.
 		}
 	}