From 8bcb4a6880ad61aad019ec03a18dacc89e23ac46 Mon Sep 17 00:00:00 2001 From: nicolas-arnaud Date: Tue, 24 Jan 2023 14:57:03 +0100 Subject: [PATCH] update comments --- includes/Env.hpp | 1 - includes/Master.hpp | 9 +++++---- srcs/load/Env.cpp | 26 ++++++++++---------------- srcs/sock/Client.cpp | 4 ++++ srcs/sock/Master.cpp | 44 +++++++++++++++++++++----------------------- 5 files changed, 40 insertions(+), 44 deletions(-) diff --git a/includes/Env.hpp b/includes/Env.hpp index bdf534d..e942dcc 100644 --- a/includes/Env.hpp +++ b/includes/Env.hpp @@ -10,5 +10,4 @@ public: Env(JSONNode *conf); ~Env(void); void cycle(void); - void post_poll(void); }; diff --git a/includes/Master.hpp b/includes/Master.hpp index 3956e22..e42fc89 100644 --- a/includes/Master.hpp +++ b/includes/Master.hpp @@ -11,11 +11,12 @@ public: Master(ip_port_t listen); ~Master(void); - void post_poll(Env *env); + void check_socket(void); + void check_childs(Env *env); Server *choose_server(Env *env, string host); - ip_port_t _listen; - static int _poll_id_amount; - static int _first_cli_id; + ip_port_t _listen; + static int _poll_id_amount; + static int _first_cli_id; static struct pollfd *_pollfds; }; diff --git a/srcs/load/Env.cpp b/srcs/load/Env.cpp index 0a733f7..1677794 100644 --- a/srcs/load/Env.cpp +++ b/srcs/load/Env.cpp @@ -50,24 +50,18 @@ Env::~Env() { /** * @brief A Wevserv cycle: - * - append sockets to listen to select list - * - select ehem - * - refresh and handle requests + * - poll the Master::pollfds array + * - for each master socket, call his own post_poll method to check and handle the sockets flaged. */ void Env::cycle(void) { - if (!SILENT) cout << "|===||===| Waiting some HTTP request... |===||===|\n"; + if (!SILENT) cout << "|===||===| Waiting some HTTP request... |===||===|\n"; int pollResult = poll(Master::_pollfds, Master::_poll_id_amount + 1, 5000); if ((pollResult < 0) && (errno != EINTR)) std::cerr << "Select: " << strerror(errno) << "\n"; - if (pollResult > 0) post_poll(); -} - -/** - * @brief Refresh all master_sockets and their clients datas (disconnect, new - * connection, etc..) and parse requests recieved. - */ -void Env::post_poll() { - if (!SILENT) cout << "==> Handle requests and answers:\n"; - for (std::vector::iterator it = this->_masters.begin(); it < this->_masters.end(); it++) try { - (*it)->post_poll(this); - } catch (std::exception &e) { std::cerr << e.what(); } + if (pollResult > 0) { + if (!SILENT) cout << "==> Handle requests and answers:\n"; + for (std::vector::iterator it = this->_masters.begin(); it < this->_masters.end(); it++) try { + (*it)->check_socket(); + (*it)->check_childs(this); + } catch (std::exception &e) { std::cerr << e.what(); } + } } diff --git a/srcs/sock/Client.cpp b/srcs/sock/Client.cpp index 04e6e63..08ea4b1 100644 --- a/srcs/sock/Client.cpp +++ b/srcs/sock/Client.cpp @@ -22,6 +22,10 @@ Client::Client(int fd, ip_port_t ip_port, Master *parent) : _fd(fd), _ip_port(ip Client::~Client(void) { close(_fd); + Master::_pollfds[_poll_id].fd = 0; + Master::_pollfds[_poll_id].events = 0; + Master::_pollfds[_poll_id].revents = 0; + Master::_poll_id_amount--; _headers.clear(); if (!SILENT) cout << "Host disconnected, ip " << _ip_port.ip << ", port " << _ip_port.port << "\n"; } diff --git a/srcs/sock/Master.cpp b/srcs/sock/Master.cpp index cd38ee4..84ebdf3 100644 --- a/srcs/sock/Master.cpp +++ b/srcs/sock/Master.cpp @@ -19,7 +19,7 @@ Master::~Master(void) { /** * @brief Constructor * Try to create a socket listening to ip and port defined by input. - * If no exception if thrown, the creation success and the socket is then ready to select for new clients. + * If no exception if thrown, the creation success and the socket is then ready to listen for new clients. * * @param list An ip_port_t struct which contain the ip and the port the master listen. */ @@ -40,7 +40,7 @@ Master::Master(ip_port_t list) : _listen(list) { #ifdef __APPLE__ fcntl(socket, F_SETFL, O_NONBLOCK); #endif - if(!SILENT) cout << "New master socket with fd " << _fd << " which listen " << ip << ":" << port << "\n"; + if (!SILENT) cout << "New master socket with fd " << _fd << " which listen " << ip << ":" << port << "\n"; _pollfds[_poll_id_amount].fd = _fd; _pollfds[_poll_id_amount].events = POLLIN | POLLPRI; _poll_id = _poll_id_amount; @@ -48,15 +48,9 @@ Master::Master(ip_port_t list) : _listen(list) { } /** - * @brief Checkk master and his clients sockets after select performed. - * - First look for new clients - * - Then handle clients awaiting action: - * - Disconnect them if nothing's new on the socket (request altready handled) - * - Read and parse request else until it's ready to answer and does it. - * - * @param env The environment object which contain the liste of servers to know which one the client is trying to reach. + * @brief Check master and his clients sockets after poll performed. */ -void Master::post_poll(Env *env) { +void Master::check_socket(void) { int addrlen = sizeof(_address); if (_pollfds[_poll_id].revents & POLLIN) { /// < incomming master request @@ -75,40 +69,44 @@ void Master::post_poll(Env *env) { for (int i = _first_cli_id; i < MAX_CLIENTS; i++) { if (_pollfds[i].fd != 0) continue; _pollfds[i].fd = new_socket; - _pollfds[i].events = POLLIN | POLLPRI; + _pollfds[i].events = POLLIN | POLLPRI; new_cli->_poll_id = i; _poll_id_amount++; break; } } } +} + +/** + * @brief Handle master's childs. + * Loop along _childs + * if poll even incomming on the child socket: + * - give 1023 next sockets bits to Client methods to handle. + * - if Request is fully received, does the answer and flag the socket for outcomming event. + * - if request isn't fully received, flag the socket for incomming event. + * - if read returned 0, delete client. + * @param env The environment object. + */ +void Master::check_childs(Env *env) { int child_fd; for (std::vector::iterator it = _childs.begin(); it < _childs.end(); it++) { child_fd = (*it)->_fd; int i = (*it)->_poll_id; if (_pollfds[i].fd > 0 && _pollfds[i].revents & POLLIN) { - char buffer[128]; - int valread = read(child_fd, buffer, 127); + char buffer[1024]; + int valread = read(child_fd, buffer, 1023); buffer[valread] = '\0'; if (valread == 0) { - // getpeername(child_fd, (struct sockaddr *)&_address, (socklen_t *)&addrlen); delete (*it); _childs.erase(it); - _pollfds[i].fd = 0; - _pollfds[i].events = 0; - _pollfds[i].revents = 0; - _poll_id_amount--; } else if ((*it)->getRequest(env, buffer)) { (*it)->handleRequest(); - _pollfds[i].events = POLLOUT; + _pollfds[i].events = POLLOUT; if ((*it)->_finish) { delete (*it); _childs.erase(it); - _pollfds[i].fd = 0; - _pollfds[i].events = 0; - _pollfds[i].revents = 0; - _poll_id_amount--; } } else _pollfds[i].events = POLLIN | POLLPRI; }