Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion inc/Client.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#pragma once
#include <queue>
#include <functional>
#include <cstdint>
#include <stdexcept>
Expand All @@ -7,6 +8,8 @@
#include <memory>
#include "CommandDispatcher.hpp"
#include "User.hpp"
#include "RecvParser.hpp"

class User;
class CommandDispatcher;
/*
Expand All @@ -28,7 +31,8 @@ class Client {
bool _authenticated = false;
bool _registered = false;
CommandDispatcher* _test;

std::queue<std::unique_ptr<Message>> _msg_queue;
RecvParser _parser;
public:
explicit Client(int fd);
~Client();
Expand All @@ -41,6 +45,7 @@ class Client {
std::function<void(int)>& getHandler(uint32_t eventType);
User* getUser(void);
CommandDispatcher* getDispatch(void);
RecvParser& getParser(void);
void authenticate(void);
bool isAuthenticated(void) const;
bool& accessRegistered(void);
Expand Down
3 changes: 2 additions & 1 deletion inc/RecvParser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <memory>
#include "Message.hpp"

/**
/**
* @class RecvParser
* @brief A class for parsing data read by recv() into a message queue
*/
Expand All @@ -16,6 +16,7 @@ class RecvParser
public:
RecvParser(std::queue<std::unique_ptr<Message>> &msg_queue);

std::queue<std::unique_ptr<Message>> &getQueue(void);
void feed(const char *read_buf, size_t len);

private:
Expand Down
7 changes: 6 additions & 1 deletion src/Client.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#include "Client.hpp"


Client::Client(int fd) : _self(new User()), _test(new CommandDispatcher()), _fd(fd) {}
Client::Client(int fd) : _self(new User()), _test(new CommandDispatcher()), _parser(RecvParser(_msg_queue)), _fd(fd) {}

Client::Client(const Client& other) :
_IN(other._IN),
_RDHUP(other._RDHUP),
_HUP(other._HUP),
_self(new User(*other._self)),
_test(other._test),
_parser(other._parser),
_fd(other._fd),
_initialized(other._initialized){}

Expand All @@ -33,6 +34,10 @@ CommandDispatcher* Client::getDispatch(void) {
return _test;
}

RecvParser& Client::getParser(void) {
return _parser;
}

void Client::setHandler(uint32_t eventType, std::function<void(int)> handler) {
switch (eventType) {
case EPOLLIN:
Expand Down
6 changes: 4 additions & 2 deletions src/CommandDispatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ bool CommandDispatcher::dispatch(const std::unique_ptr<Message> &msg, int fd)
{
if (auto cmd = _handlers.find(msg->command); cmd != _handlers.end())
{
if (!irc->checkPassword() &&
!irc->getClient(fd).isAuthenticated() &&
if (not irc->checkPassword() &&
not irc->getClient(fd).isAuthenticated() &&
msg->command != "PASS" &&
msg->command != "CAP")
{
Expand All @@ -46,6 +46,8 @@ bool CommandDispatcher::dispatch(const std::unique_ptr<Message> &msg, int fd)
irc->removeClient(fd);
return false;
}
if (msg->command == "QUIT")
return cmd->second->execute(*msg, fd), false;
cmd->second->execute(*msg, fd);
if (msg->command != "QUIT" &&
not irc->getClient(fd).getUser()->getNick().empty() &&
Expand Down
5 changes: 3 additions & 2 deletions src/Handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ using namespace std;
void Handler::clientWrite(int fd) {
ssize_t messageLen = 0;
vector<char> buf(BUFSIZ);
queue<unique_ptr<Message>> msg_queue;
RecvParser parser(msg_queue);
Client& client = irc->getClient(fd);
RecvParser& parser = client.getParser();
std::queue<std::unique_ptr<Message>> &msg_queue = parser.getQueue();

messageLen = recv(fd, &buf[0], buf.size(), 0);
if (messageLen == -1)
Expand Down
5 changes: 5 additions & 0 deletions src/RecvParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
RecvParser::RecvParser(std::queue<std::unique_ptr<Message>> &msg_queue)
: _output(msg_queue){}

std::queue<std::unique_ptr<Message>> &RecvParser::getQueue(void)
{
return _output;
}

/**
* Feed the recv() buffer into std::string buffer
* @param read_buf The buffer to read
Expand Down