From 1bd83e15b0851fc5e38b02b3d929b53ec53ff8a8 Mon Sep 17 00:00:00 2001 From: adjoly Date: Tue, 22 Apr 2025 11:41:05 +0200 Subject: [PATCH] =?UTF-8?q?=E3=80=8C=F0=9F=8F=97=EF=B8=8F=E3=80=8D=20wip:?= =?UTF-8?q?=20added=20some=20macro=20for=20easier=20coding=20with=20iterat?= =?UTF-8?q?or?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/webserv.hpp | 7 ++++++- src/server/Server.cpp | 30 +++++++++--------------------- 2 files changed, 15 insertions(+), 22 deletions(-) diff --git a/includes/webserv.hpp b/includes/webserv.hpp index 7fb6d3a..557b577 100644 --- a/includes/webserv.hpp +++ b/includes/webserv.hpp @@ -6,7 +6,7 @@ /* By: mmoussou #include #include @@ -25,6 +26,10 @@ #include #include +#define auto __auto_type +#define range(x) x.begin(); it != x.end(); it++ +#define prange(x) x->begin(); it != x->end(); it++ + #define BUFFER_SIZE 4096 namespace webserv { diff --git a/src/server/Server.cpp b/src/server/Server.cpp index 5e02319..1ca2443 100644 --- a/src/server/Server.cpp +++ b/src/server/Server.cpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/11 16:11:40 by adjoly #+# #+# */ -/* Updated: 2025/04/20 18:41:04 by adjoly ### ########.fr */ +/* Updated: 2025/04/22 10:51:15 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -49,14 +49,13 @@ std::string getMethod(std::string &data) { int Server::_fillHostsPorts(std::vector &hosts, std::vector &ports) { - std::vector *config = _conf->getServers(); + std::vector config = _conf->getServers(); - for (std::vector::iterator it = config->begin(); - it != config->end(); it++) { + for (auto it = range(config)) { hosts.push_back((*it)->getHost()); ports.push_back((*it)->getPort()); } - return config->size(); + return config.size(); } void Server::_setup(void) { @@ -67,11 +66,10 @@ void Server::_setup(void) { if (size < 1) throw std::runtime_error("no server present in the config file"); - std::vector::iterator itH = hosts.begin(); - for (std::vector::iterator itP = ports.begin(); itP != ports.end(); - itP++, itH++) { + auto itH = hosts.begin(); + for (auto it = range(ports), itH++) { try { - int fd = _createSocket(*itH, *itP); + int fd = _createSocket(*itH, *it); _fds_server.push_back(fd); } catch (std::exception &e) { throw e; @@ -100,12 +98,11 @@ void Server::_run(void) { continue; } - for (std::vector::iterator it = _client_fds.begin(); - it != _client_fds.end(); it++) { + for (auto it = range(_fds_server)) { struct sockaddr_in client_addr; socklen_t addrlen = sizeof(client_addr); int client_fd = - accept((*it).fd, (struct sockaddr *)&client_addr, &addrlen); + accept((*it), (struct sockaddr *)&client_addr, &addrlen); if (client_fd < 0) { std::stringstream str; @@ -115,15 +112,6 @@ void Server::_run(void) { continue; } - if (fcntl(client_fd, F_SETFL, O_NONBLOCK) == -1) { - std::stringstream str; - str << "Failed to set non-blocking mode: "; - str << strerror(errno); - _log->error(str.str()); - close(client_fd); - continue; - } - pollfd pfd; pfd.fd = client_fd; pfd.events = POLLIN | POLLOUT;