From bd19d78ac7bc6653d0d6dbac108b0e97ceef0063 Mon Sep 17 00:00:00 2001 From: adjoly Date: Sun, 20 Apr 2025 13:30:10 +0200 Subject: [PATCH] =?UTF-8?q?=E3=80=8C=F0=9F=94=A8=E3=80=8D=20fix:=20merge?= =?UTF-8?q?=20conflic=20on=20socket.cpp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/server/Server.hpp | 28 ++++++++++++++-- src/config/Config.cpp | 4 +-- src/server/Server.cpp | 66 ++++++++++++++++++++++++++++---------- src/server/Socket.cpp | 5 +-- 4 files changed, 77 insertions(+), 26 deletions(-) diff --git a/includes/server/Server.hpp b/includes/server/Server.hpp index d8fa556..ef1b43b 100644 --- a/includes/server/Server.hpp +++ b/includes/server/Server.hpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/11 17:45:43 by adjoly #+# #+# */ -/* Updated: 2025/04/17 18:45:07 by adjoly ### ########.fr */ +/* Updated: 2025/04/20 12:37:23 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,6 +22,11 @@ namespace webserv { +struct client_data { + sockaddr_in sock_data; + pollfd poll_fd; +}; + class Server { public: Server(config::Config *); @@ -43,7 +48,7 @@ class Server { * * @param The fd of the client */ - void _handle_client(int, sockaddr_in, struct pollfd); + bool _handle_client(struct pollfd &, sockaddr_in *); /** * @brief Can be used to fill the vector passed as parameters with all the @@ -60,11 +65,28 @@ class Server { */ int _createSocket(std::string, int); + /** + * @brief Can be used to check if an fd is one of the socket or not + * + * @param the fd to check + */ + bool _isServerSocket(int fd) { + for (std::vector::iterator it = _fds_server.begin(); + it != _fds_server.end(); it++) { + if (fd == *it) { + return true; + } + } + return false; + } + config::Config *_conf; // Pointer to the configuration class (with all config in) Logger *_log; // Pointer to the log class - std::vector _fds_server; // The fds of the sockets + std::vector _fds_server; // The fds of the sockets std::vector _client_fds; // A vector of all the poll fd + std::vector + _client_data; // vector of all the client sockaddr_in }; }; // namespace webserv diff --git a/src/config/Config.cpp b/src/config/Config.cpp index f460309..e0b8fbf 100644 --- a/src/config/Config.cpp +++ b/src/config/Config.cpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/14 12:53:54 by adjoly #+# #+# */ -/* Updated: 2025/04/18 09:57:01 by adjoly ### ########.fr */ +/* Updated: 2025/04/18 10:21:36 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -32,7 +32,7 @@ Config::Config(std::string &filename) { std::map *node = table->getTable(); for (std::map::iterator it = node->begin(); it != node->end(); it++) { - Server *srv = new Server(it->second, _log); + Server *srv = new Server(it->second); _servers->push_back(srv); } delete table; diff --git a/src/server/Server.cpp b/src/server/Server.cpp index 212e7b0..6e14275 100644 --- a/src/server/Server.cpp +++ b/src/server/Server.cpp @@ -6,18 +6,21 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/11 16:11:40 by adjoly #+# #+# */ -/* Updated: 2025/04/18 09:19:14 by adjoly ### ########.fr */ +/* Updated: 2025/04/20 13:00:33 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #include +#include #include #include #include #include #include #include +#include #include +#include #include #include @@ -76,37 +79,66 @@ void Server::_setup(void) { void Server::_run(void) { struct pollfd fd; - int nbr_client = 0; for (std::vector::iterator it = _fds_server.begin(); - it != _fds_server.end(); it++, nbr_client++) { + it != _fds_server.end(); it++) { fd.fd = *it; fd.events = POLLIN; _client_fds.push_back(fd); + _client_data.push_back(NULL); } // to add signal instead of 727 while (727) { - int ret = poll(_client_fds.data(), nbr_client, -1); + int ret = poll(_client_fds.data(), _client_fds.size(), -1); if (ret < 0) { - _log->error("poll failed :("); + std::stringstream str; + str << "poll failed : "; + str << strerror(errno); + _log->error(str.str()); continue; } - for (int i = 0; i < nbr_client; i++) { + for (size_t i = 0; i < _client_fds.size(); ++i) { if (_client_fds[i].revents & POLLIN) { - struct sockaddr_in client_addr; - socklen_t addrlen = sizeof(client_addr); - int client_fd = - accept(_client_fds[i].fd, (struct sockaddr *)&client_addr, - &addrlen); - if (client_fd < 0) { - _log->error("accept failed with : " + - convertIPToString(&client_addr.sin_addr) + ":" + - convertPortToString(&client_addr)); - continue ; + if (_isServerSocket(_client_fds[i].fd)) { + struct sockaddr_in client_addr; + socklen_t addrlen = sizeof(client_addr); + int client_fd = + accept(_client_fds[i].fd, + (struct sockaddr *)&client_addr, &addrlen); + + if (client_fd < 0) { + std::stringstream str; + str << "Accept failed: "; + str << strerror(errno); + _log->error(str.str()); + 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; + _client_fds.push_back(pfd); + _client_data.push_back(new sockaddr_in(client_addr)); + } else { + if (!_handle_client(_client_fds[i], _client_data[i])) { + close(_client_fds[i].fd); + _client_fds.erase(_client_fds.begin() + i); + delete _client_data[i]; + _client_data.erase(_client_data.begin() + i); + i--; + } } - _handle_client(client_fd, client_addr, _conf, _client_fds[i]); } } } diff --git a/src/server/Socket.cpp b/src/server/Socket.cpp index 3538eee..ff58107 100644 --- a/src/server/Socket.cpp +++ b/src/server/Socket.cpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/17 11:58:42 by adjoly #+# #+# */ -/* Updated: 2025/04/20 10:58:26 by mmoussou ### ########.fr */ +/* Updated: 2025/04/20 13:29:53 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -77,6 +77,3 @@ int Server::_createSocket(std::string host, int port) { return (fd); } -void Server::_handle_client(int fd, sockaddr_in client_addr, struct pollfd poll_fd) { - server::Client *client = new server::Client(fd, client_addr, NULL); -}