small changes in server code

This commit is contained in:
bettercallous
2024-04-30 19:29:47 +01:00
parent 204ed9d7bd
commit e3f9610ec7

View File

@@ -123,6 +123,10 @@ void Server::handleClientConnection() {
if (newFd == -1) { if (newFd == -1) {
throw std::runtime_error("Error: accept() failed"); throw std::runtime_error("Error: accept() failed");
} }
if (fcntl(newFd, F_SETFL, O_NONBLOCK) == -1)
throw std::runtime_error("Error: fcntl() failed");
std::string passwordRequest = "Please Enter The password Of This Server :\n"; std::string passwordRequest = "Please Enter The password Of This Server :\n";
@@ -531,11 +535,10 @@ void Server::handleClientData(int fd)
size_t newlinePos = client.getCommand().find_first_of("\r\n"); size_t newlinePos = client.getCommand().find_first_of("\r\n");
if (newlinePos != std::string::npos) { if (newlinePos != std::string::npos) {
// Extract the complete message up to the newline character
std::string command = client.getCommand().substr(0, newlinePos); std::string command = client.getCommand().substr(0, newlinePos);
client.setCommand(command); client.setCommand(command);
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 = client.getAuthentication();
//******************* FROM THERE IM STARTING TOP GGG ************ //******************* FROM THERE IM STARTING TOP GGG ************
@@ -569,7 +572,7 @@ void Server::handleClientData(int fd)
else { else {
std::string confirmation = "Please Enter Your Nickname : \n"; std::string confirmation = "Please Enter Your Nickname : \n";
send(fd, confirmation.c_str(), confirmation.length(), 0); send(fd, confirmation.c_str(), confirmation.length(), 0);
getClientByFd(fd).setAuthentication(1); client.setAuthentication(1);
} }
} }
else if (startsWith(command, "QUIT ")) else if (startsWith(command, "QUIT "))
@@ -646,7 +649,7 @@ void Server::handleClientData(int fd)
} }
std::string confirmation = "Please Enter Your Username : \n"; std::string confirmation = "Please Enter Your Username : \n";
send(fd, confirmation.c_str(), confirmation.length(), 0); send(fd, confirmation.c_str(), confirmation.length(), 0);
getClientByFd(fd).setAuthentication(2); client.setAuthentication(2);
} }
} }
@@ -1148,7 +1151,6 @@ void Server::handleClientData(int fd)
} }
} }
//**************** STOOOOOOP HERE TOP G ... //**************** STOOOOOOP HERE TOP G ...
// command.clear();
client.clearCommand(); client.clearCommand();
} }
} }