Skip to content

Commit

Permalink
Merge pull request #35 from brmoretti/develop
Browse files Browse the repository at this point in the history
.
  • Loading branch information
0bvim authored Aug 18, 2024
2 parents f010606 + 12d10ab commit 86c5297
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 3 deletions.
8 changes: 5 additions & 3 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 17:22:15 by bmoretti ### ########.fr */
/* Updated: 2024/08/18 18:06:13 by bmoretti ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -16,6 +16,8 @@ Events::Events(const std::vector<ServerConfig> &servers)
{
this->_epoll_fd = epoll_create(MAX_CONNECTIONS);
this->_event = new epoll_event[MAX_CONNECTIONS];
memset(this->_event, 0, sizeof(epoll_event) * MAX_CONNECTIONS);
this->_event->events = EPOLLIN | EPOLLET;
if (this->_epoll_fd == -1)
throw std::runtime_error("Failed to create epoll file descriptor");
for (std::vector<ServerConfig>::const_iterator it = servers.begin();
Expand Down Expand Up @@ -53,8 +55,8 @@ void Events::run()
if (!this->_servers[j]->_acceptClient())
break;
}
// else
// this->_servers[j]->_handleConnection(_event[i].data.fd);
else
this->_servers[j]->_handleConnection(_event[i].data.fd);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/InitServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ void Server::_initServerAddress(sockaddr_in &server_add)
void Server::_initServer()
{
this->_server_fd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
OUTNL("SERVER FD: " << this->_server_fd);
if (this->_server_fd == -1)
throw std::runtime_error("Failed to create socket");
int opt = 1;
Expand Down
2 changes: 2 additions & 0 deletions src/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ bool Server::_acceptClient()
{
sockaddr_in client_addr;
socklen_t client_addr_len = sizeof(client_addr);
OUTNL("fd no accept " << _server_fd);
int client_fd = accept(_server_fd, (sockaddr *)&client_addr, &client_addr_len);
if (client_fd == -1)
if (!this->_handleAcceptError(errno))
Expand All @@ -58,6 +59,7 @@ bool Server::_handleAcceptError(int error_code)
void Server::_handleConnection(int client_fd)
{
char buffer[BUFFER_SIZE];
OUTNL("Entrou no handle connection");
while (true)
{
ssize_t bytes_read = _readFromClient(client_fd, buffer);
Expand Down
2 changes: 2 additions & 0 deletions tests/nonblk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ void handle_connections(int server_fd)
{
if (events[i].data.fd == server_fd)
{
std::cout << "Incoming connection" << std::endl;
struct sockaddr_in client_address;
socklen_t client_len = sizeof(client_address);
int client_fd = accept(server_fd, (struct sockaddr *)&client_address, &client_len);
Expand All @@ -112,6 +113,7 @@ void handle_connections(int server_fd)
}
else
{
std::cout << "Incoming data" << std::endl;
char buffer[1024] = {0};
int client_fd = events[i].data.fd;
int bytes_read = read(client_fd, buffer, sizeof(buffer));
Expand Down

0 comments on commit 86c5297

Please sign in to comment.