🔨」 fix: merge conflic on socket.cpp

This commit is contained in:
2025-04-20 13:30:10 +02:00
parent 5824985c93
commit bd19d78ac7
4 changed files with 77 additions and 26 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/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<int>::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<int> _fds_server; // The fds of the sockets
std::vector<struct pollfd> _client_fds; // A vector of all the poll fd
std::vector<sockaddr_in *>
_client_data; // vector of all the client sockaddr_in
};
}; // namespace webserv

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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<std::string, toml::ANode *> *node = table->getTable();
for (std::map<std::string, toml::ANode *>::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;

View File

@ -6,18 +6,21 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <cmath>
#include <fcntl.h>
#include <iterator>
#include <log.hpp>
#include <netinet/in.h>
#include <poll.h>
#include <requests/default.hpp>
#include <server/default.hpp>
#include <sstream>
#include <stdexcept>
#include <string>
#include <sys/socket.h>
#include <webserv.hpp>
@ -76,37 +79,66 @@ void Server::_setup(void) {
void Server::_run(void) {
struct pollfd fd;
int nbr_client = 0;
for (std::vector<int>::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) {
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);
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 ;
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]);
}
}
}

View File

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