🏗️」 wip(server/_run): fixed _run command

This commit is contained in:
y-syo
2025-04-12 18:57:41 +02:00
parent c5d8abb3d6
commit f185a811d8
3 changed files with 41 additions and 14 deletions

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/11 17:45:43 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 * @param The number of the client fd
*/ */
void _handle_client(int); void _handle_client(int fd);
config::Server config::Server
*_conf; ///> Pointer to the configuration class (with all config in) *_conf; ///> Pointer to the configuration class (with all config in)

View File

@ -6,11 +6,12 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */ /* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/03 15:45:07 by mmoussou #+# #+# */ /* Created: 2025/02/03 15:45:07 by mmoussou #+# #+# */
/* Updated: 2025/04/11 17:51:33 by adjoly ### ########.fr */ /* Updated: 2025/04/12 15:48:12 by mmoussou ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include <config/Server.hpp> #include <config/Server.hpp>
#include <server/default.hpp>
#include <cstdlib> #include <cstdlib>
#include <exception> #include <exception>
#include <help.hpp> #include <help.hpp>
@ -37,5 +38,8 @@ int main(int ac, char **av) {
return 1; return 1;
} }
webserv::Server *serv = new webserv::Server(conf);
delete serv;
delete conf; delete conf;
} }

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/12 11:17:30 by adjoly ### ########.fr */ /* Updated: 2025/04/12 18:53:45 by mmoussou ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -20,6 +20,11 @@
using namespace webserv; using namespace webserv;
void Server::_handle_client(int fd)
{
(void) fd;
}
void Server::_setup(void) { void Server::_setup(void) {
_fd_server = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0); _fd_server = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
if (_fd_server == -1) if (_fd_server == -1)
@ -42,35 +47,53 @@ void Server::_setup(void) {
} }
void Server::_run(void) { void Server::_run(void) {
_client_fds[0].fd = _fd_server; struct pollfd fd;
_client_fds[0].events = POLLIN;
fd.fd = _fd_server;
fd.events = POLLIN;
_client_fds.clear();
_client_fds.push_back(fd);
int nbr_client = 0; int nbr_client = 0;
while (727) { while (727)
{
int ret = poll(_client_fds.data(), nbr_client + 1, -1); int ret = poll(_client_fds.data(), nbr_client + 1, -1);
if (ret < 0) { if (ret < 0)
{
close(_fd_server); close(_fd_server);
throw std::runtime_error("poll failed :("); throw std::runtime_error("poll failed :(");
} }
if (_client_fds[0].revents & POLLIN) { if (_client_fds[0].revents & POLLIN)
{
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(_fd_server, (struct sockaddr *)&client_addr, &addrlen);
accept(_fd_server, (struct sockaddr *)&client_addr, &addrlen); if (client_fd < 0)
if (client_fd < 0) { {
_log->error("accept failed"); _log->error("accept failed");
continue; 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 // if (nbr_client ) TODO do we need a max client probably not :D
} }
for (int i = 1; i <= nbr_client; ++i) { for (int i = 1; i <= nbr_client; )
if (_client_fds[i].revents & POLLIN) { {
if (_client_fds[i].revents & POLLIN)
{
_handle_client(i); _handle_client(i);
close(_client_fds[i].fd); close(_client_fds[i].fd);
_client_fds[i] = _client_fds[nbr_client--]; _client_fds[i] = _client_fds[nbr_client--];
_client_fds.pop_back();
} }
else
++i;
} }
} }
} }