fixed ctrl+D bug

This commit is contained in:
Bettercallous
2024-04-29 16:06:48 +01:00
parent 23f12a5e61
commit 9abced38a0
5 changed files with 32 additions and 74 deletions

56
.vscode/settings.json vendored
View File

@@ -1,56 +0,0 @@
{
"files.associations": {
"iostream": "cpp",
"array": "cpp",
"atomic": "cpp",
"bit": "cpp",
"*.tcc": "cpp",
"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"compare": "cpp",
"concepts": "cpp",
"csignal": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"deque": "cpp",
"map": "cpp",
"string": "cpp",
"unordered_map": "cpp",
"vector": "cpp",
"exception": "cpp",
"algorithm": "cpp",
"functional": "cpp",
"iterator": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"optional": "cpp",
"random": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"initializer_list": "cpp",
"iomanip": "cpp",
"iosfwd": "cpp",
"istream": "cpp",
"limits": "cpp",
"new": "cpp",
"numbers": "cpp",
"ostream": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"cinttypes": "cpp",
"typeinfo": "cpp"
}
}

View File

@@ -16,7 +16,6 @@ std::string Client::getPassowrd() const {
return pass; return pass;
} }
// Implementation of getter and setter functions for name
std::string Client::getName() const { std::string Client::getName() const {
return name; return name;
} }
@@ -25,7 +24,6 @@ void Client::setName(const std::string& newName) {
name = newName; name = newName;
} }
// Implementation of getter and setter functions for nick
std::string Client::getNick() const { std::string Client::getNick() const {
return nick; return nick;
} }
@@ -34,7 +32,6 @@ void Client::setNick(const std::string& newNick) {
nick = newNick; nick = newNick;
} }
// Implementation of getter and setter functions for user
std::string Client::getUser() const { std::string Client::getUser() const {
return user; return user;
} }
@@ -50,3 +47,15 @@ int Client::getAuthentication() const {
void Client::setAuthentication(int auth) { void Client::setAuthentication(int auth) {
_authentication = auth; _authentication = auth;
} }
void Client::appendToCommand(std::string toAppend) {
command += toAppend;
}
const std::string& Client::getCommand() const {
return command;
}
void Client::clearCommand() {
command.clear();
}

View File

@@ -10,6 +10,7 @@ class Client {
std::string name; std::string name;
std::string nick; std::string nick;
std::string user; std::string user;
std::string command;
int _authentication; int _authentication;
public: public:
Client(); Client();
@@ -18,22 +19,25 @@ class Client {
int getFd() const; int getFd() const;
std::string getPassowrd() const; std::string getPassowrd() const;
void setPassword(const std::string& password); // Function to set the password void setPassword(const std::string& password);
// Getter and setter for name
std::string getName() const; std::string getName() const;
void setName(const std::string& newName); void setName(const std::string& newName);
// Getter and setter for nick
std::string getNick() const; std::string getNick() const;
void setNick(const std::string& newNick); void setNick(const std::string& newNick);
// Getter and setter for user
std::string getUser() const; std::string getUser() const;
void setUser(const std::string& newUser); void setUser(const std::string& newUser);
int getAuthentication() const; int getAuthentication() const;
void setAuthentication(int auth); void setAuthentication(int auth);
void appendToCommand(std::string toAppend);
const std::string& getCommand() const;
void clearCommand();
}; };
#endif #endif

View File

@@ -518,12 +518,11 @@ int stringToInt(const std::string& str) {
void Server::handleClientData(int fd) void Server::handleClientData(int fd)
{ {
std::string command;
char buffer[BUFFER_SIZE]; char buffer[BUFFER_SIZE];
memset(buffer, 0, sizeof(buffer)); memset(buffer, 0, sizeof(buffer));
ssize_t bytesRead; ssize_t bytesRead = recv(fd, buffer, BUFFER_SIZE - 1, 0);
while ((bytesRead = recv(fd, buffer, BUFFER_SIZE - 1, 0)) > 0) { if (bytesRead > 0) {
bool foundEof = false; bool foundEof = false;
for (ssize_t i = 0; i < bytesRead; ++i) { for (ssize_t i = 0; i < bytesRead; ++i) {
if (buffer[i] == '\n') { if (buffer[i] == '\n') {
@@ -534,9 +533,12 @@ void Server::handleClientData(int fd)
if (!foundEof) { if (!foundEof) {
buffer[bytesRead] = '\0'; buffer[bytesRead] = '\0';
command += buffer; getClientByFd(fd).appendToCommand(buffer);
return;
} else { } else {
command.append(buffer, bytesRead - 1); buffer[bytesRead] = '\0';
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();
@@ -1140,10 +1142,9 @@ void Server::handleClientData(int fd)
} }
} }
} }
//**************** STOOOOOOP HERE TOP G ... //**************** STOOOOOOP HERE TOP G ...
break;
} }
getClientByFd(fd).clearCommand();
} }
if (bytesRead == 0) { if (bytesRead == 0) {

View File

@@ -38,9 +38,9 @@ class Server {
std::vector<struct pollfd> _fds; std::vector<struct pollfd> _fds;
std::vector<Client> _clients; std::vector<Client> _clients;
// THAT'S THA DATA OF TOOOP GGG START FROM THERE . // THAT'S THA DATA OF TOOOP GGG START FROM THERE .
std::map<int, std::string> nicknames; // Replace unordered_map with map std::map<int, std::string> nicknames;
std::map<int, std::string> usernames; // Replace unordered_map with map std::map<int, std::string> usernames;
// std::map<std::string, std::vector<std::string> > channels; //here a chanel name and list of client in every chanel
std::map<std::string, Channel> channels; std::map<std::string, Channel> channels;