Skip to content

Commit

Permalink
Merge pull request #37 from brmoretti/develop
Browse files Browse the repository at this point in the history
.
  • Loading branch information
0bvim authored Aug 31, 2024
2 parents 86c5297 + d4d097f commit a795c9e
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 14 deletions.
4 changes: 2 additions & 2 deletions include/Events.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: bmoretti <bmoretti@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand All @@ -29,7 +29,7 @@ class Events
int _epoll_fd;
epoll_event *_event;

void handleEvents();
void _handleEvents();
};

#endif
5 changes: 4 additions & 1 deletion include/Server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: bmoretti <bmoretti@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand All @@ -26,6 +26,7 @@ class Server
int _getServerFd() const;
bool _acceptClient();
void _handleConnection(int client_fd);
bool _isClientConnected(int fd);
void run();

private:
Expand All @@ -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;
Expand All @@ -47,6 +49,7 @@ class Server
std::map<int, std::string> _buffer_request;
struct epoll_event _events[MAX_EVENTS];
const ServerConfig &_config;
std::vector<int> _clients;
};

#endif
3 changes: 2 additions & 1 deletion include/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: bmoretti <bmoretti@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand All @@ -30,6 +30,7 @@
#include <stdlib.h>
#include <string.h>
#include <sys/epoll.h>
#include <sys/ioctl.h>
#include <unistd.h>

// C++ LIBS
Expand Down
29 changes: 19 additions & 10 deletions src/Events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: bmoretti <bmoretti@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -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()
{


Expand Down
21 changes: 21 additions & 0 deletions src/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit a795c9e

Please sign in to comment.