Added a basic command parser with a temporary echo command. Can be expanded by just adding command functions to the std::map commands, Should maybe move the commands map to the Server class later
This commit is contained in:
@@ -6,7 +6,7 @@
|
|||||||
/* By: iherman- <iherman-@student.42malaga.com +#+ +:+ +#+ */
|
/* By: iherman- <iherman-@student.42malaga.com +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2026/05/06 17:19:12 by iherman- #+# #+# */
|
/* Created: 2026/05/06 17:19:12 by iherman- #+# #+# */
|
||||||
/* Updated: 2026/05/09 21:50:37 by iherman- ### ########.fr */
|
/* Updated: 2026/05/11 20:34:33 by iherman- ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -31,6 +31,7 @@
|
|||||||
#include <poll.h>
|
#include <poll.h>
|
||||||
|
|
||||||
#include <cerrno>
|
#include <cerrno>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
const int Server::kConnectionQueueLimit = 10;
|
const int Server::kConnectionQueueLimit = 10;
|
||||||
|
|
||||||
@@ -121,13 +122,43 @@ Server &Server::operator=(const Server& other)
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TEMPORARY TESTING FUNCTION
|
||||||
|
void echo(User& client)
|
||||||
|
{
|
||||||
|
std::string message = "Server received: " + client.getBuffer();
|
||||||
|
send(client.getFd(), message.c_str(), message.size(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Server::parseCommand(User& client)
|
||||||
|
{
|
||||||
|
std::stringstream args(client.getBuffer());
|
||||||
|
std::string command;
|
||||||
|
|
||||||
|
args >> command;
|
||||||
|
|
||||||
|
std::map<std::string, void (*)(User&)> commands;
|
||||||
|
// TEMP
|
||||||
|
commands["echo"] = &echo;
|
||||||
|
|
||||||
|
std::map<std::string, void (*)(User&)>::iterator it = commands.find(command);
|
||||||
|
if (it == commands.end())
|
||||||
|
{
|
||||||
|
std::string message = "Error: command not found!";
|
||||||
|
send(client.getFd(), message.c_str(), message.size(), 0);
|
||||||
|
client.clearBuffer();
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
it->second(client);
|
||||||
|
client.clearBuffer();
|
||||||
|
}
|
||||||
|
|
||||||
bool Server::handleClient(User& client)
|
bool Server::handleClient(User& client)
|
||||||
{
|
{
|
||||||
const std::size_t kBufferSize = 1024;
|
const std::size_t kBufferSize = 1024;
|
||||||
char buffer[kBufferSize] = {0};
|
char buffer[kBufferSize] = {0};
|
||||||
|
|
||||||
int recv_amount = recv(client.getFd(), buffer, kBufferSize, 0);
|
int recv_amount = recv(client.getFd(), buffer, kBufferSize, 0);
|
||||||
|
|
||||||
if (recv_amount == 0)
|
if (recv_amount == 0)
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
@@ -141,11 +172,7 @@ bool Server::handleClient(User& client)
|
|||||||
client.appendBuffer(std::string(buffer, recv_amount));
|
client.appendBuffer(std::string(buffer, recv_amount));
|
||||||
|
|
||||||
if (client.getBuffer().find('\n') != std::string::npos)
|
if (client.getBuffer().find('\n') != std::string::npos)
|
||||||
{
|
parseCommand(client);
|
||||||
std::string message = "Server received: " + client.getBuffer();
|
|
||||||
send(client.getFd(), message.c_str(), message.size(), 0);
|
|
||||||
client.clearBuffer();
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -177,6 +204,16 @@ void Server::addClient()
|
|||||||
std::cout << "Client with fd: " << newClientSocket << " connected" << std::endl;
|
std::cout << "Client with fd: " << newClientSocket << " connected" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::vector<struct pollfd>::iterator Server::removeClient(std::vector<struct pollfd>::iterator& client)
|
||||||
|
{
|
||||||
|
close(client->fd);
|
||||||
|
|
||||||
|
std::cout << "Client with fd: " << client->fd << " disconnected " << std::endl;
|
||||||
|
|
||||||
|
clients_.erase(client->fd);
|
||||||
|
return (sockets_.erase(client));
|
||||||
|
}
|
||||||
|
|
||||||
void Server::run()
|
void Server::run()
|
||||||
{
|
{
|
||||||
// add serverSocket_ to sockets_
|
// add serverSocket_ to sockets_
|
||||||
@@ -197,7 +234,8 @@ void Server::run()
|
|||||||
{
|
{
|
||||||
if (it->revents & (POLLERR | POLLHUP | POLLNVAL))
|
if (it->revents & (POLLERR | POLLHUP | POLLNVAL))
|
||||||
{
|
{
|
||||||
// remove client
|
it = removeClient(it);
|
||||||
|
continue ;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!(it->revents & POLLIN))
|
if (!(it->revents & POLLIN))
|
||||||
@@ -229,13 +267,7 @@ void Server::run()
|
|||||||
bool disconnected = handleClient(clientIt->second);
|
bool disconnected = handleClient(clientIt->second);
|
||||||
|
|
||||||
if (disconnected)
|
if (disconnected)
|
||||||
{
|
it = removeClient(it);
|
||||||
close(it->fd);
|
|
||||||
std::cout << "Client with fd: " << it->fd << " disconnected " << std::endl;
|
|
||||||
|
|
||||||
clients_.erase(it->fd);
|
|
||||||
it = sockets_.erase(it);
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
it++;
|
it++;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,16 +6,13 @@
|
|||||||
/* By: iherman- <iherman-@student.42malaga.com +#+ +:+ +#+ */
|
/* By: iherman- <iherman-@student.42malaga.com +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2026/05/06 17:18:11 by iherman- #+# #+# */
|
/* Created: 2026/05/06 17:18:11 by iherman- #+# #+# */
|
||||||
/* Updated: 2026/05/09 21:47:58 by iherman- ### ########.fr */
|
/* Updated: 2026/05/11 18:11:59 by iherman- ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#ifndef SERVER_HPP
|
#ifndef SERVER_HPP
|
||||||
# define SERVER_HPP
|
# define SERVER_HPP
|
||||||
|
|
||||||
// C lib functions
|
|
||||||
|
|
||||||
|
|
||||||
// C++ lib functions
|
// C++ lib functions
|
||||||
# include <iostream>
|
# include <iostream>
|
||||||
|
|
||||||
@@ -45,7 +42,9 @@ class Server
|
|||||||
bool handleClient(User& client);
|
bool handleClient(User& client);
|
||||||
|
|
||||||
void addClient();
|
void addClient();
|
||||||
std::vector<struct pollfd>::iterator removeClient(std::vector<struct pollfd>::iterator client);
|
std::vector<struct pollfd>::iterator removeClient(std::vector<struct pollfd>::iterator& client);
|
||||||
|
|
||||||
|
void parseCommand(User& client);
|
||||||
|
|
||||||
Server(const Server& other);
|
Server(const Server& other);
|
||||||
Server &operator=(const Server& other);
|
Server &operator=(const Server& other);
|
||||||
|
|||||||
Reference in New Issue
Block a user