Client class now has getters and setters, and server has 4 functions for main loop
This commit is contained in:
@@ -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(); }
|
||||
@@ -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
|
||||
|
||||
@@ -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<pollfd> 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 (this->fds[i].fd == server_fd)
|
||||
acceptClient();
|
||||
else
|
||||
readClient(this->fds[i].fd);
|
||||
}
|
||||
|
||||
if (fds[i].fd == server_fd)
|
||||
}
|
||||
|
||||
return (0);
|
||||
|
||||
}
|
||||
|
||||
void Server::acceptClient()
|
||||
{
|
||||
int client_fd = accept(server_fd, NULL, NULL);
|
||||
std::cout << "Cliente conectado (fd=" << client_fd << ")\n";
|
||||
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);
|
||||
} else {
|
||||
char buf[512] = {0};
|
||||
ssize_t n = recv(fds[i].fd, buf, sizeof(buf) - 1, 0);
|
||||
if(n <= 0)
|
||||
|
||||
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)
|
||||
{
|
||||
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;
|
||||
}
|
||||
break ;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
close(server_fd);
|
||||
|
||||
return (0);
|
||||
|
||||
clients.erase(fd);
|
||||
close(fd);
|
||||
}
|
||||
@@ -22,16 +22,27 @@
|
||||
# include <poll.h>
|
||||
|
||||
# include "../Client/Client.hpp"
|
||||
# include <map>
|
||||
|
||||
class Server
|
||||
{
|
||||
private:
|
||||
int port;
|
||||
std::string password;
|
||||
int server_fd;
|
||||
std::vector<pollfd> fds;
|
||||
std::map<int, Client> 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
|
||||
|
||||
Reference in New Issue
Block a user