From e2288bbd569290515337bc65eda3e5f6eeb17e94 Mon Sep 17 00:00:00 2001 From: Bruno Moretti Date: Sat, 31 Aug 2024 17:09:04 -0300 Subject: [PATCH 1/2] client fd is now added to epoll struct --- include/Events.hpp | 4 ++-- include/Server.hpp | 3 ++- include/common.hpp | 3 ++- src/Events.cpp | 10 ++++++---- src/Server.cpp | 15 +++++++++++++++ 5 files changed, 27 insertions(+), 8 deletions(-) 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..541abc7 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 16:43:19 by bmoretti ### ########.fr */ /* */ /* ************************************************************************** */ @@ -37,6 +37,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; 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..a94ecb0 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:08:18 by bmoretti ### ########.fr */ /* */ /* ************************************************************************** */ @@ -50,9 +50,10 @@ void Events::run() { for(int j = 0, size = this->_servers.size(); j < size; j++) { - if (_event[i].data.fd == this->_servers[j]->_getServerFd()) + int server_fd = this->_servers[j]->_getServerFd(); + if (_event[i].data.fd == server_fd) { - if (!this->_servers[j]->_acceptClient()) + if (this->_servers[j]->_acceptClient()) break; } else @@ -62,7 +63,8 @@ void Events::run() } } -void Events::handleEvents() + +void Events::_handleEvents() { diff --git a/src/Server.cpp b/src/Server.cpp index 9811d6c..af18eef 100644 --- a/src/Server.cpp +++ b/src/Server.cpp @@ -41,6 +41,14 @@ 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); + } return true; } @@ -85,6 +93,13 @@ 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"); +} + int Server::_getServerFd() const { return this->_server_fd; From d4d097fe27a1d928573117b784a36f91f1c3dd37 Mon Sep 17 00:00:00 2001 From: Bruno Moretti Date: Sat, 31 Aug 2024 18:24:40 -0300 Subject: [PATCH 2/2] now client is identified --- include/Server.hpp | 4 +++- src/Events.cpp | 25 ++++++++++++++++--------- src/Server.cpp | 6 ++++++ 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/include/Server.hpp b/include/Server.hpp index 541abc7..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/31 16:43:19 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: @@ -48,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/src/Events.cpp b/src/Events.cpp index a94ecb0..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/31 17:08:18 by bmoretti ### ########.fr */ +/* Updated: 2024/08/31 17:50:13 by bmoretti ### ########.fr */ /* */ /* ************************************************************************** */ @@ -48,17 +48,24 @@ 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++) { int server_fd = this->_servers[j]->_getServerFd(); - if (_event[i].data.fd == server_fd) - { - if (this->_servers[j]->_acceptClient()) - break; - } - else - this->_servers[j]->_handleConnection(_event[i].data.fd); + 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; } } } diff --git a/src/Server.cpp b/src/Server.cpp index af18eef..048d2e8 100644 --- a/src/Server.cpp +++ b/src/Server.cpp @@ -49,6 +49,7 @@ bool Server::_acceptClient() perror("epoll_ctl failed"); close(client_fd); } + _clients.push_back(client_fd); return true; } @@ -100,6 +101,11 @@ void Server::_setSocketNonBlocking(int fd) 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;