🏗️」 wip: done _handle_client

This commit is contained in:
2025-04-21 11:08:11 +02:00
parent 25eef85b27
commit 773a54d7cd
3 changed files with 58 additions and 41 deletions

View File

@ -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/04/20 12:37:23 by adjoly ### ########.fr */ /* Updated: 2025/04/21 10:46:31 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View File

@ -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/04/20 18:29:00 by adjoly ### ########.fr */ /* Updated: 2025/04/20 18:41:04 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -22,6 +22,7 @@
#include <sstream> #include <sstream>
#include <stdexcept> #include <stdexcept>
#include <string> #include <string>
#include <sys/poll.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <webserv.hpp> #include <webserv.hpp>
@ -99,14 +100,12 @@ void Server::_run(void) {
continue; continue;
} }
for (size_t i = 0; i < _client_fds.size(); ++i) { for (std::vector<struct pollfd>::iterator it = _client_fds.begin();
if (_client_fds[i].revents & POLLIN) { it != _client_fds.end(); it++) {
if (_isServerSocket(_client_fds[i].fd)) {
struct sockaddr_in client_addr; struct sockaddr_in client_addr;
socklen_t addrlen = sizeof(client_addr); socklen_t addrlen = sizeof(client_addr);
int client_fd = int client_fd =
accept(_client_fds[i].fd, accept((*it).fd, (struct sockaddr *)&client_addr, &addrlen);
(struct sockaddr *)&client_addr, &addrlen);
if (client_fd < 0) { if (client_fd < 0) {
std::stringstream str; std::stringstream str;
@ -127,10 +126,13 @@ void Server::_run(void) {
pollfd pfd; pollfd pfd;
pfd.fd = client_fd; pfd.fd = client_fd;
pfd.events = POLLIN; pfd.events = POLLIN | POLLOUT;
_client_fds.push_back(pfd); _client_fds.push_back(pfd);
_client_data.push_back(new sockaddr_in(client_addr)); _client_data.push_back(new sockaddr_in(client_addr));
} else { }
for (size_t i = 0; i < _client_fds.size(); ++i) {
if (_client_fds[i].revents & POLLIN) {
if (!_handle_client(_client_fds[i], _client_data[i])) { if (!_handle_client(_client_fds[i], _client_data[i])) {
close(_client_fds[i].fd); close(_client_fds[i].fd);
_client_fds.erase(_client_fds.begin() + i); _client_fds.erase(_client_fds.begin() + i);
@ -142,7 +144,6 @@ void Server::_run(void) {
} }
} }
} }
}
Server::Server(config::Config *conf) : _conf(conf) { Server::Server(config::Config *conf) : _conf(conf) {
log("", "Server::Server", "config constructor called"); log("", "Server::Server", "config constructor called");

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/17 11:58:42 by adjoly #+# #+# */ /* Created: 2025/04/17 11:58:42 by adjoly #+# #+# */
/* Updated: 2025/04/20 17:31:41 by adjoly ### ########.fr */ /* Updated: 2025/04/21 10:53:45 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,7 +15,7 @@
#include <server/default.hpp> #include <server/default.hpp>
#include <sys/socket.h> #include <sys/socket.h>
using namespace webserv; using namespace webserv::server;
bool convertStringToIP(const char *ip_str, struct in_addr *addr) { bool convertStringToIP(const char *ip_str, struct in_addr *addr) {
// Split the IP string into four octets // Split the IP string into four octets
@ -76,3 +76,19 @@ int Server::_createSocket(std::string host, int port) {
return (fd); return (fd);
} }
bool Server::_handle_client(struct pollfd &pollfd, sockaddr_in *sock_data) {
Client *client;
try {
client = new Client(pollfd.fd, sock_data, _conf);
client->answer();
} catch (std::exception &e) {
_log->error(e.what());
return false;
}
delete client;
return true;
}