diff --git a/default.conf b/default.conf index 8804441..42ed651 100644 --- a/default.conf +++ b/default.conf @@ -1,33 +1,21 @@ -events { - worker_connections 1024; -} - -http { - server { - listen 127.0.0.1:8042; - server_name localhost; +server { + listen 127.0.0.1:8042; + server_name localhost; - location / { - root /usr/share/nginx/html; - index index.html; - cgi .py cgi-bin; - } + location / { + root /usr/share/nginx/html; + index index.html; + cgi .py cgi-bin; } } -events { - worker_connections 1024; -} - -http { - server { - listen 127.0.0.1:8041; - server_name localhost; +server { + listen 127.0.0.1:8041; + server_name localhost; - location / { - root /usr/share/nginx/html; - index index.html; - cgi .py cgi-bin; - } + location / { + root /usr/share/nginx/html; + index index.html; + cgi .py cgi-bin; } } diff --git a/include/Events.hpp b/include/Events.hpp new file mode 100644 index 0000000..72c482d --- /dev/null +++ b/include/Events.hpp @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Events.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bmoretti +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/08/18 16:05:36 by bmoretti #+# #+# */ +/* Updated: 2024/08/18 17:31:39 by bmoretti ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef EVENTS_HPP +# define EVENTS_HPP + +# include "common.hpp" +# include "Config.hpp" +# include "Server.hpp" + +class Events +{ +public: + Events(const std::vector & servers); + ~Events(); + void run(); + +private: + std::vector _servers; + int _epoll_fd; + epoll_event *_event; + + void handleEvents(); +}; + +#endif diff --git a/include/Server.hpp b/include/Server.hpp index 7a1aab1..fafe703 100644 --- a/include/Server.hpp +++ b/include/Server.hpp @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* Server.hpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: nivicius +#+ +:+ +#+ */ +/* By: bmoretti +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/27 10:40:41 by bmoretti #+# #+# */ -/* Updated: 2024/08/17 18:58:07 by nivicius ### ########.fr */ +/* Updated: 2024/08/18 17:31:41 by bmoretti ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,20 +24,19 @@ class Server Server(const ServerConfig &config, int event_fd, epoll_event *event); ~Server(); int _getServerFd() const; + bool _acceptClient(); + void _handleConnection(int client_fd); void run(); private: /* Server initialization */ void _initServer(); - void _handleConnection(int client_fd); - void _handleEvents(); void _fillBuffer(int fd, const char *str); bool _checkEndMessage(int fd); void _printOnClient(int fd, std::string const &str); ssize_t _readFromClient(int fd, char *buff); bool _handleAcceptError(int error_code); void _initServerAddress(sockaddr_in &server_add); - bool _acceptClient(); std::string _address; int _port; diff --git a/include/common.hpp b/include/common.hpp index b02dd0e..fd0f543 100644 --- a/include/common.hpp +++ b/include/common.hpp @@ -6,7 +6,7 @@ /* By: bmoretti +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/27 11:24:48 by bmoretti #+# #+# */ -/* Updated: 2024/08/04 16:53:08 by bmoretti ### ########.fr */ +/* Updated: 2024/08/18 17:31:40 by bmoretti ### ########.fr */ /* */ /* ************************************************************************** */ @@ -34,6 +34,7 @@ // C++ LIBS #include +#include #include #include #include diff --git a/src/Events.cpp b/src/Events.cpp new file mode 100644 index 0000000..850138e --- /dev/null +++ b/src/Events.cpp @@ -0,0 +1,67 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Events.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bmoretti +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/08/18 16:09:46 by bmoretti #+# #+# */ +/* Updated: 2024/08/18 17:22:15 by bmoretti ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Events.hpp" + +Events::Events(const std::vector &servers) +{ + this->_epoll_fd = epoll_create(MAX_CONNECTIONS); + this->_event = new epoll_event[MAX_CONNECTIONS]; + if (this->_epoll_fd == -1) + throw std::runtime_error("Failed to create epoll file descriptor"); + for (std::vector::const_iterator it = servers.begin(); + it != servers.end(); ++it) + { + Server *server = new Server(*it, this->_epoll_fd, this->_event); + this->_servers.push_back(server); + } +} + +Events::~Events() +{ + for (std::vector::iterator it = this->_servers.begin(); + it != this->_servers.end(); ++it) + delete *it; + if (this->_epoll_fd != -1) + close(this->_epoll_fd); + if (this->_event) + delete[] this->_event; +} + +void Events::run() +{ + while (true) + { + int event_count = epoll_wait(this->_epoll_fd, this->_event, MAX_EVENTS, -1); + if (event_count == -1) + throw std::runtime_error("Failed to wait on epoll"); + for (int i = 0; i < event_count; i++) + { + for(int j = 0, size = this->_servers.size(); j < size; j++) + { + if (_event[i].data.fd == this->_servers[j]->_getServerFd()) + { + if (!this->_servers[j]->_acceptClient()) + break; + } + // else + // this->_servers[j]->_handleConnection(_event[i].data.fd); + } + } + } +} + +void Events::handleEvents() +{ + + +} diff --git a/src/InitServer.cpp b/src/InitServer.cpp index fb01108..cbfde55 100644 --- a/src/InitServer.cpp +++ b/src/InitServer.cpp @@ -10,15 +10,11 @@ void Server::_initServerAddress(sockaddr_in &server_add) throw std::runtime_error("Failed to bind socket"); if (listen(_server_fd, MAX_CONNECTIONS) == -1) throw std::runtime_error("Failed to listen socket"); - // this->_epoll_fd = epoll_create1(0); - // if (this->_epoll_fd == -1) - // throw std::runtime_error("Failed to create epoll file descriptor"); } void Server::_initServer() { this->_server_fd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0); - OUTNL("server fd > " << _server_fd); if (this->_server_fd == -1) throw std::runtime_error("Failed to create socket"); int opt = 1; @@ -26,11 +22,7 @@ void Server::_initServer() throw std::runtime_error("Failed to set socket options"); sockaddr_in server_addr; this->_initServerAddress(server_addr); - // epoll_event event; - // memset(&event, 0, sizeof(epoll_event)); - // event.events = EPOLLIN | EPOLLET; (*_event).data.fd = this->_server_fd; - OUTNL("epoll fd > " << _epoll_fd << " server fd > " << _server_fd); if (epoll_ctl(_epoll_fd, EPOLL_CTL_ADD, _server_fd, _event) == -1) throw std::runtime_error("Failed to add server socket to epoll"); } diff --git a/src/Server.cpp b/src/Server.cpp index f32b4d3..119c854 100644 --- a/src/Server.cpp +++ b/src/Server.cpp @@ -21,7 +21,7 @@ Server::Server(const ServerConfig &config, int epoll_fd, epoll_event *event) : _ this->_epoll_fd = epoll_fd; this->_event = event; this->_initServer(); - OUTNL(MAGENTA("Read to connect in: " << _address + ":" << _port)); + OUTNL(MAGENTA("Ready to connect in: " << _address + ":" << _port)); } Server::~Server() @@ -34,44 +34,13 @@ Server::~Server() bool Server::_acceptClient() { - while (true) - { - sockaddr_in client_addr; - socklen_t client_addr_len = sizeof(client_addr); - int client_fd = accept(_server_fd, (sockaddr *)&client_addr, &client_addr_len); - if (client_fd == -1) - if (!this->_handleAcceptError(errno)) - return false; - epoll_event event; - memset(&event, 0, sizeof(epoll_event)); - event.events = EPOLLIN | EPOLLET; - event.data.fd = client_fd; - if (epoll_ctl(_epoll_fd, EPOLL_CTL_ADD, client_fd, &event) == -1) - { - std::cerr << "Failed to add client socket to epoll" << std::endl; - close(client_fd); - } - } -} - -void Server::run() -{ - while (true) - { - int event_count = epoll_wait(_epoll_fd, _events, MAX_EVENTS, -1); - if (event_count == -1) - throw std::runtime_error("Failed to wait on epoll"); - for (int i = 0; i < event_count; i++) - { - if (_events[i].data.fd == _server_fd) - { - if (!this->_acceptClient()) - break; - } - else - this->_handleConnection(_events[i].data.fd); - } - } + sockaddr_in client_addr; + socklen_t client_addr_len = sizeof(client_addr); + int client_fd = accept(_server_fd, (sockaddr *)&client_addr, &client_addr_len); + if (client_fd == -1) + if (!this->_handleAcceptError(errno)) + return false; + return true; } bool Server::_handleAcceptError(int error_code) diff --git a/src/main.cpp b/src/main.cpp index a44aa0b..dffcaf7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,51 +1,17 @@ #include "../include/Server.hpp" #include "../include/Config.hpp" - -void run(int epoll_fd, epoll_event *event) -{ - while (true) - { - int event_count = epoll_wait(epoll_fd, event, MAX_EVENTS, -1); - if (event_count == -1) - throw std::runtime_error("Failed to wait on epoll"); - // for (int i = 0; i < event_count; i++) - // { - // if (_events[i].data.fd == _server_fd) - // { - // if (!this->_acceptClient()) - // break; - // } - // else - // this->_handleConnection(_events[i].data.fd); - // } - } -} +#include "../include/Events.hpp" int main(int ac, char **av) { std::string file; - int epoll_fd; - OUTNL("pid " << getpid()); ac == 2 ? file = av[1] : file = DEFAULT_CONFIG_FILE; try { Config config(file); - const std::vector & servers = config.getServers(); - epoll_fd = epoll_create(MAX_CONNECTIONS); - epoll_event *event; - event = new epoll_event[MAX_CONNECTIONS]; - if (epoll_fd == -1) - throw std::runtime_error("Failed to create epoll file descriptor"); - for (std::vector::const_iterator it = servers.begin(); - it != servers.end(); ++it) - { - Server server(*it, epoll_fd, event); - OUTNL(server._getServerFd()); - //server.run(); - std::cout << "Server started on port" << std::endl; - } - run(epoll_fd, event); + Events events(config.getServers()); + events.run(); } catch (std::exception &e) {