🏗️」 wip: started server class

This commit is contained in:
2025-04-11 19:23:44 +02:00
parent 1c79dbcb13
commit 20a0bf80de
4 changed files with 137 additions and 8 deletions

View File

@ -6,20 +6,18 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/11 13:29:05 by mmoussou #+# #+# */
/* Updated: 2025/02/12 00:14:11 by mmoussou ### ########.fr */
/* Updated: 2025/04/11 18:06:05 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
#ifndef __WEBSERV_SERVER_DEFAULT_HPP__
# define __WEBSERV_SERVER_DEFAULT_HPP__
#include "server.hpp"
namespace webserv {
namespace server {
} // -namespace server
} // -namespace webserv
using namespace webserv;
#endif // __WEBSERV_SERVER_DEFAULT_HPP__

View File

@ -0,0 +1,54 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* server.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/11 17:45:43 by adjoly #+# #+# */
/* Updated: 2025/04/11 19:22:43 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
#include "log.hpp"
#include <config/default.hpp>
#include <fcntl.h>
#include <stdexcept>
#include <sys/poll.h>
#include <vector>
namespace webserv {
class Server {
public:
Server(config::Server *);
~Server(void);
protected:
private:
/**
* @brief Used to setup the webserver (primarly socket)
*/
void _setup(void);
/**
* @brief Used to run the webserver
*/
void _run(void);
/**
* @brief Used to handle client request
*
* @param The number of the client fd
*/
void _handle_client(int);
config::Server
*_conf; ///> Pointer to the configuration class (with all config in)
Logger *_log; ///> Pointer to the log class
int _fd_server; ///> The fd of the socket
std::vector<struct pollfd> _client_fds; ///> A vector of all the poll fd
};
}; // namespace webserv

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/03 15:45:07 by mmoussou #+# #+# */
/* Updated: 2025/04/11 15:58:17 by adjoly ### ########.fr */
/* Updated: 2025/04/11 17:51:33 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -36,5 +36,6 @@ int main(int ac, char **av) {
std::cout << e.what() << std::endl;
return 1;
}
delete conf;
}

76
src/setup.cpp Normal file
View File

@ -0,0 +1,76 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* setup.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/11 16:11:40 by adjoly #+# #+# */
/* Updated: 2025/04/11 19:22:50 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "log.hpp"
#include "server/default.hpp"
#include <cmath>
#include <netinet/in.h>
#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
}
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--];
}
}
}
}