little edits
This commit is contained in:
parent
1e66d6c33e
commit
a51264cefa
3 changed files with 59 additions and 47 deletions
|
|
@ -6,7 +6,7 @@
|
|||
/* By: omoudni <omoudni@student.42paris.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/05/20 21:50:32 by rparodi #+# #+# */
|
||||
/* Updated: 2025/05/21 21:19:47 by omoudni ### ########.fr */
|
||||
/* Updated: 2025/05/22 18:44:35 by omoudni ### ########.fr */
|
||||
/* */
|
||||
/******************************************************************************/
|
||||
|
||||
|
|
@ -15,18 +15,20 @@
|
|||
#include "core.hpp"
|
||||
|
||||
class User;
|
||||
class Server {
|
||||
private:
|
||||
int _port;
|
||||
int _serverFd;
|
||||
std::string _password;
|
||||
PollManager _pollManager;
|
||||
std::map<int, User*> _users;
|
||||
|
||||
public:
|
||||
Server(int port, const std::string &password);
|
||||
~Server();
|
||||
void start();
|
||||
unsigned short int getPort() const;
|
||||
void showInfo() const;
|
||||
class Server
|
||||
{
|
||||
private:
|
||||
int _port;
|
||||
int _serverFd;
|
||||
std::string _password;
|
||||
PollManager _pollManager;
|
||||
std::map<int, User *> _users;
|
||||
|
||||
public:
|
||||
Server(int port, const std::string &password);
|
||||
~Server();
|
||||
void start();
|
||||
unsigned short int getPort() const;
|
||||
void showInfo() const;
|
||||
void printUsers() const;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -29,8 +29,7 @@ void PollManager::pollLoop(int server_fd, std::vector<int> &newClients, std::vec
|
|||
}
|
||||
for (size_t i = 0; i < _fds.size(); ++i)
|
||||
{
|
||||
short unsigned fd = _fds[i].fd;
|
||||
std::cout << "I'm here 1" << std::endl;
|
||||
short unsigned fd = _fds[i].fd;
|
||||
if ((fd == server_fd) && (_fds[i].revents & POLLIN))
|
||||
{
|
||||
int client_fd = accept(server_fd, NULL, NULL);
|
||||
|
|
@ -39,11 +38,11 @@ void PollManager::pollLoop(int server_fd, std::vector<int> &newClients, std::vec
|
|||
std::cerr << "Error accept()" << std::endl;
|
||||
continue;
|
||||
}
|
||||
std::cout << "I'm here 2" << std::endl;
|
||||
addClient(client_fd);
|
||||
addClient(client_fd);
|
||||
newClients.push_back(client_fd);
|
||||
} else if (_fds[i].revents & POLLIN) {
|
||||
std::cout << "I'm here 3" << std::endl;
|
||||
}
|
||||
else if (_fds[i].revents & POLLIN)
|
||||
{
|
||||
char buffer[1024];
|
||||
ssize_t bytes = recv(fd, buffer, sizeof(buffer) - 1, 0);
|
||||
if (bytes > 0)
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: omoudni <omoudni@student.42paris.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/05/13 11:11:07 by rparodi #+# #+# */
|
||||
/* Updated: 2025/05/22 17:31:42 by omoudni ### ########.fr */
|
||||
/* Updated: 2025/05/22 18:45:41 by omoudni ### ########.fr */
|
||||
/* */
|
||||
/******************************************************************************/
|
||||
|
||||
|
|
@ -29,24 +29,28 @@
|
|||
*
|
||||
* @note Thanks to check the port / password before give them to the constructor.
|
||||
*/
|
||||
Server::Server(int port, const std::string &password) : _port(port), _password(password) {
|
||||
std::cout << CLR_GREY << "Info: Server constructor called" << CLR_RESET << std::endl;
|
||||
Server::Server(int port, const std::string &password) : _port(port), _password(password)
|
||||
{
|
||||
std::cout << CLR_GREY << "Info: Server constructor called" << CLR_RESET << std::endl;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief The default destructor of the Server class.
|
||||
*/
|
||||
Server::~Server() {
|
||||
std::cout << CLR_GREY << "Info: Server destructor called" << CLR_RESET << std::endl;
|
||||
if (_serverFd != -1) {
|
||||
Server::~Server()
|
||||
{
|
||||
std::cout << CLR_GREY << "Info: Server destructor called" << CLR_RESET << std::endl;
|
||||
if (_serverFd != -1)
|
||||
{
|
||||
close(_serverFd);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Server::start() {
|
||||
void Server::start()
|
||||
{
|
||||
_serverFd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (_serverFd == -1) {
|
||||
if (_serverFd == -1)
|
||||
{
|
||||
std::cerr << "Erreur socket" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
|
@ -57,8 +61,9 @@ void Server::start() {
|
|||
addr.sin_addr.s_addr = INADDR_ANY;
|
||||
addr.sin_port = htons(_port);
|
||||
|
||||
if (bind(_serverFd, (sockaddr*)&addr, sizeof(addr)) == -1 ||
|
||||
listen(_serverFd, 10) == -1) {
|
||||
if (bind(_serverFd, (sockaddr *)&addr, sizeof(addr)) == -1 ||
|
||||
listen(_serverFd, 10) == -1)
|
||||
{
|
||||
std::cerr << "Erreur bind/listen" << std::endl;
|
||||
close(_serverFd);
|
||||
return;
|
||||
|
|
@ -71,15 +76,18 @@ void Server::start() {
|
|||
std::vector<std::pair<int, std::string> > readyClients;
|
||||
while (true)
|
||||
{
|
||||
printUsers();
|
||||
newClients.clear();
|
||||
disconnected.clear();
|
||||
readyClients.clear();
|
||||
_pollManager.pollLoop(_serverFd, newClients, disconnected,
|
||||
readyClients);
|
||||
std::cout << "Poll loop finished" << std::endl;
|
||||
// Handle new clients
|
||||
std::cout << "New clients: " << newClients.size() << std::endl;
|
||||
for (size_t i = 0; i < newClients.size(); ++i)
|
||||
_users[newClients[i]] = new User(newClients[i]);
|
||||
{
|
||||
_users[newClients[i]] = new User(newClients[i]);
|
||||
}
|
||||
// Handle disconnected clients
|
||||
for (size_t i = 0; i < disconnected.size(); ++i)
|
||||
{
|
||||
|
|
@ -93,15 +101,11 @@ void Server::start() {
|
|||
if (_users.count(fd))
|
||||
{
|
||||
_users[fd]->appendToReadBuffer(data);
|
||||
//print users
|
||||
std::cout << "User " << fd << " is registered: " << _users[fd]->isRegistered() << std::endl;
|
||||
std::cout << "Received data from fd " << fd << ": " << data << std::endl;
|
||||
std::string cmd;
|
||||
while (!(cmd = _users[fd]->extractFullCommand()).empty())
|
||||
{
|
||||
// This prints every command/message received from any client
|
||||
std::cout << "Client " << fd << " says: " << cmd << std::endl;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -118,12 +122,12 @@ void Server::start() {
|
|||
* It is used for debug purpose.
|
||||
*/
|
||||
|
||||
void Server::showInfo() const {
|
||||
std::cout << std::endl;
|
||||
std::cout << CLR_BLUE << "IRCSettings:" << CLR_RESET << std::endl;
|
||||
std::cout << CLR_BLUE << "\t- Port:\t\t" << CLR_GOLD << this->_port << CLR_RESET << std::endl;
|
||||
std::cout << CLR_BLUE << "\t- Password:\t" << CLR_GOLD << this->_password << CLR_RESET << std::endl;
|
||||
|
||||
void Server::showInfo() const
|
||||
{
|
||||
std::cout << std::endl;
|
||||
std::cout << CLR_BLUE << "IRCSettings:" << CLR_RESET << std::endl;
|
||||
std::cout << CLR_BLUE << "\t- Port:\t\t" << CLR_GOLD << this->_port << CLR_RESET << std::endl;
|
||||
std::cout << CLR_BLUE << "\t- Password:\t" << CLR_GOLD << this->_password << CLR_RESET << std::endl;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -131,7 +135,14 @@ void Server::showInfo() const {
|
|||
*
|
||||
* @return the port of the server
|
||||
*/
|
||||
unsigned short int Server::getPort() const {
|
||||
return this->_port;
|
||||
}
|
||||
unsigned short int Server::getPort() const { return this->_port; }
|
||||
|
||||
void Server::printUsers() const
|
||||
{
|
||||
std::cout << "Connected users:" << std::endl;
|
||||
for (std::map<int, User *>::const_iterator it = _users.begin(); it != _users.end(); ++it)
|
||||
{
|
||||
std::cout << "User fd: " << it->first << std::endl;
|
||||
std::cout << "Nickname: " << it->second->getNickname() << std::endl;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue