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> +#+ +:+ +#+ */
|
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/05/27 17:01:01 by adjoly #+# #+# */
|
/* 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 */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -82,8 +82,9 @@ class PfdManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct pollfd *data(void) { return _pfd_vec.data(); }
|
static struct pollfd * data(void) { return _pfd_vec.data(); }
|
||||||
static size_t size(void) { return _pfd_vec.size(); }
|
static size_t size(void) { return _pfd_vec.size(); }
|
||||||
|
static std::vector<struct pollfd> *vec(void) { return &_pfd_vec; }
|
||||||
|
|
||||||
static void clear(void) {
|
static void clear(void) {
|
||||||
for (auto it = range(_pfd_vec)) {
|
for (auto it = range(_pfd_vec)) {
|
||||||
|
@ -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/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 <fcntl.h>
|
||||||
#include <log.hpp>
|
#include <log.hpp>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <stdexcept>
|
|
||||||
#include <poll.h>
|
#include <poll.h>
|
||||||
|
#include <server/PfdManager.hpp>
|
||||||
|
#include <stdexcept>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <webserv.hpp>
|
||||||
|
|
||||||
namespace webserv {
|
namespace webserv {
|
||||||
namespace server {
|
namespace server {
|
||||||
@ -74,11 +76,21 @@ class Server {
|
|||||||
Client *_getClient(int);
|
Client *_getClient(int);
|
||||||
|
|
||||||
config::Config
|
config::Config
|
||||||
*_conf; // Pointer to the configuration class (with all config in)
|
* _conf; // Pointer to the configuration class (with all config in)
|
||||||
Logger *_log; // Pointer to the log class
|
Logger *_log; // Pointer to the log class
|
||||||
std::vector<int> _fds_server; // The fds of the sockets
|
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<struct pollfd> _client_fds; // A vector of all the poll fd
|
||||||
std::vector<Client *> _client_data; // vector of all the client sockaddr_in
|
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
|
}; // namespace server
|
||||||
|
@ -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/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 <algorithm>
|
||||||
#include <cerrno>
|
#include <cerrno>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
#include <cstddef>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <exception>
|
#include <exception>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
@ -30,11 +31,10 @@
|
|||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <webserv.hpp>
|
#include <webserv.hpp>
|
||||||
|
|
||||||
|
|
||||||
using namespace webserv::server;
|
using namespace webserv::server;
|
||||||
|
|
||||||
std::vector<struct pollfd> PfdManager::_pfd_vec;
|
std::vector<struct pollfd> PfdManager::_pfd_vec;
|
||||||
std::vector<pfd_type> PfdManager::_pfd_type;
|
std::vector<pfd_type> PfdManager::_pfd_type;
|
||||||
|
|
||||||
extern int _sig;
|
extern int _sig;
|
||||||
|
|
||||||
@ -123,116 +123,18 @@ void Server::_run(void) {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t i = 0;
|
for (size_t i = 0; i < PfdManager::size(); ++i) {
|
||||||
for (auto it = range(_fds_server), i++) {
|
pfd_type type = PfdManager::getType(PfdManager::at(i).fd);
|
||||||
if (PfdManager::at(i).revents & POLLIN) {
|
switch (type) {
|
||||||
struct sockaddr_in client_addr;
|
case SRV:
|
||||||
socklen_t addrlen = sizeof(client_addr);
|
_handle_srv(i);
|
||||||
int client_fd =
|
break;
|
||||||
accept((*it), (struct sockaddr *)&client_addr, &addrlen);
|
case CLIENT:
|
||||||
|
_handle_client(&i);
|
||||||
if (client_fd < 0) {
|
break;
|
||||||
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
case RES:
|
||||||
continue;
|
_log->warn("not handling resource for now");
|
||||||
}
|
break;
|
||||||
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--;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
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