split lines before commands dispatch + irssi recev code 001 + command PING ok-tiers
This commit is contained in:
parent
d52b0cc3c2
commit
e88d706ae0
4 changed files with 43 additions and 24 deletions
|
|
@ -1,14 +1,14 @@
|
||||||
/******************************************************************************/
|
/* ************************************************************************** */
|
||||||
/* */
|
/* */
|
||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* server.hpp :+: :+: :+: */
|
/* server.hpp :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: omoudni <omoudni@student.42paris.fr> +#+ +:+ +#+ */
|
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/05/20 21:50:32 by rparodi #+# #+# */
|
/* Created: 2025/05/20 21:50:32 by rparodi #+# #+# */
|
||||||
/* Updated: 2025/05/29 12:17:55 by rparodi ### ########.fr */
|
/* Updated: 2025/06/14 23:08:11 by sben-tay ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/******************************************************************************/
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
|
@ -21,6 +21,7 @@
|
||||||
class User;
|
class User;
|
||||||
class Channel;
|
class Channel;
|
||||||
class Server
|
class Server
|
||||||
|
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
int _port;
|
int _port;
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
|
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/05/24 17:29:48 by rparodi #+# #+# */
|
/* Created: 2025/05/24 17:29:48 by rparodi #+# #+# */
|
||||||
/* Updated: 2025/06/12 13:24:55 by sben-tay ### ########.fr */
|
/* Updated: 2025/06/14 23:25:45 by sben-tay ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -19,7 +19,7 @@
|
||||||
using namespace cmd;
|
using namespace cmd;
|
||||||
|
|
||||||
e_code Ping::checkArgs() {
|
e_code Ping::checkArgs() {
|
||||||
if (_args.size() < 3) {
|
if (_args.size() < 2) {
|
||||||
WARNING_MSG("Not enough arguments for PING command");
|
WARNING_MSG("Not enough arguments for PING command");
|
||||||
return ERR_NEEDMOREPARAMS;
|
return ERR_NEEDMOREPARAMS;
|
||||||
}
|
}
|
||||||
|
|
@ -32,11 +32,10 @@ e_code Ping::checkArgs() {
|
||||||
*/
|
*/
|
||||||
|
|
||||||
void Ping::execute() {
|
void Ping::execute() {
|
||||||
clock_t start = clock() / CLOCKS_PER_SEC;
|
|
||||||
if (checkArgs() != _PARSING_OK) {
|
if (checkArgs() != _PARSING_OK) {
|
||||||
ERROR_MSG("Invalid arguments for PRIVMSG command (see warning message)");
|
ERROR_MSG("Invalid arguments for PRIVMSG command (see warning message)");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
clock_t diff = Pong().answer(start);
|
_sender->appendToWriteBuffer("PONG " + _args[1] + "\r\n");
|
||||||
INFO_MSG(diff);
|
DEBUG_MSG(_sender->getWriteBuffer());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
|
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/05/13 11:11:07 by rparodi #+# #+# */
|
/* Created: 2025/05/13 11:11:07 by rparodi #+# #+# */
|
||||||
/* Updated: 2025/06/12 18:03:53 by sben-tay ### ########.fr */
|
/* Updated: 2025/06/14 23:16:54 by sben-tay ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -22,6 +22,7 @@
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief The constructor of the Server class.
|
* @brief The constructor of the Server class.
|
||||||
|
|
@ -48,6 +49,8 @@ Server::~Server()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::vector<std::string> splitLines(const std::string& input);
|
||||||
|
|
||||||
void Server::start()
|
void Server::start()
|
||||||
{
|
{
|
||||||
_serverFd = socket(AF_INET, SOCK_STREAM, 0);
|
_serverFd = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
|
|
@ -102,12 +105,15 @@ void Server::start()
|
||||||
if (_users.count(fd))
|
if (_users.count(fd))
|
||||||
{
|
{
|
||||||
_users[fd]->appendToReadBuffer(data);
|
_users[fd]->appendToReadBuffer(data);
|
||||||
std::string cmd;
|
std::string rawCmd;
|
||||||
while (!(cmd = _users[fd]->extractFullCommand()).empty())
|
while (!(rawCmd = _users[fd]->extractFullCommand()).empty())
|
||||||
{
|
{
|
||||||
|
std::vector<std::string> lines = splitLines(rawCmd);
|
||||||
|
for (size_t i = 0; i < lines.size(); ++i) {
|
||||||
|
std::cout << "Client " << fd << " says: " << lines[i] << std::endl;
|
||||||
|
cmd::dispatch(_users[fd], NULL, this, lines[i]);
|
||||||
|
}
|
||||||
// This prints every command/message received from any client
|
// This prints every command/message received from any client
|
||||||
std::cout << "Client " << fd << " says: " << cmd << std::endl;
|
|
||||||
cmd::dispatch(_users[fd], NULL, this, cmd);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -122,8 +128,7 @@ void Server::start()
|
||||||
{
|
{
|
||||||
std::cerr << "Erreur send" << std::endl;
|
std::cerr << "Erreur send" << std::endl;
|
||||||
}
|
}
|
||||||
else
|
else {
|
||||||
{
|
|
||||||
_users[fd]->clearWriteBuffer();
|
_users[fd]->clearWriteBuffer();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -186,3 +191,16 @@ std::list<User *> Server::getUsersList() const {
|
||||||
std::list<Channel *> Server::getChannelsList() const {
|
std::list<Channel *> Server::getChannelsList() const {
|
||||||
return this->_channels;
|
return this->_channels;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::vector<std::string> splitLines(const std::string& input) {
|
||||||
|
std::vector<std::string> lines;
|
||||||
|
std::istringstream stream(input);
|
||||||
|
std::string line;
|
||||||
|
|
||||||
|
while (std::getline(stream, line)) {
|
||||||
|
if (!line.empty() && line[line.length() - 1] == '\r')
|
||||||
|
line.erase(line.length() - 1); // retirer le \r final
|
||||||
|
lines.push_back(line);
|
||||||
|
}
|
||||||
|
return lines;
|
||||||
|
}
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
|
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/05/21 20:37:12 by omoudni #+# #+# */
|
/* Created: 2025/05/21 20:37:12 by omoudni #+# #+# */
|
||||||
/* Updated: 2025/06/14 22:34:59 by sben-tay ### ########.fr */
|
/* Updated: 2025/06/14 23:27:43 by sben-tay ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -83,7 +83,7 @@ void User::appendToWriteBuffer(const std::string &data)
|
||||||
// Check registration
|
// Check registration
|
||||||
void User::checkRegistration()
|
void User::checkRegistration()
|
||||||
{
|
{
|
||||||
if (!_registered && _hasNick && _hasUser) // without _hasPass check
|
if (!_registered && _hasNick && _hasUser && _hasPass)
|
||||||
{
|
{
|
||||||
std::cout << "JE SUIS ENREGISTRE" << std::endl;
|
std::cout << "JE SUIS ENREGISTRE" << std::endl;
|
||||||
_registered = true;
|
_registered = true;
|
||||||
|
|
@ -102,18 +102,19 @@ 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;
|
|
||||||
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
|
pos = _read_buffer.find("\n"); // fallback
|
||||||
|
|
||||||
if (pos != std::string::npos) {
|
if (pos != std::string::npos) {
|
||||||
command = _read_buffer.substr(0, pos);
|
std::string command = _read_buffer.substr(0, pos);
|
||||||
|
if (_read_buffer.substr(pos, 2) == "\r\n")
|
||||||
|
_read_buffer.erase(0, pos + 2);
|
||||||
|
else
|
||||||
_read_buffer.erase(0, pos + 1);
|
_read_buffer.erase(0, pos + 1);
|
||||||
if (_read_buffer[pos] == '\r') // clean up stray \r
|
|
||||||
_read_buffer.erase(0, 1);
|
|
||||||
}
|
|
||||||
return command;
|
return command;
|
||||||
|
}
|
||||||
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
void User::setHasNick(bool value) { _hasNick = value; }
|
void User::setHasNick(bool value) { _hasNick = value; }
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue