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; _authentication = auth;
} }
void Client::appendToCommand(std::string toAppend) {
command += toAppend;
}
const std::string& Client::getCommand() const { const std::string& Client::getCommand() const {
return command; return command;
} }
void Client::clearCommand() { 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; int getAuthentication() const;
void setAuthentication(int auth); void setAuthentication(int auth);
void appendToCommand(std::string toAppend);
const std::string& getCommand() const; const std::string& getCommand() const;
void clearCommand(); void clearCommand();
void appendCommand(std::string str);
}; };
#endif #endif

View File

@@ -518,27 +518,21 @@ int stringToInt(const std::string& str) {
void Server::handleClientData(int fd) void Server::handleClientData(int fd)
{ {
Client& client = getClientByFd(fd);
char buffer[BUFFER_SIZE]; char buffer[BUFFER_SIZE];
memset(buffer, 0, sizeof(buffer)); memset(buffer, 0, sizeof(buffer));
ssize_t bytesRead = recv(fd, buffer, BUFFER_SIZE - 1, 0); ssize_t bytesRead = recv(fd, buffer, BUFFER_SIZE - 1, 0);
if (bytesRead > 0) { if (bytesRead > 0) {
bool foundEof = false; buffer[bytesRead] = '\0';
for (ssize_t i = 0; i < bytesRead; ++i) {
if (buffer[i] == '\n') {
foundEof = true;
break;
}
}
if (!foundEof) { client.appendCommand(buffer);
buffer[bytesRead] = '\0';
getClientByFd(fd).appendToCommand(buffer); size_t newlinePos = client.getCommand().find("\n");
return; if (newlinePos != std::string::npos) {
} else { // Extract the complete message up to the newline character
buffer[bytesRead] = '\0'; std::string command = client.getCommand().substr(0, newlinePos);
getClientByFd(fd).appendToCommand(buffer);
std::string command = getClientByFd(fd).getCommand();
std::cout << "Received data from client " << fd << ": " << command << std::endl; std::cout << "Received data from client " << fd << ": " << command << std::endl;
int auth = getClientByFd(fd).getAuthentication(); int auth = getClientByFd(fd).getAuthentication();
@@ -1143,8 +1137,8 @@ void Server::handleClientData(int fd)
} }
} }
//**************** STOOOOOOP HERE TOP G ... //**************** STOOOOOOP HERE TOP G ...
client.clearCommand();
} }
getClientByFd(fd).clearCommand();
} }
if (bytesRead == 0) { if (bytesRead == 0) {