Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test #39

Merged
merged 3 commits into from
Sep 2, 2024
Merged

Test #39

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
14 changes: 7 additions & 7 deletions include/Client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* Client.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vde-frei <vde-frei@student.42.fr> +#+ +:+ +#+ */
/* By: bmoretti <bmoretti@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/27 14:40:02 by bmoretti #+# #+# */
/* Updated: 2024/07/28 22:34:05 by vde-frei ### ########.fr */
/* Updated: 2024/09/01 16:30:10 by bmoretti ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -18,16 +18,16 @@
class Client
{
public:
Client();
Client(int serverSocket);
~Client();
void connectToServer(const int serverSocket);
int getClientSocket(void) const;
int _getClientSocket() const;
std::string _getBuffer();
void _addToBuffer(const std::string &str);

private:
int _clientSocket;
struct sockaddr_in _address;

void _createClientSocket(void);
std::string _buffer;
};

#endif
5 changes: 3 additions & 2 deletions include/Server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: bmoretti <bmoretti@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/27 10:40:41 by bmoretti #+# #+# */
/* Updated: 2024/08/31 17:44:21 by bmoretti ### ########.fr */
/* Updated: 2024/09/01 16:26:31 by bmoretti ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -17,6 +17,7 @@
#include "Request.hpp"
#include "Response.hpp"
#include "Config.hpp"
#include "Client.hpp"

class Server
{
Expand Down Expand Up @@ -49,7 +50,7 @@ class Server
std::map<int, std::string> _buffer_request;
struct epoll_event _events[MAX_EVENTS];
const ServerConfig &_config;
std::vector<int> _clients;
std::map<int, Client *> _clients;
};

#endif
39 changes: 20 additions & 19 deletions src/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,43 @@
/* ::: :::::::: */
/* Client.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nivicius <nivicius@student.42.fr> +#+ +:+ +#+ */
/* By: bmoretti <bmoretti@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/27 14:46:10 by bmoretti #+# #+# */
/* Updated: 2024/08/08 01:55:14 by nivicius ### ########.fr */
/* Updated: 2024/09/01 16:30:34 by bmoretti ### ########.fr */
/* */
/* ************************************************************************** */

#include "../include/Client.hpp"

Client::Client() {}
Client::Client(int serverSocket)
{
memset(&this->_address, 0, sizeof(this->_address));

socklen_t client_addr_len = sizeof(this->_address);
this->_clientSocket = accept(serverSocket, (sockaddr *)&this->_address, &client_addr_len);
if (this->_clientSocket == -1)
throw std::runtime_error("Failed to accept client");
int flags = 1;
if (ioctl(this->_clientSocket, FIONBIO, &flags) < 0)
throw std::runtime_error("Failed to set socket non-blocking");
}

Client::~Client()
{
close(this->_clientSocket);
}

void Client::connectToServer(const int serverSocket)
int Client::_getClientSocket() const
{
socklen_t addressSize = sizeof(this->_address);

this->_clientSocket = accept(serverSocket,
(struct sockaddr *)&this->_address, &addressSize);
if (this->_clientSocket == -1)
throw std::runtime_error("Error connecting to the server");
return this->_clientSocket;
}

int Client::getClientSocket(void) const
std::string Client::_getBuffer()
{
return this->_clientSocket;
return this->_buffer;
}

void Client::_createClientSocket(void)
void Client::_addToBuffer(const std::string &str)
{
this->_clientSocket = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
if (this->_clientSocket == -1)
{
throw std::runtime_error("Error creating the client socket");
}
this->_buffer += str;
}
77 changes: 47 additions & 30 deletions src/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,28 @@ Server::~Server()
{
if (_server_fd != -1)
close(_server_fd);
if (_epoll_fd != -1)
close(_epoll_fd);
for (std::map<int, Client*>::iterator it = _clients.begin(); it != _clients.end(); ++it)
{
delete it->second;
_clients.erase(it);
break;
}
}

bool Server::_acceptClient()
{
sockaddr_in client_addr;
socklen_t client_addr_len = sizeof(client_addr);
OUTNL("fd no accept " << _server_fd);
int client_fd = accept(_server_fd, (sockaddr *)&client_addr, &client_addr_len);
if (client_fd == -1)
if (!this->_handleAcceptError(errno))
return false;
this->_setSocketNonBlocking(client_fd);
Client *client = new Client(this->_server_fd);

int client_fd = client->_getClientSocket();

(*_event).events = EPOLLIN | EPOLLET;
(*_event).data.fd = client_fd;
if (epoll_ctl(this->_epoll_fd, EPOLL_CTL_ADD, client_fd, _event) == -1)
{
perror("epoll_ctl failed");
close(client_fd);
}
_clients.push_back(client_fd);
this->_clients[client_fd] = client;
return true;
}

Expand All @@ -65,32 +65,49 @@ bool Server::_handleAcceptError(int error_code)
return true;
}

// void Server::_handleConnection(int client_fd)
// {
// char buffer[BUFFER_SIZE];

// while (true)
// {
// ssize_t bytes_read = _readFromClient(client_fd, buffer);
// if (bytes_read <= 0)
// break;
// else
// {
// buffer[bytes_read] = '\0';
// _fillBuffer(client_fd, buffer);
// if (this->_status == HttpStatus::BAD_REQUEST)
// {
// this->_printOnClient(client_fd, "Bad Request\n");
// close(client_fd);
// }
// else if (_checkEndMessage(client_fd))
// {
// Request request(this->_buffer_request[client_fd].c_str());
// // request.printRequest();
// Response response(request, this->_config);
// this->_printOnClient(client_fd, response.getResponse());
// }
// }
// }
// }

void Server::_handleConnection(int client_fd)
{
char buffer[BUFFER_SIZE];
OUTNL("Entrou no handle connection");
char buffer[BUFFER_SIZE + 1];

while (true)
{
ssize_t bytes_read = _readFromClient(client_fd, buffer);
if (bytes_read <= 0)
break;
else
if (bytes_read > 0)
{
buffer[bytes_read] = '\0';
_fillBuffer(client_fd, buffer);
if (this->_status == HttpStatus::BAD_REQUEST)
{
this->_printOnClient(client_fd, "Bad Request\n");
close(client_fd);
}
else if (_checkEndMessage(client_fd))
{
Request request(this->_buffer_request[client_fd].c_str());
// request.printRequest();
Response response(request, this->_config);
this->_printOnClient(client_fd, response.getResponse());
}
this->_clients[client_fd]->_addToBuffer(buffer);
}
else
break;
}
}

Expand All @@ -103,7 +120,7 @@ void Server::_setSocketNonBlocking(int fd)

bool Server::_isClientConnected(int fd)
{
return std::find(_clients.begin(), _clients.end(), fd) != _clients.end();
return this->_clients.find(fd) != this->_clients.end();
}

int Server::_getServerFd() const
Expand Down