🏗️」 wip: added some macro for easier coding with iterator

This commit is contained in:
2025-04-22 11:41:05 +02:00
parent 6db9d66374
commit 1bd83e15b0
2 changed files with 15 additions and 22 deletions

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */ /* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/11 13:29:21 by mmoussou #+# #+# */ /* Created: 2025/02/11 13:29:21 by mmoussou #+# #+# */
/* Updated: 2025/04/14 14:25:35 by adjoly ### ########.fr */ /* Updated: 2025/04/21 11:33:14 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,6 +15,7 @@
#ifndef __WEBSERV_WEBSERV_HPP__ #ifndef __WEBSERV_WEBSERV_HPP__
# define __WEBSERV_WEBSERV_HPP__ # define __WEBSERV_WEBSERV_HPP__
#include <string> #include <string>
#include <cstring> #include <cstring>
#include <fstream> #include <fstream>
@ -25,6 +26,10 @@
#include <sys/socket.h> #include <sys/socket.h>
#include <netinet/in.h> #include <netinet/in.h>
#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 #define BUFFER_SIZE 4096
namespace webserv { namespace webserv {

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/11 16:11:40 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<std::string> &hosts, int Server::_fillHostsPorts(std::vector<std::string> &hosts,
std::vector<int> &ports) { std::vector<int> &ports) {
std::vector<config::Server *> *config = _conf->getServers(); std::vector<config::Server *> config = _conf->getServers();
for (std::vector<config::Server *>::iterator it = config->begin(); for (auto it = range(config)) {
it != config->end(); it++) {
hosts.push_back((*it)->getHost()); hosts.push_back((*it)->getHost());
ports.push_back((*it)->getPort()); ports.push_back((*it)->getPort());
} }
return config->size(); return config.size();
} }
void Server::_setup(void) { void Server::_setup(void) {
@ -67,11 +66,10 @@ void Server::_setup(void) {
if (size < 1) if (size < 1)
throw std::runtime_error("no server present in the config file"); throw std::runtime_error("no server present in the config file");
std::vector<std::string>::iterator itH = hosts.begin(); auto itH = hosts.begin();
for (std::vector<int>::iterator itP = ports.begin(); itP != ports.end(); for (auto it = range(ports), itH++) {
itP++, itH++) {
try { try {
int fd = _createSocket(*itH, *itP); int fd = _createSocket(*itH, *it);
_fds_server.push_back(fd); _fds_server.push_back(fd);
} catch (std::exception &e) { } catch (std::exception &e) {
throw e; throw e;
@ -100,12 +98,11 @@ void Server::_run(void) {
continue; continue;
} }
for (std::vector<struct pollfd>::iterator it = _client_fds.begin(); for (auto it = range(_fds_server)) {
it != _client_fds.end(); it++) {
struct sockaddr_in client_addr; struct sockaddr_in client_addr;
socklen_t addrlen = sizeof(client_addr); socklen_t addrlen = sizeof(client_addr);
int client_fd = int client_fd =
accept((*it).fd, (struct sockaddr *)&client_addr, &addrlen); accept((*it), (struct sockaddr *)&client_addr, &addrlen);
if (client_fd < 0) { if (client_fd < 0) {
std::stringstream str; std::stringstream str;
@ -115,15 +112,6 @@ void Server::_run(void) {
continue; 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; pollfd pfd;
pfd.fd = client_fd; pfd.fd = client_fd;
pfd.events = POLLIN | POLLOUT; pfd.events = POLLIN | POLLOUT;