From f185a811d8bfb1b5b88718ab173307a91f2bd061 Mon Sep 17 00:00:00 2001 From: y-syo Date: Sat, 12 Apr 2025 18:57:41 +0200 Subject: [PATCH] =?UTF-8?q?=E3=80=8C=F0=9F=8F=97=EF=B8=8F=E3=80=8D=20wip(s?= =?UTF-8?q?erver/=5Frun):=20fixed=20=5Frun=20command?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/server/server.hpp | 4 ++-- src/main.cpp | 6 ++++- src/server.cpp | 45 ++++++++++++++++++++++++++++---------- 3 files changed, 41 insertions(+), 14 deletions(-) diff --git a/includes/server/server.hpp b/includes/server/server.hpp index d528979..b15dab2 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/11 19:22:43 by adjoly ### ########.fr */ +/* Updated: 2025/04/12 15:39:14 by mmoussou ### ########.fr */ /* */ /* ************************************************************************** */ @@ -42,7 +42,7 @@ class Server { * * @param The number of the client fd */ - void _handle_client(int); + void _handle_client(int fd); config::Server *_conf; ///> Pointer to the configuration class (with all config in) diff --git a/src/main.cpp b/src/main.cpp index 983fd62..77512a2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,11 +6,12 @@ /* By: mmoussou +#include #include #include #include @@ -37,5 +38,8 @@ int main(int ac, char **av) { return 1; } + webserv::Server *serv = new webserv::Server(conf); + + delete serv; delete conf; } diff --git a/src/server.cpp b/src/server.cpp index b2bcaa7..b405c4b 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/11 16:11:40 by adjoly #+# #+# */ -/* Updated: 2025/04/12 11:17:30 by adjoly ### ########.fr */ +/* Updated: 2025/04/12 18:53:45 by mmoussou ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,6 +20,11 @@ using namespace webserv; +void Server::_handle_client(int fd) +{ + (void) fd; +} + void Server::_setup(void) { _fd_server = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0); if (_fd_server == -1) @@ -42,35 +47,53 @@ void Server::_setup(void) { } void Server::_run(void) { - _client_fds[0].fd = _fd_server; - _client_fds[0].events = POLLIN; + struct pollfd fd; + + fd.fd = _fd_server; + fd.events = POLLIN; + _client_fds.clear(); + _client_fds.push_back(fd); int nbr_client = 0; - while (727) { + while (727) + { int ret = poll(_client_fds.data(), nbr_client + 1, -1); - if (ret < 0) { + if (ret < 0) + { close(_fd_server); throw std::runtime_error("poll failed :("); } - if (_client_fds[0].revents & POLLIN) { + if (_client_fds[0].revents & POLLIN) + { struct sockaddr_in client_addr; socklen_t addrlen = sizeof(client_addr); - int client_fd = - accept(_fd_server, (struct sockaddr *)&client_addr, &addrlen); - if (client_fd < 0) { + int client_fd = accept(_fd_server, (struct sockaddr *)&client_addr, &addrlen); + if (client_fd < 0) + { _log->error("accept failed"); continue; } + struct pollfd client_pollfd; + client_pollfd.fd = client_fd; + client_pollfd.events = POLLIN; + _client_fds.push_back(client_pollfd); + ++nbr_client; + // if (nbr_client ) TODO do we need a max client probably not :D } - for (int i = 1; i <= nbr_client; ++i) { - if (_client_fds[i].revents & POLLIN) { + for (int i = 1; i <= nbr_client; ) + { + if (_client_fds[i].revents & POLLIN) + { _handle_client(i); close(_client_fds[i].fd); _client_fds[i] = _client_fds[nbr_client--]; + _client_fds.pop_back(); } + else + ++i; } } }