diff --git a/Client/Client.cpp b/Client/Client.cpp index 84d49c7..f32d895 100644 --- a/Client/Client.cpp +++ b/Client/Client.cpp @@ -18,16 +18,33 @@ Client::Client() { - std::cout << "Client default constructor called" << std::endl; + // std::cout << "Client default constructor called" << std::endl; } Client::Client(int fd) : fd(fd), registered(false), authenticated(false) { - std::cout << "Client will use fd: " << fd << std::endl; + // std::cout << "Client will use fd: " << fd << std::endl; } Client::~Client() { - std::cout << "Client destructor called" << std::endl; + // std::cout << "Client destructor called" << std::endl; } +// Getters +int Client::getFd() const { return (this->fd); } +std::string Client::getNick() const { return (this->nick); } +std::string Client::getUsername() const { return (this->username); } +std::string Client::getRealname() const { return (this->realname); } +std::string Client::getBuffer() const { return (this->buffer); } +bool Client::isAuthenticated() const { return (this->authenticated); } +bool Client::isRegistered() const { return (this->registered); } + +// Seters +void Client::setNick(std::string nick) { this->nick = nick; } +void Client::setUsername(std::string username) { this->username = username; } +void Client::setRealname(std::string realname) { this->realname = realname; } +void Client::setAuthenticated(bool value) { this->authenticated = value; } +void Client::setRegistered(bool value) { this->authenticated = value; } +void Client::appendBuffer(std::string data) { this->buffer += data; } +void Client::clearBuffer() { this->buffer.clear(); } \ No newline at end of file diff --git a/Client/Client.hpp b/Client/Client.hpp index e55ae3b..7ccfb7c 100644 --- a/Client/Client.hpp +++ b/Client/Client.hpp @@ -31,6 +31,27 @@ class Client Client(); Client(int fd); ~Client(); + + + + // Getters + int getFd() const; + std::string getNick() const; + std::string getUsername() const; + std::string getRealname() const; + std::string getBuffer() const; + bool isAuthenticated() const; + bool isRegistered() const; + + // Seters + void setNick(std::string nick); + void setUsername(std::string username); + void setRealname(std::string realname); + void setAuthenticated(bool value); + void setRegistered(bool value); + void appendBuffer(std::string data); + void clearBuffer(); + }; #endif diff --git a/Server/Server.cpp b/Server/Server.cpp index 84aae6d..c0c9534 100644 --- a/Server/Server.cpp +++ b/Server/Server.cpp @@ -21,9 +21,34 @@ Server::Server() std::cout << "Server default constructor called" << std::endl; } -Server::Server(int port) : port(port) +Server::Server(int port, std::string password) +: port(port), password(password), server_fd(-1) { + server_fd = socket(AF_INET, SOCK_STREAM, 0); + if (server_fd < 0) + throw std::runtime_error("Error: socket()"); + + int opt = 1; + setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); + + sockaddr_in addr = {}; + addr.sin_family = AF_INET; + addr.sin_port = htons(port); + addr.sin_addr.s_addr = INADDR_ANY; + + if (bind(server_fd, (sockaddr*)&addr, sizeof(addr)) < 0) + throw std::runtime_error("Error: bind()"); + if (listen(server_fd, 10) < 0) + throw std::runtime_error("Error: listen()"); + + pollfd server_pfd; + server_pfd.fd = server_fd; + server_pfd.events = POLLIN; + server_pfd.revents = 0; + fds.push_back(server_pfd); + std::cout << "Server will use port: " << port << std::endl; + } Server::~Server() @@ -33,63 +58,72 @@ Server::~Server() int Server::run() { - int server_fd = socket(AF_INET, SOCK_STREAM, 0); - - sockaddr_in addr = {}; - addr.sin_family = AF_INET; - addr.sin_port = htons(this->port); - addr.sin_addr.s_addr = INADDR_ANY; - - bind(server_fd, (sockaddr*)&addr, sizeof(addr)); - listen(server_fd, 1); - std::cout << "Esperando conexion" << std::endl; - - std::vector fds; - - pollfd server_pfd; - server_pfd.fd = server_fd; - server_pfd.events = POLLIN; - server_pfd.revents = 0; - fds.push_back(server_pfd); - while (true) { - poll(fds.data(), fds.size(), -1); + poll(this->fds.data(), this->fds.size(), -1); - for(unsigned int i = 0; i < fds.size(); i++) + for(unsigned int i = 0; i < this->fds.size(); i++) { - if (!(fds[i].revents & POLLIN)) + if (!(this->fds[i].revents & POLLIN)) continue ; - - if (fds[i].fd == server_fd) - { - int client_fd = accept(server_fd, NULL, NULL); - std::cout << "Cliente conectado (fd=" << client_fd << ")\n"; - - pollfd client_pfd; - client_pfd.fd = client_fd; - client_pfd.events = POLLIN; - client_pfd.revents = 0; - fds.push_back(client_pfd); - } else { - char buf[512] = {0}; - ssize_t n = recv(fds[i].fd, buf, sizeof(buf) - 1, 0); - if(n <= 0) - { - std::cout << "Cliente (fd=" << fds[i].fd << ") desconectado\n"; - close(fds[i].fd); - fds.erase(fds.begin() + i); - i--; - } else { - std::cout << "Mensaje de fd=" << fds[i].fd << ": " << buf; - } - } + if (this->fds[i].fd == server_fd) + acceptClient(); + else + readClient(this->fds[i].fd); } } - close(server_fd); - return (0); } + +void Server::acceptClient() +{ + int client_fd = accept(server_fd, NULL, NULL); + if (client_fd < 0) + return ; + + // AƱadimos al poll + pollfd client_pfd; + client_pfd.fd = client_fd; + client_pfd.events = POLLIN; + client_pfd.revents = 0; + fds.push_back(client_pfd); + + clients.insert(std::make_pair(client_fd, Client(client_fd))); + + std::cout << "Nuevo cliente (fd=" << client_fd << ")\n"; +} + +void Server::readClient(int fd) +{ + char buf[512] = {0}; + int r = recv(fd, buf, sizeof(buf) - 1, 0); + + if (r <= 0) + { + removeClient(fd); + return ; + } + + clients[fd].appendBuffer(buf); + std::cout << "fd=" << fd << " dice: " << buf; +} + +void Server::removeClient(int fd) +{ + std::cout << "Cliente (fd=" << fd << ") desconectado\n"; + + for (size_t i = 0; i < fds.size(); i++) + { + if (fds[i].fd == fd) + { + fds.erase(fds.begin() + i); + break ; + } + } + + clients.erase(fd); + close(fd); +} \ No newline at end of file diff --git a/Server/Server.hpp b/Server/Server.hpp index 92933ee..02f9dbd 100644 --- a/Server/Server.hpp +++ b/Server/Server.hpp @@ -22,16 +22,27 @@ # include # include "../Client/Client.hpp" +# include class Server { private: - int port; + int port; + std::string password; + int server_fd; + std::vector fds; + std::map clients; + + void acceptClient(); + void readClient(int fd); + void removeClient(int fd); + public: Server(); - Server(int port); + Server(int port, std::string password); ~Server(); int run(); + }; #endif diff --git a/main.cpp b/main.cpp index f344eae..104fd76 100644 --- a/main.cpp +++ b/main.cpp @@ -14,12 +14,12 @@ int main(int argc, char **argv) { - if (argc != 2) + if (argc != 3) { std::cout << "Port is needed" << std::endl; return (1); } - Server sv(atoi(argv[1])); + Server sv(atoi(argv[1]), argv[2]); sv.run();