optimizing code

This commit is contained in:
bettercallous
2024-04-27 14:53:25 +01:00
parent ef8a9fd5d8
commit bb721c9b2a
5 changed files with 10 additions and 68 deletions

56
.vscode/settings.json vendored
View File

@@ -1,56 +0,0 @@
{
"files.associations": {
"cstring": "cpp",
"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",
"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

@@ -2,13 +2,12 @@
Client::Client() : _isRegistered(false) {} Client::Client() : _isRegistered(false) {}
Client::Client(int fd, std::string addr) : _fd(fd), _addr(addr) {} Client::Client(int fd) : _fd(fd) {}
Client::~Client() {} Client::~Client() {}
int Client::getFd() const {return _fd;} int Client::getFd() const {return _fd;}
void Client::setPassword(const std::string& password) { void Client::setPassword(const std::string& password) {
pass = password; pass = password;
} }

View File

@@ -10,11 +10,10 @@ class Client {
std::string name; std::string name;
std::string nick; std::string nick;
std::string user; std::string user;
std::string _addr;
bool _isRegistered; bool _isRegistered;
public: public:
Client(); Client();
Client(int fd, std::string addr); Client(int fd);
~Client(); ~Client();
int getFd() const; int getFd() const;

View File

@@ -37,7 +37,7 @@ void Server::parseArgs(int ac, char **av) {
throw std::runtime_error("Error: Invalid arguments"); throw std::runtime_error("Error: Invalid arguments");
long _port = atol(av[1]); long _port = atol(av[1]);
if (!(_port >= 1 && _port <= 65535)) if (!(_port >= 0 && _port <= 65535))
throw std::runtime_error("Error: Invalid arguments"); throw std::runtime_error("Error: Invalid arguments");
if (pwd.empty()) if (pwd.empty())
@@ -96,7 +96,7 @@ void Server::createServerSocket() {
if (listen(_serverSocketFd, SOMAXCONN) == -1) if (listen(_serverSocketFd, SOMAXCONN) == -1)
throw std::runtime_error("Error: listen() failed"); throw std::runtime_error("Error: listen() failed");
addPollfd(_serverSocketFd, POLLIN, 0); addPollfd(_serverSocketFd);
} }
void Server::bindServerSocket() { void Server::bindServerSocket() {
@@ -109,11 +109,11 @@ void Server::bindServerSocket() {
} }
} }
void Server::addPollfd(int fd, short events, short revents) { void Server::addPollfd(int fd) {
struct pollfd newPollfd; struct pollfd newPollfd;
newPollfd.fd = fd; newPollfd.fd = fd;
newPollfd.events = events; newPollfd.events = POLLIN;
newPollfd.revents = revents; newPollfd.revents = 0;
_fds.push_back(newPollfd); _fds.push_back(newPollfd);
} }
@@ -145,8 +145,8 @@ std::string art =
send(newFd, art.c_str(), art.length(), 0); send(newFd, art.c_str(), art.length(), 0);
send(newFd, passwordRequest.c_str(), passwordRequest.length(), 0); send(newFd, passwordRequest.c_str(), passwordRequest.length(), 0);
addPollfd(newFd, POLLIN, 0); addPollfd(newFd);
_clients.push_back(Client(newFd, inet_ntoa((client_addr.sin_addr)))); _clients.push_back(Client(newFd));
std::cout << "Client <" << newFd << "> Connected" << std::endl; std::cout << "Client <" << newFd << "> Connected" << std::endl;
} }

View File

@@ -84,7 +84,7 @@ class Server {
void createServerSocket(); void createServerSocket();
void bindServerSocket(); void bindServerSocket();
void addPollfd(int fd, short events, short revents); void addPollfd(int fd);
void handleClientConnection(); void handleClientConnection();
void handleClientData(int fd); void handleClientData(int fd);
void clientCleanup(int fd); void clientCleanup(int fd);