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> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# 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/main.cpp \
sources/core/Server.cpp \
sources/core/user.cpp \
sources/user/user.cpp \
sources/channel/channel.cpp
INC_DIR = include/core \

View file

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

View file

@ -6,7 +6,7 @@
/* By: omoudni <omoudni@student.42paris.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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();
void setServerFd(int fd);
void addClient(short unsigned fd);
void removeClient(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)
{
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)
{
@ -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)
{
short unsigned fd = _fds[i].fd;
std::cout << "I'm here 1" << std::endl;
if ((fd == server_fd) && (_fds[i].revents & POLLIN))
{
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;
continue;
}
std::cout << "I'm here 2" << std::endl;
addClient(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];
ssize_t bytes = recv(fd, buffer, sizeof(buffer) - 1, 0);
if (bytes > 0)
{
buffer[bytes] = '\0';
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
{
@ -93,3 +86,11 @@ void PollManager::removeClient(short unsigned fd)
close(fd);
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> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 "server.hpp"
#include "core.hpp"
#include "PollManager.hpp"
#include <iostream>
#include <sys/socket.h>
#include <netinet/in.h>
@ -64,6 +65,7 @@ void Server::start() {
}
std::cout << "Serveur lancé sur le port " << _port << std::endl;
_pollManager.setServerFd(_serverFd);
std::vector<int> newClients;
std::vector<int> disconnected;
std::vector<std::pair<int, std::string> > readyClients;
@ -72,8 +74,9 @@ void Server::start() {
newClients.clear();
disconnected.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
for (size_t i = 0; i < newClients.size(); ++i)
_users[newClients[i]] = new User(newClients[i]);

View file

@ -6,7 +6,7 @@
/* By: omoudni <omoudni@student.42paris.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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
std::string User::extractFullCommand()
{
std::string User::extractFullCommand() {
std::string command;
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);
_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;
}