diff --git a/Client.cpp b/Client.cpp index 5080e9c..125975d 100644 --- a/Client.cpp +++ b/Client.cpp @@ -48,14 +48,15 @@ void Client::setAuthentication(int auth) { _authentication = auth; } -void Client::appendToCommand(std::string toAppend) { - command += toAppend; -} const std::string& Client::getCommand() const { return command; } void Client::clearCommand() { - command.clear(); + command = ""; +} + +void Client::appendCommand(std::string str) { + this->command += str; } \ No newline at end of file diff --git a/Client.hpp b/Client.hpp index 061efe9..121363b 100644 --- a/Client.hpp +++ b/Client.hpp @@ -32,12 +32,12 @@ class Client { int getAuthentication() const; void setAuthentication(int auth); - - void appendToCommand(std::string toAppend); const std::string& getCommand() const; void clearCommand(); + + void appendCommand(std::string str); }; #endif \ No newline at end of file diff --git a/Server.cpp b/Server.cpp index 80f47e9..dbf0b5e 100644 --- a/Server.cpp +++ b/Server.cpp @@ -518,27 +518,21 @@ int stringToInt(const std::string& str) { void Server::handleClientData(int fd) { + Client& client = getClientByFd(fd); + char buffer[BUFFER_SIZE]; memset(buffer, 0, sizeof(buffer)); ssize_t bytesRead = recv(fd, buffer, BUFFER_SIZE - 1, 0); if (bytesRead > 0) { - bool foundEof = false; - for (ssize_t i = 0; i < bytesRead; ++i) { - if (buffer[i] == '\n') { - foundEof = true; - break; - } - } + buffer[bytesRead] = '\0'; - if (!foundEof) { - buffer[bytesRead] = '\0'; - getClientByFd(fd).appendToCommand(buffer); - return; - } else { - buffer[bytesRead] = '\0'; - getClientByFd(fd).appendToCommand(buffer); - std::string command = getClientByFd(fd).getCommand(); + client.appendCommand(buffer); + + size_t newlinePos = client.getCommand().find("\n"); + if (newlinePos != std::string::npos) { + // Extract the complete message up to the newline character + std::string command = client.getCommand().substr(0, newlinePos); std::cout << "Received data from client " << fd << ": " << command << std::endl; int auth = getClientByFd(fd).getAuthentication(); @@ -1143,8 +1137,8 @@ void Server::handleClientData(int fd) } } //**************** STOOOOOOP HERE TOP G ... + client.clearCommand(); } - getClientByFd(fd).clearCommand(); } if (bytesRead == 0) {