diff --git a/include/Events.hpp b/include/Events.hpp index 72c482d..488797e 100644 --- a/include/Events.hpp +++ b/include/Events.hpp @@ -6,7 +6,7 @@ /* By: bmoretti +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/18 16:05:36 by bmoretti #+# #+# */ -/* Updated: 2024/08/18 17:31:39 by bmoretti ### ########.fr */ +/* Updated: 2024/08/31 16:59:14 by bmoretti ### ########.fr */ /* */ /* ************************************************************************** */ @@ -29,7 +29,7 @@ class Events int _epoll_fd; epoll_event *_event; - void handleEvents(); + void _handleEvents(); }; #endif diff --git a/include/Server.hpp b/include/Server.hpp index fafe703..6a5af08 100644 --- a/include/Server.hpp +++ b/include/Server.hpp @@ -6,7 +6,7 @@ /* By: bmoretti +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/27 10:40:41 by bmoretti #+# #+# */ -/* Updated: 2024/08/18 17:31:41 by bmoretti ### ########.fr */ +/* Updated: 2024/08/31 17:44:21 by bmoretti ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,6 +26,7 @@ class Server int _getServerFd() const; bool _acceptClient(); void _handleConnection(int client_fd); + bool _isClientConnected(int fd); void run(); private: @@ -37,6 +38,7 @@ class Server ssize_t _readFromClient(int fd, char *buff); bool _handleAcceptError(int error_code); void _initServerAddress(sockaddr_in &server_add); + void _setSocketNonBlocking(int fd); std::string _address; int _port; @@ -47,6 +49,7 @@ class Server std::map _buffer_request; struct epoll_event _events[MAX_EVENTS]; const ServerConfig &_config; + std::vector _clients; }; #endif diff --git a/include/common.hpp b/include/common.hpp index fd0f543..f2e555b 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/18 17:31:40 by bmoretti ### ########.fr */ +/* Updated: 2024/08/31 16:38:59 by bmoretti ### ########.fr */ /* */ /* ************************************************************************** */ @@ -30,6 +30,7 @@ #include #include #include +#include #include // C++ LIBS diff --git a/src/Events.cpp b/src/Events.cpp index 675a181..45a6d34 100644 --- a/src/Events.cpp +++ b/src/Events.cpp @@ -6,7 +6,7 @@ /* By: bmoretti +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/18 16:09:46 by bmoretti #+# #+# */ -/* Updated: 2024/08/18 18:06:13 by bmoretti ### ########.fr */ +/* Updated: 2024/08/31 17:50:13 by bmoretti ### ########.fr */ /* */ /* ************************************************************************** */ @@ -48,21 +48,30 @@ void Events::run() 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++) + bool found = false; + 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); + int server_fd = this->_servers[j]->_getServerFd(); + if (_event[i].data.fd == server_fd && this->_servers[j]->_acceptClient()) + { + found = true; + break; + } + else if (this->_servers[j]->_isClientConnected(_event[i].data.fd)) + { + this->_servers[j]->_handleConnection(_event[i].data.fd); + found = true; + break; + } } + if (found) + break; } } } -void Events::handleEvents() + +void Events::_handleEvents() { diff --git a/src/Server.cpp b/src/Server.cpp index 9811d6c..048d2e8 100644 --- a/src/Server.cpp +++ b/src/Server.cpp @@ -41,6 +41,15 @@ bool Server::_acceptClient() if (client_fd == -1) if (!this->_handleAcceptError(errno)) return false; + this->_setSocketNonBlocking(client_fd); + (*_event).events = EPOLLIN | EPOLLET; + (*_event).data.fd = client_fd; + if (epoll_ctl(this->_epoll_fd, EPOLL_CTL_ADD, client_fd, _event) == -1) + { + perror("epoll_ctl failed"); + close(client_fd); + } + _clients.push_back(client_fd); return true; } @@ -85,6 +94,18 @@ void Server::_handleConnection(int client_fd) } } +void Server::_setSocketNonBlocking(int fd) +{ + int flags = 1; + if (ioctl(fd, FIONBIO, &flags) < 0) + throw std::runtime_error("Failed to set socket non-blocking"); +} + +bool Server::_isClientConnected(int fd) +{ + return std::find(_clients.begin(), _clients.end(), fd) != _clients.end(); +} + int Server::_getServerFd() const { return this->_server_fd;