Something went wrong. Try again.
An HTTP server written in C++98 projects.intra.42.fr/projects/webserv
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106/* ************************************************************************** *//* *//* ::: :::::::: *//* loop.cpp :+: :+: :+: *//* +:+ +:+ +:+ *//* By: bcausseq <bcausseq@42angouleme.fr> +#+ +:+ +#+ *//* +#+#+#+#+#+ +#+ *//* 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 <csignal>#include <iostream>#include <sstream>#include <poll.h>#include <vector>
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<Pollable*>& pollables){ signal(SIGINT, sig); signal(SIGQUIT, sig);
std::vector<pollfd> 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; } } }}