Files
webserv/src/server.cpp

92 lines
2.8 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* server.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/11 16:11:40 by adjoly #+# #+# */
/* Updated: 2025/04/12 11:17:30 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include <cmath>
#include <log.hpp>
#include <netinet/in.h>
#include <server/default.hpp>
#include <stdexcept>
#include <sys/socket.h>
#include <webserv.hpp>
using namespace webserv;
void Server::_setup(void) {
_fd_server = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
if (_fd_server == -1)
throw std::runtime_error("error at socket setup");
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = htons(_conf->getPort());
if (bind(_fd_server, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
close(_fd_server);
throw std::runtime_error("error when binding address");
}
if (listen(_fd_server, SOMAXCONN) < 0) {
close(_fd_server);
throw std::runtime_error("error when listening");
}
}
void Server::_run(void) {
_client_fds[0].fd = _fd_server;
_client_fds[0].events = POLLIN;
int nbr_client = 0;
while (727) {
int ret = poll(_client_fds.data(), nbr_client + 1, -1);
if (ret < 0) {
close(_fd_server);
throw std::runtime_error("poll failed :(");
}
if (_client_fds[0].revents & POLLIN) {
struct sockaddr_in client_addr;
socklen_t addrlen = sizeof(client_addr);
int client_fd =
accept(_fd_server, (struct sockaddr *)&client_addr, &addrlen);
if (client_fd < 0) {
_log->error("accept failed");
continue;
}
// if (nbr_client ) TODO do we need a max client probably not :D
}
for (int i = 1; i <= nbr_client; ++i) {
if (_client_fds[i].revents & POLLIN) {
_handle_client(i);
close(_client_fds[i].fd);
_client_fds[i] = _client_fds[nbr_client--];
}
}
}
}
Server::Server(config::Server *conf) : _conf(conf) {
log("", "Server::Server", "config constructor called");
_log = conf->getLogger();
_setup();
_run();
}
Server::~Server(void) {
log("", "Server::Server", "destructor called");
for (std::vector<struct pollfd>::iterator it = _client_fds.begin();
it != _client_fds.end(); it++)
close(it->fd);
close(_fd_server);
}