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

. #34

Merged
merged 4 commits into from
Aug 18, 2024
Merged

. #34

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
40 changes: 14 additions & 26 deletions default.conf
Original file line number Diff line number Diff line change
@@ -1,33 +1,21 @@
events {
worker_connections 1024;
}

http {
server {
listen 127.0.0.1:8042;
server_name localhost;
server {
listen 127.0.0.1:8042;
server_name localhost;

location / {
root /usr/share/nginx/html;
index index.html;
cgi .py cgi-bin;
}
location / {
root /usr/share/nginx/html;
index index.html;
cgi .py cgi-bin;
}
}

events {
worker_connections 1024;
}

http {
server {
listen 127.0.0.1:8041;
server_name localhost;
server {
listen 127.0.0.1:8041;
server_name localhost;

location / {
root /usr/share/nginx/html;
index index.html;
cgi .py cgi-bin;
}
location / {
root /usr/share/nginx/html;
index index.html;
cgi .py cgi-bin;
}
}
35 changes: 35 additions & 0 deletions include/Events.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Events.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bmoretti <bmoretti@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/18 16:05:36 by bmoretti #+# #+# */
/* Updated: 2024/08/18 17:31:39 by bmoretti ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef EVENTS_HPP
# define EVENTS_HPP

# include "common.hpp"
# include "Config.hpp"
# include "Server.hpp"

class Events
{
public:
Events(const std::vector<ServerConfig> & servers);
~Events();
void run();

private:
std::vector<Server *> _servers;
int _epoll_fd;
epoll_event *_event;

void handleEvents();
};

#endif
9 changes: 4 additions & 5 deletions include/Server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* Server.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nivicius <nivicius@student.42.fr> +#+ +:+ +#+ */
/* By: bmoretti <bmoretti@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/27 10:40:41 by bmoretti #+# #+# */
/* Updated: 2024/08/17 18:58:07 by nivicius ### ########.fr */
/* Updated: 2024/08/18 17:31:41 by bmoretti ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -24,20 +24,19 @@ class Server
Server(const ServerConfig &config, int event_fd, epoll_event *event);
~Server();
int _getServerFd() const;
bool _acceptClient();
void _handleConnection(int client_fd);
void run();

private:
/* Server initialization */
void _initServer();
void _handleConnection(int client_fd);
void _handleEvents();
void _fillBuffer(int fd, const char *str);
bool _checkEndMessage(int fd);
void _printOnClient(int fd, std::string const &str);
ssize_t _readFromClient(int fd, char *buff);
bool _handleAcceptError(int error_code);
void _initServerAddress(sockaddr_in &server_add);
bool _acceptClient();

std::string _address;
int _port;
Expand Down
3 changes: 2 additions & 1 deletion include/common.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 11:24:48 by bmoretti #+# #+# */
/* Updated: 2024/08/04 16:53:08 by bmoretti ### ########.fr */
/* Updated: 2024/08/18 17:31:40 by bmoretti ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -34,6 +34,7 @@

// C++ LIBS
#include <cerrno>
#include <algorithm>
#include <cstring>
#include <csignal>
#include <cstdlib>
Expand Down
67 changes: 67 additions & 0 deletions src/Events.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Events.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bmoretti <bmoretti@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/18 16:09:46 by bmoretti #+# #+# */
/* Updated: 2024/08/18 17:22:15 by bmoretti ### ########.fr */
/* */
/* ************************************************************************** */

#include "Events.hpp"

Events::Events(const std::vector<ServerConfig> &servers)
{
this->_epoll_fd = epoll_create(MAX_CONNECTIONS);
this->_event = new epoll_event[MAX_CONNECTIONS];
if (this->_epoll_fd == -1)
throw std::runtime_error("Failed to create epoll file descriptor");
for (std::vector<ServerConfig>::const_iterator it = servers.begin();
it != servers.end(); ++it)
{
Server *server = new Server(*it, this->_epoll_fd, this->_event);
this->_servers.push_back(server);
}
}

Events::~Events()
{
for (std::vector<Server *>::iterator it = this->_servers.begin();
it != this->_servers.end(); ++it)
delete *it;
if (this->_epoll_fd != -1)
close(this->_epoll_fd);
if (this->_event)
delete[] this->_event;
}

void Events::run()
{
while (true)
{
int event_count = epoll_wait(this->_epoll_fd, this->_event, MAX_EVENTS, -1);
if (event_count == -1)
throw std::runtime_error("Failed to wait on epoll");
for (int i = 0; i < event_count; i++)
{
for(int j = 0, size = this->_servers.size(); j < size; j++)
{
if (_event[i].data.fd == this->_servers[j]->_getServerFd())
{
if (!this->_servers[j]->_acceptClient())
break;
}
// else
// this->_servers[j]->_handleConnection(_event[i].data.fd);
}
}
}
}

void Events::handleEvents()
{


}
8 changes: 0 additions & 8 deletions src/InitServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,19 @@ void Server::_initServerAddress(sockaddr_in &server_add)
throw std::runtime_error("Failed to bind socket");
if (listen(_server_fd, MAX_CONNECTIONS) == -1)
throw std::runtime_error("Failed to listen socket");
// this->_epoll_fd = epoll_create1(0);
// if (this->_epoll_fd == -1)
// throw std::runtime_error("Failed to create epoll file descriptor");
}

void Server::_initServer()
{
this->_server_fd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
OUTNL("server fd > " << _server_fd);
if (this->_server_fd == -1)
throw std::runtime_error("Failed to create socket");
int opt = 1;
if (setsockopt(_server_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) == -1)
throw std::runtime_error("Failed to set socket options");
sockaddr_in server_addr;
this->_initServerAddress(server_addr);
// epoll_event event;
// memset(&event, 0, sizeof(epoll_event));
// event.events = EPOLLIN | EPOLLET;
(*_event).data.fd = this->_server_fd;
OUTNL("epoll fd > " << _epoll_fd << " server fd > " << _server_fd);
if (epoll_ctl(_epoll_fd, EPOLL_CTL_ADD, _server_fd, _event) == -1)
throw std::runtime_error("Failed to add server socket to epoll");
}
47 changes: 8 additions & 39 deletions src/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Server::Server(const ServerConfig &config, int epoll_fd, epoll_event *event) : _
this->_epoll_fd = epoll_fd;
this->_event = event;
this->_initServer();
OUTNL(MAGENTA("Read to connect in: " << _address + ":" << _port));
OUTNL(MAGENTA("Ready to connect in: " << _address + ":" << _port));
}

Server::~Server()
Expand All @@ -34,44 +34,13 @@ Server::~Server()

bool Server::_acceptClient()
{
while (true)
{
sockaddr_in client_addr;
socklen_t client_addr_len = sizeof(client_addr);
int client_fd = accept(_server_fd, (sockaddr *)&client_addr, &client_addr_len);
if (client_fd == -1)
if (!this->_handleAcceptError(errno))
return false;
epoll_event event;
memset(&event, 0, sizeof(epoll_event));
event.events = EPOLLIN | EPOLLET;
event.data.fd = client_fd;
if (epoll_ctl(_epoll_fd, EPOLL_CTL_ADD, client_fd, &event) == -1)
{
std::cerr << "Failed to add client socket to epoll" << std::endl;
close(client_fd);
}
}
}

void Server::run()
{
while (true)
{
int event_count = epoll_wait(_epoll_fd, _events, MAX_EVENTS, -1);
if (event_count == -1)
throw std::runtime_error("Failed to wait on epoll");
for (int i = 0; i < event_count; i++)
{
if (_events[i].data.fd == _server_fd)
{
if (!this->_acceptClient())
break;
}
else
this->_handleConnection(_events[i].data.fd);
}
}
sockaddr_in client_addr;
socklen_t client_addr_len = sizeof(client_addr);
int client_fd = accept(_server_fd, (sockaddr *)&client_addr, &client_addr_len);
if (client_fd == -1)
if (!this->_handleAcceptError(errno))
return false;
return true;
}

bool Server::_handleAcceptError(int error_code)
Expand Down
40 changes: 3 additions & 37 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,51 +1,17 @@
#include "../include/Server.hpp"
#include "../include/Config.hpp"

void run(int epoll_fd, epoll_event *event)
{
while (true)
{
int event_count = epoll_wait(epoll_fd, event, MAX_EVENTS, -1);
if (event_count == -1)
throw std::runtime_error("Failed to wait on epoll");
// for (int i = 0; i < event_count; i++)
// {
// if (_events[i].data.fd == _server_fd)
// {
// if (!this->_acceptClient())
// break;
// }
// else
// this->_handleConnection(_events[i].data.fd);
// }
}
}
#include "../include/Events.hpp"

int main(int ac, char **av)
{
std::string file;
int epoll_fd;
OUTNL("pid " << getpid());

ac == 2 ? file = av[1] : file = DEFAULT_CONFIG_FILE;
try
{
Config config(file);
const std::vector<ServerConfig> & servers = config.getServers();
epoll_fd = epoll_create(MAX_CONNECTIONS);
epoll_event *event;
event = new epoll_event[MAX_CONNECTIONS];
if (epoll_fd == -1)
throw std::runtime_error("Failed to create epoll file descriptor");
for (std::vector<ServerConfig>::const_iterator it = servers.begin();
it != servers.end(); ++it)
{
Server server(*it, epoll_fd, event);
OUTNL(server._getServerFd());
//server.run();
std::cout << "Server started on port" << std::endl;
}
run(epoll_fd, event);
Events events(config.getServers());
events.run();
}
catch (std::exception &e)
{
Expand Down