Skip to content

Commit

Permalink
now client is identified
Browse files Browse the repository at this point in the history
  • Loading branch information
brmoretti committed Aug 31, 2024
1 parent e2288bb commit d4d097f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
4 changes: 3 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/31 16:43:19 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 @@ -48,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
25 changes: 16 additions & 9 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/31 17:08:18 by bmoretti ### ########.fr */
/* Updated: 2024/08/31 17:50:13 by bmoretti ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -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;
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ bool Server::_acceptClient()
perror("epoll_ctl failed");
close(client_fd);
}
_clients.push_back(client_fd);
return true;
}

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

0 comments on commit d4d097f

Please sign in to comment.