mirror of
https://github.com/KeyZox71/webserv.git
synced 2025-06-25 09:33:36 +02:00
「🏗️」 wip: working remade mainloop
This commit is contained in:
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/05/27 17:01:01 by adjoly #+# #+# */
|
||||
/* Updated: 2025/05/27 17:59:37 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/05/27 18:12:13 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -84,6 +84,7 @@ class PfdManager {
|
||||
|
||||
static struct pollfd * data(void) { return _pfd_vec.data(); }
|
||||
static size_t size(void) { return _pfd_vec.size(); }
|
||||
static std::vector<struct pollfd> *vec(void) { return &_pfd_vec; }
|
||||
|
||||
static void clear(void) {
|
||||
for (auto it = range(_pfd_vec)) {
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/11 17:45:43 by adjoly #+# #+# */
|
||||
/* Updated: 2025/05/27 17:43:21 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/05/27 18:38:04 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -16,9 +16,11 @@
|
||||
#include <fcntl.h>
|
||||
#include <log.hpp>
|
||||
#include <netinet/in.h>
|
||||
#include <stdexcept>
|
||||
#include <poll.h>
|
||||
#include <server/PfdManager.hpp>
|
||||
#include <stdexcept>
|
||||
#include <vector>
|
||||
#include <webserv.hpp>
|
||||
|
||||
namespace webserv {
|
||||
namespace server {
|
||||
@ -79,6 +81,16 @@ class Server {
|
||||
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<Client *> _client_data; // vector of all the client sockaddr_in
|
||||
|
||||
/**
|
||||
* @brief Can be used to handle a pollfd that is a server
|
||||
*/
|
||||
void _handle_srv(size_t i);
|
||||
|
||||
/**
|
||||
* @brief Can be used to handle pollfd taht is a client
|
||||
*/
|
||||
void _handle_client(size_t *i);
|
||||
};
|
||||
|
||||
}; // namespace server
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/11 16:11:40 by adjoly #+# #+# */
|
||||
/* Updated: 2025/05/27 18:03:35 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/05/27 18:39:00 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -14,6 +14,7 @@
|
||||
#include <algorithm>
|
||||
#include <cerrno>
|
||||
#include <cmath>
|
||||
#include <cstddef>
|
||||
#include <cstring>
|
||||
#include <exception>
|
||||
#include <fcntl.h>
|
||||
@ -30,7 +31,6 @@
|
||||
#include <sys/socket.h>
|
||||
#include <webserv.hpp>
|
||||
|
||||
|
||||
using namespace webserv::server;
|
||||
|
||||
std::vector<struct pollfd> PfdManager::_pfd_vec;
|
||||
@ -123,116 +123,18 @@ void Server::_run(void) {
|
||||
continue;
|
||||
}
|
||||
|
||||
size_t i = 0;
|
||||
for (auto it = range(_fds_server), i++) {
|
||||
if (PfdManager::at(i).revents & POLLIN) {
|
||||
struct sockaddr_in client_addr;
|
||||
socklen_t addrlen = sizeof(client_addr);
|
||||
int client_fd =
|
||||
accept((*it), (struct sockaddr *)&client_addr, &addrlen);
|
||||
|
||||
if (client_fd < 0) {
|
||||
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
||||
continue;
|
||||
}
|
||||
std::stringstream str;
|
||||
str << "Accept failed: ";
|
||||
str << strerror(errno);
|
||||
_log->error(str.str());
|
||||
continue;
|
||||
}
|
||||
config::Server *conf_srv = _conf->getServer(i);
|
||||
if (conf_srv == not_nullptr) {
|
||||
_log->warn(
|
||||
"where the f does he come from"); // does can't actually
|
||||
// happen but just in
|
||||
// case
|
||||
close(client_fd);
|
||||
continue;
|
||||
}
|
||||
|
||||
struct pollfd pfd;
|
||||
pfd.fd = client_fd;
|
||||
pfd.events = POLLIN | POLLOUT;
|
||||
pfd.revents = 0;
|
||||
PfdManager::append(pfd, CLIENT);
|
||||
Client *new_client = new Client(pfd.fd, conf_srv);
|
||||
if (new_client == NULL) {
|
||||
continue;
|
||||
}
|
||||
_client_data.push_back(new_client);
|
||||
_log->debug("client pushed");
|
||||
}
|
||||
}
|
||||
|
||||
for (size_t i = _fds_server.size(); i < PfdManager::size(); ++i) {
|
||||
if (PfdManager::at(i).revents & POLLERR) {
|
||||
_log->debug("pollerr");
|
||||
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 (PfdManager::at(i).revents & POLLIN) {
|
||||
_log->debug("pollin");
|
||||
Client *client = _getClient(PfdManager::at(i).fd);
|
||||
if (client == not_nullptr) {
|
||||
_log->error("client does not exist");
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
client->parse();
|
||||
} catch (std::exception &e) {
|
||||
_log->error(e.what());
|
||||
_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;
|
||||
// }
|
||||
// }
|
||||
close(PfdManager::at(i).fd);
|
||||
PfdManager::remove(PfdManager::at(i).fd);
|
||||
_log->debug("client removed");
|
||||
i--;
|
||||
}
|
||||
} else if (PfdManager::at(i).revents & POLLOUT) {
|
||||
std::stringstream str;
|
||||
str << PfdManager::at(i).fd;
|
||||
_log->debug("pollout = " + str.str());
|
||||
Client *client = _getClient(PfdManager::at(i).fd);
|
||||
|
||||
if (client == not_nullptr) {
|
||||
_log->error("client does not exist");
|
||||
continue;
|
||||
}
|
||||
if (client->requestParsed() == true &&
|
||||
!client->isReadyToClose()) {
|
||||
client->answer();
|
||||
continue;
|
||||
}
|
||||
|
||||
if (client->isReadyToClose()) {
|
||||
_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;
|
||||
// }
|
||||
// }
|
||||
close(PfdManager::at(i).fd);
|
||||
PfdManager::remove(PfdManager::at(i).fd);
|
||||
_log->debug("client removed");
|
||||
|
||||
i--;
|
||||
}
|
||||
for (size_t i = 0; i < PfdManager::size(); ++i) {
|
||||
pfd_type type = PfdManager::getType(PfdManager::at(i).fd);
|
||||
switch (type) {
|
||||
case SRV:
|
||||
_handle_srv(i);
|
||||
break;
|
||||
case CLIENT:
|
||||
_handle_client(&i);
|
||||
break;
|
||||
case RES:
|
||||
_log->warn("not handling resource for now");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
112
src/server/ServerHandle.cpp
Normal file
112
src/server/ServerHandle.cpp
Normal file
@ -0,0 +1,112 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ServerHandle.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/05/27 18:22:48 by adjoly #+# #+# */
|
||||
/* Updated: 2025/05/27 18:38:28 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <cstddef>
|
||||
#include <iterator>
|
||||
#include <server/default.hpp>
|
||||
|
||||
using namespace webserv::server;
|
||||
|
||||
void Server::_handle_srv(size_t i) {
|
||||
if (PfdManager::at(i).revents & POLLIN) {
|
||||
struct sockaddr_in client_addr;
|
||||
socklen_t addrlen = sizeof(client_addr);
|
||||
int client_fd = accept(PfdManager::at(i).fd,
|
||||
(struct sockaddr *)&client_addr, &addrlen);
|
||||
|
||||
if (client_fd < 0) {
|
||||
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
||||
return;
|
||||
}
|
||||
std::stringstream str;
|
||||
str << "Accept failed: ";
|
||||
str << strerror(errno);
|
||||
_log->error(str.str());
|
||||
return;
|
||||
}
|
||||
config::Server *conf_srv = _conf->getServer(i);
|
||||
if (conf_srv == not_nullptr) {
|
||||
_log->warn("where the f does he come from"); // does can't
|
||||
// actually happen
|
||||
// but just in case
|
||||
close(client_fd);
|
||||
return;
|
||||
}
|
||||
|
||||
struct pollfd pfd;
|
||||
pfd.fd = client_fd;
|
||||
pfd.events = POLLIN | POLLOUT;
|
||||
pfd.revents = 0;
|
||||
PfdManager::append(pfd, CLIENT);
|
||||
Client *new_client = new Client(pfd.fd, conf_srv);
|
||||
if (new_client == NULL) {
|
||||
return;
|
||||
}
|
||||
_client_data.push_back(new_client);
|
||||
_log->debug("client pushed");
|
||||
}
|
||||
}
|
||||
|
||||
void Server::_handle_client(size_t *i) {
|
||||
if (PfdManager::getType(PfdManager::at(*i).fd)) {
|
||||
if (PfdManager::at(*i).revents & POLLERR) {
|
||||
_log->debug("pollerr");
|
||||
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 (PfdManager::at(*i).revents & POLLIN) {
|
||||
_log->debug("pollin");
|
||||
Client *client = _getClient(PfdManager::at(*i).fd);
|
||||
if (client == not_nullptr) {
|
||||
_log->error("client does not exist");
|
||||
return ;
|
||||
}
|
||||
try {
|
||||
client->parse();
|
||||
} catch (std::exception &e) {
|
||||
_log->error(e.what());
|
||||
_client_data.erase(std::find(_client_data.begin(),
|
||||
_client_data.end(), client));
|
||||
delete client;
|
||||
close(PfdManager::at(*i).fd);
|
||||
PfdManager::remove(PfdManager::at(*i).fd);
|
||||
_log->debug("client removed");
|
||||
i--;
|
||||
}
|
||||
} else if (PfdManager::at(*i).revents & POLLOUT) {
|
||||
std::stringstream str;
|
||||
str << PfdManager::at(*i).fd;
|
||||
_log->debug("pollout = " + str.str());
|
||||
Client *client = _getClient(PfdManager::at(*i).fd);
|
||||
|
||||
if (client == not_nullptr) {
|
||||
_log->error("client does not exist");
|
||||
return;
|
||||
}
|
||||
if (client->requestParsed() == true && !client->isReadyToClose()) {
|
||||
client->answer();
|
||||
return;
|
||||
}
|
||||
|
||||
if (client->isReadyToClose()) {
|
||||
_client_data.erase(std::find(_client_data.begin(),
|
||||
_client_data.end(), client));
|
||||
delete client;
|
||||
close(PfdManager::at(*i).fd);
|
||||
PfdManager::remove(PfdManager::at(*i).fd);
|
||||
_log->debug("client removed");
|
||||
i--;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user