fixed data not received from client

This commit is contained in:
ouafabulous 2025-05-22 17:41:29 +02:00
parent bfe88daf3e
commit 1e66d6c33e
6 changed files with 66 additions and 52 deletions

View file

@ -6,7 +6,7 @@
# By: omoudni <omoudni@student.42paris.fr> +#+ +:+ +#+ # # By: omoudni <omoudni@student.42paris.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2025/05/02 15:40:00 by rparodi #+# #+# # # Created: 2025/05/02 15:40:00 by rparodi #+# #+# #
# Updated: 2025/05/21 21:51:13 by omoudni ### ########.fr # # Updated: 2025/05/22 17:35:48 by omoudni ### ########.fr #
# # # #
#******************************************************************************# #******************************************************************************#
@ -30,7 +30,7 @@ SRC = sources/core/logs.cpp \
sources/core/parser.cpp \ sources/core/parser.cpp \
sources/core/main.cpp \ sources/core/main.cpp \
sources/core/Server.cpp \ sources/core/Server.cpp \
sources/core/user.cpp \ sources/user/user.cpp \
sources/channel/channel.cpp sources/channel/channel.cpp
INC_DIR = include/core \ INC_DIR = include/core \

View file

@ -10,12 +10,13 @@ class "main()"
' ======================== ' ========================
class Server { class Server {
- _port : int - _port : int
- _serverFd : int
- _password : string - _password : string
- _server_fd : int - _pollManager : PollManager
- _poll : PollManager - _users : map<int, User*>
- _users : map<int, User>
+ Server(port : int, password : string) + Server(port : int, password : string)
+ ~Server()
+ start() : void + start() : void
+ getPort() : int + getPort() : int
+ showInfo() : void + showInfo() : void
@ -27,36 +28,41 @@ class Server {
class PollManager { class PollManager {
- _fds : vector<pollfd> - _fds : vector<pollfd>
+ addUser(fd : int) : void + PollManager()
+ removeUser(fd : int) : void + ~PollManager()
+ updateUser(fd : int, events : short) : void + setServerFd(fd : int) : void
+ pollLoop(server_fd : int, newClients : std::vector<int>, disconnected : std::vector<int>) : void + addClient(fd : short unsigned) : void
+ readFromUser(fd : int) : void + removeClient(fd : short unsigned) : void
+ writeToUser(fd : int) : void + updateServer(fd : short unsigned) : void
+ pollLoop(server_fd : int, newClients : vector<int>, disconnected : vector<int>, readyClients : vector<pair<int, string>>) : void
} }
' ======================== ' ========================
' CLASS: User ' CLASS: User
' ======================== ' ========================
class User { class User {
- _fd : int - _fd : short unsigned int
- _nickname : string
- _username : string
- _hostname : string
- _readBuffer : string
- _writeBuffer : string
- _registered : bool - _registered : bool
- _nickname : string
- _hostname : string
- _read_buffer : string
- _write_buffer : string
- _username : string
- _hasNick : bool
- _hasUser : bool
+ getFd() : int + User(fd : short unsigned int)
+ getNickname() : string + getFd() : short unsigned int
+ setNickname(name : string) : void
+ appendToReadBuffer(data : string) : void
+ appendToWriteBuffer(data : string) : void
+ extractFullCommand() : string
+ isReadyToSend() : bool + isReadyToSend() : bool
+ isRegistered() : bool + isRegistered() : bool
+ getNickname() : string
+ extractFullCommand() : string
+ appendToReadBuffer(data : string) : void
+ appendToWriteBuffer(data : string) : void
+ setNickname(nickname : string) : void
+ setUsername(username : string) : void
+ checkRegistration() : void
} }
' ======================== ' ========================
' CLASS: Channel ' CLASS: Channel
' ======================== ' ========================

View file

@ -6,7 +6,7 @@
/* By: omoudni <omoudni@student.42paris.fr> +#+ +:+ +#+ */ /* By: omoudni <omoudni@student.42paris.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/19 19:15:13 by omoudni #+# #+# */ /* Created: 2025/05/19 19:15:13 by omoudni #+# #+# */
/* Updated: 2025/05/21 21:34:20 by omoudni ### ########.fr */ /* Updated: 2025/05/22 17:30:00 by omoudni ### ########.fr */
/* */ /* */
/******************************************************************************/ /******************************************************************************/
@ -22,6 +22,7 @@ public:
PollManager(); PollManager();
~PollManager(); ~PollManager();
void setServerFd(int fd);
void addClient(short unsigned fd); void addClient(short unsigned fd);
void removeClient(short unsigned fd); void removeClient(short unsigned fd);
void updateServer(short unsigned fd); void updateServer(short unsigned fd);

View file

@ -20,15 +20,6 @@ PollManager::~PollManager()
void PollManager::pollLoop(int server_fd, std::vector<int> &newClients, std::vector<int> &disconnected, std::vector<std::pair<int, std::string> > &readyClients) void PollManager::pollLoop(int server_fd, std::vector<int> &newClients, std::vector<int> &disconnected, std::vector<std::pair<int, std::string> > &readyClients)
{ {
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); int poll_count = poll(&_fds[0], _fds.size(), -1);
if (poll_count == -1) if (poll_count == -1)
{ {
@ -39,7 +30,7 @@ void PollManager::pollLoop(int server_fd, std::vector<int> &newClients, std::vec
for (size_t i = 0; i < _fds.size(); ++i) for (size_t i = 0; i < _fds.size(); ++i)
{ {
short unsigned fd = _fds[i].fd; short unsigned fd = _fds[i].fd;
std::cout << "I'm here 1" << std::endl;
if ((fd == server_fd) && (_fds[i].revents & POLLIN)) if ((fd == server_fd) && (_fds[i].revents & POLLIN))
{ {
int client_fd = accept(server_fd, NULL, NULL); int client_fd = accept(server_fd, NULL, NULL);
@ -48,17 +39,19 @@ void PollManager::pollLoop(int server_fd, std::vector<int> &newClients, std::vec
std::cerr << "Error accept()" << std::endl; std::cerr << "Error accept()" << std::endl;
continue; continue;
} }
std::cout << "I'm here 2" << std::endl;
addClient(client_fd); addClient(client_fd);
newClients.push_back(client_fd); newClients.push_back(client_fd);
} } else if (_fds[i].revents & POLLIN) {
else if (_fds[i].revents & POLLIN) std::cout << "I'm here 3" << std::endl;
{
char buffer[1024]; char buffer[1024];
ssize_t bytes = recv(fd, buffer, sizeof(buffer) - 1, 0); ssize_t bytes = recv(fd, buffer, sizeof(buffer) - 1, 0);
if (bytes > 0) if (bytes > 0)
{ {
buffer[bytes] = '\0'; buffer[bytes] = '\0';
readyClients.push_back(std::make_pair(fd, std::string(buffer))); readyClients.push_back(std::make_pair(fd, std::string(buffer)));
std::cout << "Received data from fd " << fd << ": " << buffer << std::endl;
std::cout << std::flush;
} }
else else
{ {
@ -93,3 +86,11 @@ void PollManager::removeClient(short unsigned fd)
close(fd); close(fd);
std::cout << "Client disconnected (fd " << fd << ")" << std::endl; std::cout << "Client disconnected (fd " << fd << ")" << std::endl;
} }
void PollManager::setServerFd(int fd)
{
struct pollfd pfd;
pfd.fd = fd;
pfd.events = POLLIN;
_fds.push_back(pfd);
}

View file

@ -6,13 +6,14 @@
/* By: omoudni <omoudni@student.42paris.fr> +#+ +:+ +#+ */ /* By: omoudni <omoudni@student.42paris.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/13 11:11:07 by rparodi #+# #+# */ /* Created: 2025/05/13 11:11:07 by rparodi #+# #+# */
/* Updated: 2025/05/21 21:50:03 by omoudni ### ########.fr */ /* Updated: 2025/05/22 17:31:42 by omoudni ### ########.fr */
/* */ /* */
/******************************************************************************/ /******************************************************************************/
#include "color.hpp" #include "color.hpp"
#include "server.hpp" #include "server.hpp"
#include "core.hpp" #include "core.hpp"
#include "PollManager.hpp"
#include <iostream> #include <iostream>
#include <sys/socket.h> #include <sys/socket.h>
#include <netinet/in.h> #include <netinet/in.h>
@ -64,6 +65,7 @@ void Server::start() {
} }
std::cout << "Serveur lancé sur le port " << _port << std::endl; std::cout << "Serveur lancé sur le port " << _port << std::endl;
_pollManager.setServerFd(_serverFd);
std::vector<int> newClients; std::vector<int> newClients;
std::vector<int> disconnected; std::vector<int> disconnected;
std::vector<std::pair<int, std::string> > readyClients; std::vector<std::pair<int, std::string> > readyClients;
@ -72,8 +74,9 @@ void Server::start() {
newClients.clear(); newClients.clear();
disconnected.clear(); disconnected.clear();
readyClients.clear(); readyClients.clear();
_pollManager.pollLoop(_serverFd, newClients, disconnected, readyClients); _pollManager.pollLoop(_serverFd, newClients, disconnected,
readyClients);
std::cout << "Poll loop finished" << std::endl;
// Handle new clients // Handle new clients
for (size_t i = 0; i < newClients.size(); ++i) for (size_t i = 0; i < newClients.size(); ++i)
_users[newClients[i]] = new User(newClients[i]); _users[newClients[i]] = new User(newClients[i]);

View file

@ -6,7 +6,7 @@
/* By: omoudni <omoudni@student.42paris.fr> +#+ +:+ +#+ */ /* By: omoudni <omoudni@student.42paris.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/21 20:37:12 by omoudni #+# #+# */ /* Created: 2025/05/21 20:37:12 by omoudni #+# #+# */
/* Updated: 2025/05/21 21:44:58 by omoudni ### ########.fr */ /* Updated: 2025/05/22 17:13:35 by omoudni ### ########.fr */
/* */ /* */
/******************************************************************************/ /******************************************************************************/
@ -95,14 +95,17 @@ bool User::isReadyToSend() const
} }
// Extract full command from read buffer // Extract full command from read buffer
std::string User::extractFullCommand() std::string User::extractFullCommand() {
{
std::string command; std::string command;
size_t pos = _read_buffer.find("\r\n"); size_t pos = _read_buffer.find("\r\n");
if (pos != std::string::npos) if (pos == std::string::npos)
{ pos = _read_buffer.find("\n"); // fallback
if (pos != std::string::npos) {
command = _read_buffer.substr(0, pos); command = _read_buffer.substr(0, pos);
_read_buffer.erase(0, pos + 2); _read_buffer.erase(0, pos + 1);
if (_read_buffer[pos] == '\r') // clean up stray \r
_read_buffer.erase(0, 1);
} }
return command; return command;
} }