From ed8685133b07c22470e18a04e5f112f761a543e8 Mon Sep 17 00:00:00 2001 From: Bruno Moretti Date: Sun, 1 Sep 2024 21:34:01 -0300 Subject: [PATCH 1/2] trying to identify each client buffer --- include/Client.hpp | 14 +++++----- include/Server.hpp | 5 ++-- src/Client.cpp | 39 +++++++++++++------------- src/Server.cpp | 69 +++++++++++++++++++++++++++++----------------- 4 files changed, 73 insertions(+), 54 deletions(-) diff --git a/include/Client.hpp b/include/Client.hpp index 6293d30..4ef9c95 100644 --- a/include/Client.hpp +++ b/include/Client.hpp @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* Client.hpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: vde-frei +#+ +:+ +#+ */ +/* By: bmoretti +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/27 14:40:02 by bmoretti #+# #+# */ -/* Updated: 2024/07/28 22:34:05 by vde-frei ### ########.fr */ +/* Updated: 2024/09/01 16:30:10 by bmoretti ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,16 +18,16 @@ class Client { public: - Client(); + Client(int serverSocket); ~Client(); - void connectToServer(const int serverSocket); - int getClientSocket(void) const; + int _getClientSocket() const; + std::string _getBuffer(); + void _addToBuffer(const std::string &str); private: int _clientSocket; struct sockaddr_in _address; - - void _createClientSocket(void); + std::string _buffer; }; #endif diff --git a/include/Server.hpp b/include/Server.hpp index 6a5af08..e655880 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 17:44:21 by bmoretti ### ########.fr */ +/* Updated: 2024/09/01 16:26:31 by bmoretti ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,6 +17,7 @@ #include "Request.hpp" #include "Response.hpp" #include "Config.hpp" +#include "Client.hpp" class Server { @@ -49,7 +50,7 @@ class Server std::map _buffer_request; struct epoll_event _events[MAX_EVENTS]; const ServerConfig &_config; - std::vector _clients; + std::map _clients; }; #endif diff --git a/src/Client.cpp b/src/Client.cpp index 9643c72..72da1ab 100644 --- a/src/Client.cpp +++ b/src/Client.cpp @@ -3,42 +3,43 @@ /* ::: :::::::: */ /* Client.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: nivicius +#+ +:+ +#+ */ +/* By: bmoretti +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/27 14:46:10 by bmoretti #+# #+# */ -/* Updated: 2024/08/08 01:55:14 by nivicius ### ########.fr */ +/* Updated: 2024/09/01 16:30:34 by bmoretti ### ########.fr */ /* */ /* ************************************************************************** */ #include "../include/Client.hpp" -Client::Client() {} +Client::Client(int serverSocket) +{ + memset(&this->_address, 0, sizeof(this->_address)); + + socklen_t client_addr_len = sizeof(this->_address); + this->_clientSocket = accept(serverSocket, (sockaddr *)&this->_address, &client_addr_len); + if (this->_clientSocket == -1) + throw std::runtime_error("Failed to accept client"); + int flags = 1; + if (ioctl(this->_clientSocket, FIONBIO, &flags) < 0) + throw std::runtime_error("Failed to set socket non-blocking"); +} Client::~Client() { close(this->_clientSocket); } -void Client::connectToServer(const int serverSocket) +int Client::_getClientSocket() const { - socklen_t addressSize = sizeof(this->_address); - - this->_clientSocket = accept(serverSocket, - (struct sockaddr *)&this->_address, &addressSize); - if (this->_clientSocket == -1) - throw std::runtime_error("Error connecting to the server"); + return this->_clientSocket; } -int Client::getClientSocket(void) const +std::string Client::_getBuffer() { - return this->_clientSocket; + return this->_buffer; } - -void Client::_createClientSocket(void) +void Client::_addToBuffer(const std::string &str) { - this->_clientSocket = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0); - if (this->_clientSocket == -1) - { - throw std::runtime_error("Error creating the client socket"); - } + this->_buffer += str; } diff --git a/src/Server.cpp b/src/Server.cpp index 048d2e8..06457fa 100644 --- a/src/Server.cpp +++ b/src/Server.cpp @@ -28,20 +28,20 @@ Server::~Server() { if (_server_fd != -1) close(_server_fd); - if (_epoll_fd != -1) - close(_epoll_fd); + for (std::map::iterator it = _clients.begin(); it != _clients.end(); ++it) + { + delete it->second; + _clients.erase(it); + break; + } } 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)) - return false; - this->_setSocketNonBlocking(client_fd); + Client *client = new Client(this->_server_fd); + + int client_fd = client->_getClientSocket(); + (*_event).events = EPOLLIN | EPOLLET; (*_event).data.fd = client_fd; if (epoll_ctl(this->_epoll_fd, EPOLL_CTL_ADD, client_fd, _event) == -1) @@ -49,7 +49,7 @@ bool Server::_acceptClient() perror("epoll_ctl failed"); close(client_fd); } - _clients.push_back(client_fd); + this->_clients[client_fd] = client; return true; } @@ -65,10 +65,39 @@ bool Server::_handleAcceptError(int error_code) return true; } +// void Server::_handleConnection(int client_fd) +// { +// char buffer[BUFFER_SIZE]; + +// while (true) +// { +// ssize_t bytes_read = _readFromClient(client_fd, buffer); +// if (bytes_read <= 0) +// break; +// else +// { +// buffer[bytes_read] = '\0'; +// _fillBuffer(client_fd, buffer); +// if (this->_status == HttpStatus::BAD_REQUEST) +// { +// this->_printOnClient(client_fd, "Bad Request\n"); +// close(client_fd); +// } +// else if (_checkEndMessage(client_fd)) +// { +// Request request(this->_buffer_request[client_fd].c_str()); +// // request.printRequest(); +// Response response(request, this->_config); +// this->_printOnClient(client_fd, response.getResponse()); +// } +// } +// } +// } + 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); @@ -77,19 +106,7 @@ void Server::_handleConnection(int client_fd) else { buffer[bytes_read] = '\0'; - _fillBuffer(client_fd, buffer); - if (this->_status == HttpStatus::BAD_REQUEST) - { - this->_printOnClient(client_fd, "Bad Request\n"); - close(client_fd); - } - else if (_checkEndMessage(client_fd)) - { - Request request(this->_buffer_request[client_fd].c_str()); - // request.printRequest(); - Response response(request, this->_config); - this->_printOnClient(client_fd, response.getResponse()); - } + this->_clients[client_fd]->_addToBuffer(buffer); } } } @@ -103,7 +120,7 @@ void Server::_setSocketNonBlocking(int fd) bool Server::_isClientConnected(int fd) { - return std::find(_clients.begin(), _clients.end(), fd) != _clients.end(); + return this->_clients.find(fd) != this->_clients.end(); } int Server::_getServerFd() const From d8f9c07b5d255a7034dc0a156a845123098ed185 Mon Sep 17 00:00:00 2001 From: Bruno Moretti Date: Sun, 1 Sep 2024 22:10:36 -0300 Subject: [PATCH 2/2] trying to identify each client buffer --- src/Server.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Server.cpp b/src/Server.cpp index 06457fa..212f0d7 100644 --- a/src/Server.cpp +++ b/src/Server.cpp @@ -96,18 +96,18 @@ bool Server::_handleAcceptError(int error_code) void Server::_handleConnection(int client_fd) { - char buffer[BUFFER_SIZE]; + char buffer[BUFFER_SIZE + 1]; while (true) { ssize_t bytes_read = _readFromClient(client_fd, buffer); - if (bytes_read <= 0) - break; - else + if (bytes_read > 0) { buffer[bytes_read] = '\0'; this->_clients[client_fd]->_addToBuffer(buffer); } + else + break; } }