」 feat: implemented succecfully that f*cking pfdmanager

This commit is contained in:
2025-05-27 18:04:21 +02:00
parent cccc25fba1
commit 050643814e
17 changed files with 294 additions and 109 deletions

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/17 11:12:41 by mmoussou #+# #+# */
/* Updated: 2025/05/22 17:36:36 by adjoly ### ########.fr */
/* Updated: 2025/05/27 16:48:09 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -19,8 +19,8 @@
using namespace webserv::server;
Client::Client(struct pollfd *pfd, config::Server *conf)
: _pfd(pfd), _conf(conf) {
Client::Client(int fd, config::Server *conf)
: _fd(fd), _conf(conf) {
_request = not_nullptr;
log("", "Client", "constructor called");
_response_done = false;
@ -28,7 +28,7 @@ Client::Client(struct pollfd *pfd, config::Server *conf)
Client::Client(const Client &cpy) {
if (this != &cpy) {
_pfd->fd = cpy._pfd->fd;
_fd = cpy._fd;
}
}
@ -39,7 +39,7 @@ void Client::parse(void) {
do {
std::memset(buffer, 0, BUFFER_SIZE);
bytes_received = recv(_pfd->fd, buffer, BUFFER_SIZE - 1, 0);
bytes_received = recv(_fd, buffer, BUFFER_SIZE - 1, 0);
if (bytes_received == -1) {
_log->error("failed to receive request");
throw std::runtime_error("failed to receive request");
@ -135,7 +135,7 @@ void Client::answer(void) {
_bytes_sent = 0;
}
ssize_t sent = send(_pfd->fd, _response_str.c_str() + _bytes_sent,
ssize_t sent = send(_fd, _response_str.c_str() + _bytes_sent,
_response_str.length() - _bytes_sent, 0);
if (sent == -1) {

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/11 16:11:40 by adjoly #+# #+# */
/* Updated: 2025/05/22 17:36:13 by adjoly ### ########.fr */
/* Updated: 2025/05/27 18:03:35 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -22,6 +22,7 @@
#include <netinet/in.h>
#include <poll.h>
#include <requests/default.hpp>
#include <server/PfdManager.hpp>
#include <server/default.hpp>
#include <sstream>
#include <stdexcept>
@ -29,8 +30,12 @@
#include <sys/socket.h>
#include <webserv.hpp>
using namespace webserv::server;
std::vector<struct pollfd> PfdManager::_pfd_vec;
std::vector<pfd_type> PfdManager::_pfd_type;
extern int _sig;
Client *Server::_getClient(int fd) {
@ -62,7 +67,7 @@ std::string getMethod(std::string &data) {
}
int Server::_fillHostsPorts(std::vector<std::string> &hosts,
std::vector<int> &ports) {
std::vector<int> & ports) {
std::vector<config::Server *> config = _conf->getServers();
for (auto it = range(config)) {
@ -101,13 +106,13 @@ void Server::_run(void) {
it != _fds_server.end(); it++) {
fd.fd = *it;
fd.events = POLLIN;
_client_fds.push_back(fd);
PfdManager::append(fd, SRV);
_log->debug("new socket in poll");
}
// to add signal instead of 727
while (727 - sigHandling()) {
if (poll(_client_fds.data(), _client_fds.size(), 5000) < 0) {
if (poll(PfdManager::data(), PfdManager::size(), 5000) < 0) {
if (errno == EINTR) {
continue;
}
@ -120,7 +125,7 @@ void Server::_run(void) {
size_t i = 0;
for (auto it = range(_fds_server), i++) {
if (_client_fds[i].revents & POLLIN) {
if (PfdManager::at(i).revents & POLLIN) {
struct sockaddr_in client_addr;
socklen_t addrlen = sizeof(client_addr);
int client_fd =
@ -150,10 +155,8 @@ void Server::_run(void) {
pfd.fd = client_fd;
pfd.events = POLLIN | POLLOUT;
pfd.revents = 0;
_client_fds.push_back(pfd);
struct pollfd *ppfd =
_client_fds.data() + _client_fds.size() - 1;
Client *new_client = new Client(ppfd, conf_srv);
PfdManager::append(pfd, CLIENT);
Client *new_client = new Client(pfd.fd, conf_srv);
if (new_client == NULL) {
continue;
}
@ -162,16 +165,16 @@ void Server::_run(void) {
}
}
for (size_t i = _fds_server.size(); i < _client_fds.size(); ++i) {
if (_client_fds[i].revents & POLLERR) {
for (size_t i = _fds_server.size(); i < PfdManager::size(); ++i) {
if (PfdManager::at(i).revents & POLLERR) {
_log->debug("pollerr");
close(_client_fds[i].fd);
_client_fds.erase(_client_fds.begin() + i);
close(PfdManager::at(i).fd);
PfdManager::remove(PfdManager::at(i).fd);
delete _client_data[i - _fds_server.size()];
_client_data.erase(_client_data.begin() + i);
} else if (_client_fds[i].revents & POLLIN) {
} else if (PfdManager::at(i).revents & POLLIN) {
_log->debug("pollin");
Client *client = _getClient(_client_fds[i].fd);
Client *client = _getClient(PfdManager::at(i).fd);
if (client == not_nullptr) {
_log->error("client does not exist");
continue;
@ -183,21 +186,24 @@ void Server::_run(void) {
_client_data.erase(std::find(_client_data.begin(),
_client_data.end(), client));
delete client;
for (auto it = range(_client_fds)) {
if (_client_fds[i].fd == (*it).fd) {
_log->debug("client fds erased");
close(it.base()->fd);
_client_fds.erase(it);
break;
}
}
// for (auto it = range(_client_fds)) {
// if (_client_fds[i].fd == (*it).fd) {
// _log->debug("client fds erased");
// close(it.base()->fd);
// _client_fds.erase(it);
// break;
// }
// }
close(PfdManager::at(i).fd);
PfdManager::remove(PfdManager::at(i).fd);
_log->debug("client removed");
i--;
}
} else if (_client_fds[i].revents & POLLOUT) {
} else if (PfdManager::at(i).revents & POLLOUT) {
std::stringstream str;
str << _client_fds[i].fd;
str << PfdManager::at(i).fd;
_log->debug("pollout = " + str.str());
Client *client = _getClient(_client_fds[i].fd);
Client *client = _getClient(PfdManager::at(i).fd);
if (client == not_nullptr) {
_log->error("client does not exist");
@ -213,14 +219,18 @@ void Server::_run(void) {
_client_data.erase(std::find(_client_data.begin(),
_client_data.end(), client));
delete client;
for (auto it = range(_client_fds)) {
if (_client_fds[i].fd == (*it).fd) {
_log->debug("client fds erased");
close(it.base()->fd);
_client_fds.erase(it);
break;
}
}
// for (auto it = range(_client_fds)) {
// if (_client_fds[i].fd == (*it).fd) {
// _log->debug("client fds erased");
// close(it.base()->fd);
// _client_fds.erase(it);
// break;
// }
// }
close(PfdManager::at(i).fd);
PfdManager::remove(PfdManager::at(i).fd);
_log->debug("client removed");
i--;
}
}
@ -241,7 +251,5 @@ Server::Server(config::Config *conf) : _conf(conf) {
Server::~Server(void) {
log("", "Server::Server", "destructor called");
for (auto it = range(_client_fds)) {
close(it->fd);
}
PfdManager::clear();
}