fixex ctrl+D bug v.2

This commit is contained in:
Bettercallous
2024-04-29 18:36:37 +01:00
parent 9abced38a0
commit 3b951ffdf8
3 changed files with 17 additions and 22 deletions

View File

@@ -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;
}

View File

@@ -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

View File

@@ -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) {