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/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,11 +31,10 @@
|
||||
#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;
|
||||
std::vector<pfd_type> PfdManager::_pfd_type;
|
||||
|
||||
extern int _sig;
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user