diff --git a/wind/main.cpp b/wind/main.cpp index 943cf93c..39e51aff 100644 --- a/wind/main.cpp +++ b/wind/main.cpp @@ -87,6 +87,10 @@ Result luna_main(int argc, char** argv) TRY(make(ui::Rect { 600, 130, 350, 250 }, ui::CYAN, "File Manager"_sv)); Vector clients; + Vector fds; + TRY(fds.try_append({ .fd = mouse->fd(), .events = POLLIN, .revents = 0 })); + TRY(fds.try_append({ .fd = keyboard->fd(), .events = POLLIN, .revents = 0 })); + TRY(fds.try_append({ .fd = server->fd(), .events = POLLIN, .revents = 0 })); TRY(os::Security::pledge("stdio rpath unix", NULL)); @@ -97,13 +101,9 @@ Result luna_main(int argc, char** argv) mouse_pointer.draw(screen.canvas()); screen.sync(); - struct pollfd fds[] = { - { .fd = mouse->fd(), .events = POLLIN, .revents = 0 }, - { .fd = keyboard->fd(), .events = POLLIN, .revents = 0 }, - { .fd = server->fd(), .events = POLLIN, .revents = 0 }, - }; + for (auto& pfd : fds) { pfd.revents = 0; } - int rc = poll(fds, 3, 1000); + int rc = poll(fds.data(), fds.size(), 1000); if (!rc) continue; if (rc < 0 && errno != EINTR) { os::println("poll: error: %s", strerror(errno)); } @@ -126,10 +126,22 @@ Result luna_main(int argc, char** argv) strerror(packet.key))); } } + for (usize i = 0; i < clients.size(); i++) + { + if (fds[i + 3].revents & POLLIN) os::println("wind: Client %d sent data!", i); + if (fds[i + 3].revents & POLLHUP) + { + os::println("wind: Client %d disconnected", i); + fds.remove_at(i + 3); + auto client = clients.remove_at(i); + client.disconnect(); + } + } if (fds[2].revents & POLLIN) { auto client = TRY(server->accept()); os::println("wind: New client connected!"); + TRY(fds.try_append({ .fd = client.fd(), .events = POLLIN, .revents = 0 })); TRY(clients.try_append(move(client))); } }