merge branch samy
This commit is contained in:
commit
15cceeecd2
7 changed files with 337 additions and 226 deletions
11
Makefile
11
Makefile
|
|
@ -1,14 +1,14 @@
|
||||||
#******************************************************************************#
|
x# **************************************************************************** #
|
||||||
# #
|
# #
|
||||||
# ::: :::::::: #
|
# ::: :::::::: #
|
||||||
# Makefile :+: :+: :+: #
|
# Makefile :+: :+: :+: #
|
||||||
# +:+ +:+ +:+ #
|
# +:+ +:+ +:+ #
|
||||||
# By: omoudni <omoudni@student.42paris.fr> +#+ +:+ +#+ #
|
# By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ #
|
||||||
# +#+#+#+#+#+ +#+ #
|
# +#+#+#+#+#+ +#+ #
|
||||||
# Created: 2025/05/02 15:40:00 by rparodi #+# #+# #
|
# Created: 2025/05/02 15:40:00 by rparodi #+# #+# #
|
||||||
# Updated: 2025/05/20 15:04:42 by rparodi ### ########.fr #
|
# Updated: 2025/05/20 17:06:30 by sben-tay ### ########.fr #
|
||||||
# #
|
# #
|
||||||
#******************************************************************************#
|
# **************************************************************************** #
|
||||||
|
|
||||||
|
|
||||||
# Name
|
# Name
|
||||||
|
|
@ -25,10 +25,9 @@ SESSION = test-irc
|
||||||
|
|
||||||
# Sources
|
# Sources
|
||||||
SRC = sources/core/main.cpp \
|
SRC = sources/core/main.cpp \
|
||||||
sources/core/server.cpp \
|
sources/core/Server.cpp \
|
||||||
sources/core/check.cpp \
|
sources/core/check.cpp \
|
||||||
sources/core/parser.cpp \
|
sources/core/parser.cpp \
|
||||||
sources/core/pollManager.cpp \
|
|
||||||
|
|
||||||
|
|
||||||
INC_DIR = include/core \
|
INC_DIR = include/core \
|
||||||
|
|
|
||||||
159
diagram.puml
159
diagram.puml
|
|
@ -1,5 +1,9 @@
|
||||||
@startuml
|
@startuml "IRC Server - Detailed UML"
|
||||||
|
|
||||||
|
' ========================
|
||||||
|
' ENTRY: main()
|
||||||
|
' ========================
|
||||||
|
class "main()"
|
||||||
|
|
||||||
' ========================
|
' ========================
|
||||||
' CLASS: Server
|
' CLASS: Server
|
||||||
|
|
@ -7,32 +11,50 @@
|
||||||
class Server {
|
class Server {
|
||||||
- _port : int
|
- _port : int
|
||||||
- _password : string
|
- _password : string
|
||||||
- _users : List<User>
|
|
||||||
- _server_fd : int
|
- _server_fd : int
|
||||||
- _state_msg : std::pair<User, e_state>
|
- _poll : PollManager
|
||||||
- _pollManager : PollManager
|
|
||||||
|
|
||||||
+ showInfo() : void
|
+ Server(port : int, password : string)
|
||||||
+ getPort() : int
|
|
||||||
+ start() : void
|
+ start() : void
|
||||||
|
+ getPort() : int
|
||||||
|
+ showInfo() : void
|
||||||
|
}
|
||||||
|
|
||||||
|
' ============================
|
||||||
|
' CLASS: PollManager
|
||||||
|
' ============================
|
||||||
|
class PollManager {
|
||||||
|
- _fds : vector<pollfd>
|
||||||
|
- _users : map<int, User>
|
||||||
|
|
||||||
|
+ addUser(fd : int) : void
|
||||||
|
+ removeUser(fd : int) : void
|
||||||
|
+ updateUser(fd : int, events : short) : void
|
||||||
|
+ pollLoop(server_fd : int) : void
|
||||||
|
+ readFromUser(fd : int) : void
|
||||||
|
+ writeToUser(fd : int) : void
|
||||||
}
|
}
|
||||||
|
|
||||||
' ========================
|
' ========================
|
||||||
' CLASS: User
|
' CLASS: User
|
||||||
' ========================
|
' ========================
|
||||||
class User {
|
class User {
|
||||||
- _id : int
|
- _fd : int
|
||||||
- _name : string
|
- _nickname : string
|
||||||
|
- _username : string
|
||||||
|
- _hostname : string
|
||||||
|
- _readBuffer : string
|
||||||
|
- _writeBuffer : string
|
||||||
- _registered : bool
|
- _registered : bool
|
||||||
- _channels : List<Channel>
|
|
||||||
- _banned : List<Channel>
|
|
||||||
|
|
||||||
+ getId() : int
|
+ getFd() : int
|
||||||
+ getName() : string
|
+ getNickname() : string
|
||||||
|
+ setNickname(name : string) : void
|
||||||
|
+ appendToReadBuffer(data : string) : void
|
||||||
|
+ appendToWriteBuffer(data : string) : void
|
||||||
|
+ extractFullCommand() : string
|
||||||
|
+ isReadyToSend() : bool
|
||||||
+ isRegistered() : bool
|
+ isRegistered() : bool
|
||||||
+ joinChannel(channel : Channel) : void
|
|
||||||
+ leaveChannel(channel : Channel) : void
|
|
||||||
+ sendMessage(to : User, message : string) : void
|
|
||||||
}
|
}
|
||||||
|
|
||||||
' ========================
|
' ========================
|
||||||
|
|
@ -43,49 +65,77 @@ class Channel {
|
||||||
- _password : string
|
- _password : string
|
||||||
- _owner : User
|
- _owner : User
|
||||||
- _operators : List<User>
|
- _operators : List<User>
|
||||||
- _socket_id : int
|
|
||||||
- _topic : string
|
- _topic : string
|
||||||
|
|
||||||
+ getName() : string
|
+ getName() : string
|
||||||
+ setTopic(newTopic : string) : void
|
+ setTopic(newTopic : string) : void
|
||||||
+ addOperator(User : User) : void
|
+ addOperator(user : User) : void
|
||||||
+ removeOperator(User : User) : void
|
+ removeOperator(user : User) : void
|
||||||
|
+ isOperator(user : User) : bool
|
||||||
}
|
}
|
||||||
|
|
||||||
' ========================
|
' ====================================
|
||||||
' CLASS: Commandes
|
' NAMESPACE: CommandDispatcher (static)
|
||||||
' ========================
|
' ====================================
|
||||||
class Commandes {
|
package "CommandDispatcher" <<namespace>> {
|
||||||
- _form_user : User
|
class CommandDispatcher {
|
||||||
- _to_user : User
|
+ dispatchCommand(user : User, line : string) : void
|
||||||
- _channel : Channel
|
+ splitCommand(line : string) : vector<string>
|
||||||
|
}
|
||||||
+ MODE() : void
|
|
||||||
+ KICK() : void
|
|
||||||
+ INVITE() : void
|
|
||||||
+ TOPIC() : void
|
|
||||||
+ JOIN() : void
|
|
||||||
+ PRIVMSG() : void
|
|
||||||
+ LIST() : void
|
|
||||||
+ PASS() : void
|
|
||||||
+ NICK() : void
|
|
||||||
+ USERNAME() : void
|
|
||||||
}
|
}
|
||||||
|
|
||||||
' ========================
|
' =============================
|
||||||
' CLASS: Parser
|
' NAMESPACE: Command
|
||||||
' ========================
|
' =============================
|
||||||
class Parser {
|
package "Command" <<namespace>> {
|
||||||
- _port : int
|
class PASS {
|
||||||
- _password : string
|
+ execute(user : User, args : vector<string>) : void
|
||||||
- _valid : bool
|
}
|
||||||
- _errorMsg : string
|
class NICK {
|
||||||
|
+ execute(user : User, args : vector<string>) : void
|
||||||
+ Parser(argc : int, argv : char**)
|
}
|
||||||
+ isValid() : bool
|
class USER {
|
||||||
+ getPort() : int
|
+ execute(user : User, args : vector<string>) : void
|
||||||
+ getPassword() : string
|
}
|
||||||
+ getErrorMsg() : string
|
class JOIN {
|
||||||
|
+ execute(user : User, args : vector<string>) : void
|
||||||
|
}
|
||||||
|
class PART {
|
||||||
|
+ execute(user : User, args : vector<string>) : void
|
||||||
|
}
|
||||||
|
class PRIVMSG {
|
||||||
|
+ execute(user : User, args : vector<string>) : void
|
||||||
|
}
|
||||||
|
class NOTICE {
|
||||||
|
+ execute(user : User, args : vector<string>) : void
|
||||||
|
}
|
||||||
|
class PING {
|
||||||
|
+ execute(user : User, args : vector<string>) : void
|
||||||
|
}
|
||||||
|
class PONG {
|
||||||
|
+ execute(user : User, args : vector<string>) : void
|
||||||
|
}
|
||||||
|
class QUIT {
|
||||||
|
+ execute(user : User, args : vector<string>) : void
|
||||||
|
}
|
||||||
|
class TOPIC {
|
||||||
|
+ execute(user : User, args : vector<string>) : void
|
||||||
|
}
|
||||||
|
class MODE {
|
||||||
|
+ execute(user : User, args : vector<string>) : void
|
||||||
|
}
|
||||||
|
class INVITE {
|
||||||
|
+ execute(user : User, args : vector<string>) : void
|
||||||
|
}
|
||||||
|
class KICK {
|
||||||
|
+ execute(user : User, args : vector<string>) : void
|
||||||
|
}
|
||||||
|
class LIST {
|
||||||
|
+ execute(user : User, args : vector<string>) : void
|
||||||
|
}
|
||||||
|
class UNKNOWN {
|
||||||
|
+ execute(user : User, args : vector<string>) : void
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
' ========================
|
' ========================
|
||||||
|
|
@ -106,11 +156,10 @@ class PollManager {
|
||||||
' RELATIONS
|
' RELATIONS
|
||||||
' ========================
|
' ========================
|
||||||
|
|
||||||
Server "1" o-- "*" User : _users
|
Server "1" o-- "*" Client : _clients
|
||||||
User "1" o-- "*" Channel : _channels
|
Client "1" o-- "*" Channel : _channels
|
||||||
Channel "1" *-- "1" User : _owner
|
Channel "1" *-- "1" Client : _owner
|
||||||
Channel "1" o-- "*" User : _operators
|
Channel "1" o-- "*" Client : _operators
|
||||||
main ..> Parser : uses
|
main ..> Parser : uses
|
||||||
Server "1" *-- "1" PollManager : pollManager
|
|
||||||
|
|
||||||
@enduml
|
@enduml
|
||||||
22
include/core/PollManager.hpp
Normal file
22
include/core/PollManager.hpp
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <map>
|
||||||
|
#include <vector>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
class PollManager {
|
||||||
|
public:
|
||||||
|
PollManager();
|
||||||
|
~PollManager();
|
||||||
|
|
||||||
|
void addClient(int fd);
|
||||||
|
void removeClient(int fd);
|
||||||
|
void updateServer(int fd);
|
||||||
|
void pollLoop(int server_fd);
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
std::vector<struct pollfd> _fds;
|
||||||
|
std::map<int, std::string> _buffers;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
@ -1,32 +1,36 @@
|
||||||
/******************************************************************************/
|
/* ************************************************************************** */
|
||||||
/* */
|
/* */
|
||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* server.hpp :+: :+: :+: */
|
/* server.hpp :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: omoudni <omoudni@student.42paris.fr> +#+ +:+ +#+ */
|
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/05/13 11:06:56 by rparodi #+# #+# */
|
/* Created: 2025/05/13 11:06:56 by rparodi #+# #+# */
|
||||||
/* Updated: 2025/05/19 20:12:46 by omoudni ### ########.fr */
|
/* Updated: 2025/05/20 17:11:56 by sben-tay ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/******************************************************************************/
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "core.hpp"
|
#include "core.hpp"
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include "PollManager.hpp"
|
||||||
|
|
||||||
class Server {
|
class Server {
|
||||||
private:
|
public:
|
||||||
unsigned short int _port;
|
|
||||||
std::string _password;
|
|
||||||
int server_fd;
|
|
||||||
PollManager _pollManager;
|
|
||||||
public:
|
|
||||||
Server();
|
Server();
|
||||||
Server(int port, const std::string &password);
|
Server(int port, const std::string &password);
|
||||||
~Server();
|
~Server();
|
||||||
void showInfo() const;
|
|
||||||
unsigned short int getPort() const;
|
unsigned short int getPort() const;
|
||||||
|
void showInfo() const;
|
||||||
void setServerFd(int fd);
|
void setServerFd(int fd);
|
||||||
void start();
|
void start();
|
||||||
|
|
||||||
|
private:
|
||||||
|
unsigned short int _port;
|
||||||
|
int _serverFd;
|
||||||
|
std::string _password;
|
||||||
|
PollManager _poll;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
80
sources/core/PollManager.cpp
Normal file
80
sources/core/PollManager.cpp
Normal file
|
|
@ -0,0 +1,80 @@
|
||||||
|
#include "PollManager.hpp"
|
||||||
|
#include <iostream>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <cstring>
|
||||||
|
#include <netinet/in.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <poll.h>
|
||||||
|
#include <cstdlib>
|
||||||
|
|
||||||
|
PollManager::PollManager() {}
|
||||||
|
|
||||||
|
PollManager::~PollManager() {
|
||||||
|
for (size_t i = 0; i < _fds.size(); ++i) {
|
||||||
|
close(_fds[i].fd);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void PollManager::pollLoop(int server_fd) {
|
||||||
|
|
||||||
|
struct pollfd server_pollfd;
|
||||||
|
server_pollfd.fd = server_fd;
|
||||||
|
server_pollfd.events = POLLIN;
|
||||||
|
_fds.push_back(server_pollfd);
|
||||||
|
|
||||||
|
std::cout << "Serveur prêt à accepter des connexions..." << std::endl;
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
int poll_count = poll(&_fds[0], _fds.size(), -1);
|
||||||
|
if (poll_count == -1) {
|
||||||
|
std::cerr << "poll error\n" << std::endl;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
for (size_t i = 0; i < _fds.size(); ++i) {
|
||||||
|
int fd = _fds[i].fd;
|
||||||
|
|
||||||
|
if ((fd == server_fd) && (_fds[i].revents & POLLIN)) {
|
||||||
|
int client_fd = accept(server_fd, NULL, NULL);
|
||||||
|
if (client_fd == -1) {
|
||||||
|
std::cerr << "Error accept()" << std::endl;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
addClient(client_fd);
|
||||||
|
}
|
||||||
|
else if (_fds[i].revents & POLLIN) {
|
||||||
|
char buffer[1024];
|
||||||
|
ssize_t bytes = recv(fd, buffer, sizeof(buffer) -1, 0);
|
||||||
|
if (bytes > 0) {
|
||||||
|
buffer[bytes] = '\0';
|
||||||
|
_buffers[fd] += buffer;
|
||||||
|
std::cout << "Client " << fd << " send : " << buffer;
|
||||||
|
} else {
|
||||||
|
std::cout << "Client " << fd << " disconected." << std::endl;
|
||||||
|
// removeClient(fd);
|
||||||
|
--i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void PollManager::addClient(int fd) {
|
||||||
|
struct pollfd pfd;
|
||||||
|
pfd.fd = fd;
|
||||||
|
pfd.events = POLLIN;
|
||||||
|
_fds.push_back(pfd);
|
||||||
|
_buffers[fd] = "";
|
||||||
|
std::cout << "Client connecté (fd " << fd << ")" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void PollManager::removeClient(int fd) {
|
||||||
|
for (size_t i = 0; i < _fds.size(); ++i) {
|
||||||
|
if (_fds[i].fd == fd) {
|
||||||
|
_fds.erase(_fds.begin() + i);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_buffers.erase(fd);
|
||||||
|
close(fd);
|
||||||
|
std::cout << "Client retiré (fd " << fd << ")" << std::endl;
|
||||||
|
}
|
||||||
101
sources/core/Server.cpp
Normal file
101
sources/core/Server.cpp
Normal file
|
|
@ -0,0 +1,101 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* Server.cpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/05/13 11:11:07 by rparodi #+# #+# */
|
||||||
|
/* Updated: 2025/05/20 17:11:06 by sben-tay ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "color.hpp"
|
||||||
|
#include "server.hpp"
|
||||||
|
#include "core.hpp"
|
||||||
|
#include <iostream>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <netinet/in.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief The default constructor of the Server class.
|
||||||
|
*/
|
||||||
|
Server::Server() : _port(0), _password("") {
|
||||||
|
if (DEBUG)
|
||||||
|
std::cerr << CLR_MAGENTA << "Debug: Thanks to use the constructor with port and password !" << std::endl;
|
||||||
|
std::cout << CLR_GREY << "Info: Server default constructor called" << CLR_RESET << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief The constructor of the Server class.
|
||||||
|
*
|
||||||
|
* @param port The port given by the user (argv[1])
|
||||||
|
* @param password The password given by the user (argv[2])
|
||||||
|
*
|
||||||
|
* @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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief The default destructor of the Server class.
|
||||||
|
*/
|
||||||
|
Server::~Server() {
|
||||||
|
std::cout << CLR_GREY << "Info: Server destructor called" << CLR_RESET << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Server::start() {
|
||||||
|
_serverFd = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
|
if (_serverFd == -1) {
|
||||||
|
std::cerr << "Erreur socket" << std::endl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
sockaddr_in addr;
|
||||||
|
std::memset(&addr, 0, sizeof(addr));
|
||||||
|
addr.sin_family = AF_INET;
|
||||||
|
addr.sin_addr.s_addr = INADDR_ANY;
|
||||||
|
addr.sin_port = htons(_port);
|
||||||
|
|
||||||
|
if (bind(_serverFd, (sockaddr*)&addr, sizeof(addr)) == -1 ||
|
||||||
|
listen(_serverFd, 10) == -1) {
|
||||||
|
std::cerr << "Erreur bind/listen" << std::endl;
|
||||||
|
close(_serverFd);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << "Serveur lancé sur le port " << _port << std::endl;
|
||||||
|
|
||||||
|
_poll.pollLoop(_serverFd);
|
||||||
|
close(_serverFd);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Show the server settings.
|
||||||
|
*
|
||||||
|
* @note This function is used to show the server settings.
|
||||||
|
* 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;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief The getter of the port.
|
||||||
|
*
|
||||||
|
* @return the port of the server
|
||||||
|
*/
|
||||||
|
unsigned short int Server::getPort() const {
|
||||||
|
return this->_port;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -1,144 +0,0 @@
|
||||||
/******************************************************************************/
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* server.cpp :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: omoudni <omoudni@student.42paris.fr> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2025/05/13 11:11:07 by rparodi #+# #+# */
|
|
||||||
/* Updated: 2025/05/19 20:16:00 by omoudni ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/******************************************************************************/
|
|
||||||
|
|
||||||
#include "color.hpp"
|
|
||||||
#include "server.hpp"
|
|
||||||
#include "core.hpp"
|
|
||||||
#include <iostream>
|
|
||||||
#include <sys/socket.h>
|
|
||||||
#include <netinet/in.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
/**
|
|
||||||
* @brief The default constructor of the Server class.
|
|
||||||
*/
|
|
||||||
Server::Server() : _port(0), _password("") {
|
|
||||||
if (DEBUG)
|
|
||||||
std::cerr << CLR_MAGENTA << "Debug: Thanks to use the constructor with port and password !" << std::endl;
|
|
||||||
std::cout << CLR_GREY << "Info: Server default constructor called" << CLR_RESET << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief The constructor of the Server class.
|
|
||||||
*
|
|
||||||
* @param port The port given by the user (argv[1])
|
|
||||||
* @param password The password given by the user (argv[2])
|
|
||||||
*
|
|
||||||
* @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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief The default destructor of the Server class.
|
|
||||||
*/
|
|
||||||
Server::~Server() {
|
|
||||||
std::cout << CLR_GREY << "Info: Server destructor called" << CLR_RESET << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void Server::start() {
|
|
||||||
int fd = socket(AF_INET, SOCK_STREAM, 0);
|
|
||||||
if (server_fd == -1) {
|
|
||||||
std::cerr << CLR_RED << "Error: Failed to create socket" << CLR_RESET << std::endl;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
setServerFd(fd);
|
|
||||||
struct sockaddr_in server_addr;
|
|
||||||
server_addr.sin_family = AF_INET;
|
|
||||||
server_addr.sin_addr.s_addr = INADDR_ANY;
|
|
||||||
server_addr.sin_port = htons(this->_port);
|
|
||||||
if (bind(server_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
|
|
||||||
std::cerr << CLR_RED << "Error: Failed to bind socket" << CLR_RESET << std::endl;
|
|
||||||
close(server_fd);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (listen(server_fd, 5) == -1) {
|
|
||||||
std::cerr << CLR_RED << "Error: Failed to listen on socket" << CLR_RESET << std::endl;
|
|
||||||
close(server_fd);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
_pollManager.addFd(server_fd, POLLIN);
|
|
||||||
std::cout << CLR_GREEN << "Server started on port " << this->_port << CLR_RESET << std::endl;
|
|
||||||
std::cout << CLR_GREEN << "Waiting for clients..." << CLR_RESET << std::endl;
|
|
||||||
while (true) {
|
|
||||||
int n = _pollManager.pollEvents();
|
|
||||||
if (n < 0) {
|
|
||||||
std::cerr << CLR_RED << "Poll error" << CLR_RESET << std::endl;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
const std::vector<struct pollfd>& fds = _pollManager.getFds();
|
|
||||||
for (size_t i = 0; i < fds.size(); ++i) {
|
|
||||||
if (fds[i].revents & POLLIN) {
|
|
||||||
if (fds[i].fd == server_fd) {
|
|
||||||
// Accept new client
|
|
||||||
int client_fd = accept(server_fd, NULL, NULL);
|
|
||||||
if (client_fd != -1) {
|
|
||||||
_pollManager.addFd(client_fd, POLLIN);
|
|
||||||
std::cout << CLR_GREEN << "Client connected" << CLR_RESET << std::endl;
|
|
||||||
std::cout << "Welcome to IRC. Your client_id is: " << client_fd << std::endl;
|
|
||||||
} else {
|
|
||||||
std::cerr << CLR_RED << "Error: Failed to accept client" << CLR_RESET << std::endl;
|
|
||||||
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Handle client communication
|
|
||||||
char buffer[1024];
|
|
||||||
ssize_t bytes_received = recv(fds[i].fd, buffer, sizeof(buffer) - 1, 0);
|
|
||||||
if (bytes_received <= 0) {
|
|
||||||
// Client disconnected
|
|
||||||
// if it's negative, it means an error occurred, maybe print it with perror?
|
|
||||||
std::cout << CLR_RED << "Client disconnected" << CLR_RESET << std::endl;
|
|
||||||
close(fds[i].fd);
|
|
||||||
_pollManager.removeFd(fds[i].fd);
|
|
||||||
} else {
|
|
||||||
buffer[bytes_received] = '\0';
|
|
||||||
std::cout << "Received: " << buffer << std::endl;
|
|
||||||
// Optionally: send response to client here
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
close(server_fd);
|
|
||||||
std::cout << CLR_GREEN << "Server stopped" << CLR_RESET << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Show the server settings.
|
|
||||||
*
|
|
||||||
* @note This function is used to show the server settings.
|
|
||||||
* 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;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief The getter of the port.
|
|
||||||
*
|
|
||||||
* @return the port of the server
|
|
||||||
*/
|
|
||||||
unsigned short int Server::getPort() const {
|
|
||||||
return this->_port;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Server::setServerFd(int fd) {
|
|
||||||
this->server_fd = fd;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue