/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* loop.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: bcausseq +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/09/20 04:18:29 by bcausseq #+# #+# */ /* Updated: 2026/09/25 23:53:53 by bcausseq ### ########.fr */ /* */ /* ************************************************************************** */ #include "config/config.hpp" #include "server/Server.hpp" #include "loop.hpp" #include #include #include #include #include static void log(std::string message) { std::cout << "[main] " << message << std::endl; } bool running = true; static void sig(int tasoeur) { std::stringstream ss; ss << "Server interupted cause of "; switch (tasoeur) { case 2: ss << "SIGINT\n"; break; case 3: ss << "SIGQUIT\n"; break; } log(ss.str()); running = false; } static inline bool is_invalid(pollfd &pfd) { return pfd.revents & (POLLHUP | POLLERR); } void run(std::vector& pollables) { signal(SIGINT, sig); signal(SIGQUIT, sig); std::vector pfds; while (running) { pfds.clear(); for (auto it = pollables.begin(); it != pollables.end(); ++it) { pollfd pfd; pfd.fd = (*it)->get_fd(); pfd.events = POLLIN | POLLOUT; pfd.revents = 0; pfds.push_back(pfd); } int ret = poll(&pfds[0], pfds.size(), -1); if (ret <= 0) continue; for (size_t i = 0; i < pfds.size(); ++i) { if (!is_invalid(pfds[i]) && pfds[i].revents & (POLLIN)) { pollables[i]->receive(pollables); } } for (size_t i = 0; i < pfds.size(); ++i) { if (!is_invalid(pfds[i]) && pfds[i].revents & (POLLOUT)) { pollables[i]->send(); } } for (size_t i = 0; i < pfds.size(); ++i) { Pollable *p = pollables[i]; if (is_invalid(pfds[i]) || p->should_disconnect()) { std::stringstream ss; ss << "Disconnecting FD: " << p->get_fd(); log(ss.str()); p->disconnect(); pollables.erase(pollables.begin() + i); delete p; } } } }