🏗️」 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> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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)

View File

@ -6,11 +6,12 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <server/default.hpp>
#include <cstdlib>
#include <exception>
#include <help.hpp>
@ -37,5 +38,8 @@ int main(int ac, char **av) {
return 1;
}
webserv::Server *serv = new webserv::Server(conf);
delete serv;
delete conf;
}

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;
}
}
}