🏗️」 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> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <stdexcept>
#include <string>
#include <sys/poll.h>
#include <sys/socket.h>
#include <webserv.hpp>
@ -99,45 +100,45 @@ void Server::_run(void) {
continue;
}
for (std::vector<struct pollfd>::iterator it = _client_fds.begin();
it != _client_fds.end(); it++) {
struct sockaddr_in client_addr;
socklen_t addrlen = sizeof(client_addr);
int client_fd =
accept((*it).fd, (struct sockaddr *)&client_addr, &addrlen);
if (client_fd < 0) {
std::stringstream str;
str << "Accept failed: ";
str << strerror(errno);
_log->error(str.str());
continue;
}
if (fcntl(client_fd, F_SETFL, O_NONBLOCK) == -1) {
std::stringstream str;
str << "Failed to set non-blocking mode: ";
str << strerror(errno);
_log->error(str.str());
close(client_fd);
continue;
}
pollfd pfd;
pfd.fd = client_fd;
pfd.events = POLLIN | POLLOUT;
_client_fds.push_back(pfd);
_client_data.push_back(new sockaddr_in(client_addr));
}
for (size_t i = 0; i < _client_fds.size(); ++i) {
if (_client_fds[i].revents & POLLIN) {
if (_isServerSocket(_client_fds[i].fd)) {
struct sockaddr_in client_addr;
socklen_t addrlen = sizeof(client_addr);
int client_fd =
accept(_client_fds[i].fd,
(struct sockaddr *)&client_addr, &addrlen);
if (client_fd < 0) {
std::stringstream str;
str << "Accept failed: ";
str << strerror(errno);
_log->error(str.str());
continue;
}
if (fcntl(client_fd, F_SETFL, O_NONBLOCK) == -1) {
std::stringstream str;
str << "Failed to set non-blocking mode: ";
str << strerror(errno);
_log->error(str.str());
close(client_fd);
continue;
}
pollfd pfd;
pfd.fd = client_fd;
pfd.events = POLLIN;
_client_fds.push_back(pfd);
_client_data.push_back(new sockaddr_in(client_addr));
} else {
if (!_handle_client(_client_fds[i], _client_data[i])) {
close(_client_fds[i].fd);
_client_fds.erase(_client_fds.begin() + i);
delete _client_data[i];
_client_data.erase(_client_data.begin() + i);
i--;
}
if (!_handle_client(_client_fds[i], _client_data[i])) {
close(_client_fds[i].fd);
_client_fds.erase(_client_fds.begin() + i);
delete _client_data[i];
_client_data.erase(_client_data.begin() + i);
i--;
}
}
}

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <sys/socket.h>
using namespace webserv;
using namespace webserv::server;
bool convertStringToIP(const char *ip_str, struct in_addr *addr) {
// Split the IP string into four octets
@ -76,3 +76,19 @@ int Server::_createSocket(std::string host, int port) {
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;
}