From 99ae5f88acafac122bffe259064892278fe26676 Mon Sep 17 00:00:00 2001 From: nicolas-arnaud Date: Sun, 6 Nov 2022 20:54:34 +0100 Subject: [PATCH] fixed sockets not selected --- includes/Env.hpp | 8 ++--- includes/Nodes.hpp | 1 + includes/Parser.hpp | 2 ++ includes/Route.hpp | 1 - includes/Server.hpp | 5 +-- includes/Socket.hpp | 7 ++++ includes/Token.hpp | 1 + includes/webserv.hpp | 11 +++++-- srcs/load/Env.cpp | 48 ++++++++++++++++----------- srcs/load/Server.cpp | 28 ++++++++++++---- srcs/load/Socket.cpp | 78 +++++++++++++++++++++++++------------------- srcs/webserv.cpp | 19 ++++++++++- 12 files changed, 138 insertions(+), 71 deletions(-) diff --git a/includes/Env.hpp b/includes/Env.hpp index a24360f..216dae7 100644 --- a/includes/Env.hpp +++ b/includes/Env.hpp @@ -1,12 +1,10 @@ #pragma once #include "webserv.hpp" - -class JSONNode; -class Server; - class Env { - std::vector _servers; + std::vector _servers; public: Env(JSONNode *conf); + void listen(); + void answer(); }; diff --git a/includes/Nodes.hpp b/includes/Nodes.hpp index 08c0e90..59c6223 100644 --- a/includes/Nodes.hpp +++ b/includes/Nodes.hpp @@ -1,4 +1,5 @@ #pragma once +#include "webserv.hpp" class JSONNode { public: diff --git a/includes/Parser.hpp b/includes/Parser.hpp index 04e2af8..a970a41 100644 --- a/includes/Parser.hpp +++ b/includes/Parser.hpp @@ -1,5 +1,7 @@ +#pragma once #include "webserv.hpp" + class JSONParser { std::fstream file; Tokenizer tokenizer; diff --git a/includes/Route.hpp b/includes/Route.hpp index 3dd5171..aa3ade6 100644 --- a/includes/Route.hpp +++ b/includes/Route.hpp @@ -9,5 +9,4 @@ class Route { public: Route(); ~Route(); - }; diff --git a/includes/Server.hpp b/includes/Server.hpp index 21d1a83..341f69e 100644 --- a/includes/Server.hpp +++ b/includes/Server.hpp @@ -1,13 +1,14 @@ #pragma once #include "webserv.hpp" - class Server { string _name; - std::vector _sockets; + std::vector _sockets; std::map _routes; public: Server(JSONNode *server); + ~Server(); void check(); + void answer(); }; diff --git a/includes/Socket.hpp b/includes/Socket.hpp index b06f996..f438907 100644 --- a/includes/Socket.hpp +++ b/includes/Socket.hpp @@ -9,8 +9,15 @@ class Socket { int _max_clients; int _client_socket[30]; public: + static fd_set _readfds; + static int _max_sd; + static int _min_sd; + static int _amount; Socket(string def); + ~Socket(); + int launch(); void check(); + void answer(); /* Socket& operator=(Socket &src) { _ip = src._ip; diff --git a/includes/Token.hpp b/includes/Token.hpp index bb1dee4..105f24b 100644 --- a/includes/Token.hpp +++ b/includes/Token.hpp @@ -1,3 +1,4 @@ +#pragma once #include "webserv.hpp" enum TOKEN { diff --git a/includes/webserv.hpp b/includes/webserv.hpp index 80e5bc3..bfc700b 100644 --- a/includes/webserv.hpp +++ b/includes/webserv.hpp @@ -10,6 +10,7 @@ #include #include +#include #include #include #include @@ -26,19 +27,23 @@ using std::cout; using std::strerror; using std::string; +class Env; +class Server; +class Socket; +class Route; class JSONNode; + typedef std::map JSONObject; typedef std::vector JSONList; void *ft_memset(void *b, int c, size_t len); bool isInt(string str); +#include "Nodes.hpp" #include "Token.hpp" #include "Parser.hpp" -#include "Nodes.hpp" -#include "Env.hpp" #include "Route.hpp" #include "Socket.hpp" #include "Server.hpp" - +#include "Env.hpp" diff --git a/srcs/load/Env.cpp b/srcs/load/Env.cpp index 4a401b4..91a3f80 100644 --- a/srcs/load/Env.cpp +++ b/srcs/load/Env.cpp @@ -1,23 +1,33 @@ #include "webserv.hpp" Env::Env(JSONNode *conf) { - JSONList servers = conf->obj()["servers"]->lst(); - int i = 0; - string th[8] = {"first", "second", "third", "fourth", - "fifth", "sixth", "seventh", "eigth"}; - for (std::vector::iterator it = servers.begin(); - it < servers.end(); it++) { - Server server(*it); - _servers.push_back(server); - // delete *it; - cout << th[i] << " server launched.\n"; - i++; - } - while (1) { - for (std::vector::iterator it = _servers.begin(); - it < _servers.end(); it++) { - (*it).check(); - } - } - // delete conf; + JSONList servers = conf->obj()["servers"]->lst(); + int i = 0; + string th[8] = {"first", "second", "third", "fourth", + "fifth", "sixth", "seventh", "eigth"}; + for (std::vector::iterator it = servers.begin(); + it < servers.end(); it++) { + Server *server = new Server(*it); + _servers.push_back(server); + // delete *it; + cout << th[i] << " server launched.\n"; + i++; + } + // delete conf; +} +void Env::listen() { + for (std::vector::iterator it = _servers.begin(); + it < _servers.end(); it++) { + (*it)->check(); + } + cout << "finished env listen\n"; +} + +void Env::answer() { + cout << "env start answer\n"; + for (std::vector::iterator it = _servers.begin(); + it < _servers.end(); it++) { + (*it)->answer(); + } + cout << "finished env answer\n"; } diff --git a/srcs/load/Server.cpp b/srcs/load/Server.cpp index be32365..dae18ed 100644 --- a/srcs/load/Server.cpp +++ b/srcs/load/Server.cpp @@ -7,19 +7,33 @@ Server::Server(JSONNode *server) { if (datas["listens"]) { JSONList listens = datas["listens"]->lst(); for (JSONList::iterator i = listens.begin(); i < listens.end(); i++) { - //_listens.push_back((*i)->str()); - Socket sock((*i)->str()); - _sockets.push_back(sock); + Socket *sock = new Socket((*i)->str()); + if (sock->launch() == EXIT_SUCCESS) + _sockets.push_back(sock); + else + delete sock; } - //_port = std::atoi(_listens.front().c_str()); } //_routes["default"] = new Route(datas["root"], datas["return"], - //datas["index"], datas["autoindex"]); + // datas["index"], datas["autoindex"]); +} + +Server::~Server() { + cout << "Server destroyed!\n"; } void Server::check() { - for (std::vector::iterator it = _sockets.begin(); + for (std::vector::iterator it = _sockets.begin(); + it < _sockets.end(); it++) { + (*it)->check(); + } + cout << "finished serv listen\n"; +} + +void Server::answer() { + for (std::vector::iterator it = _sockets.begin(); it < _sockets.end(); it++) { - (*it).check(); + (*it)->answer(); } + cout << "finished serv answer\n"; } diff --git a/srcs/load/Socket.cpp b/srcs/load/Socket.cpp index 6be6cc4..d96a714 100644 --- a/srcs/load/Socket.cpp +++ b/srcs/load/Socket.cpp @@ -4,74 +4,85 @@ Socket::Socket(string def) { size_t split = def.rfind(':'); - string tmp = def.substr(0, split - 1); - _ip = isInt(tmp) ? "localhost" : tmp; + string tmp = def.substr(0, split); + _ip = isInt(tmp) || tmp == "localhost" ? "127.0.0.1" : tmp; tmp = def.substr(split + 1, def.length() - split - 1).c_str(); _port = !isInt(tmp) ? 80 : std::atoi(tmp.c_str()); _max_clients = 30; + for (int i = 0; i < _max_clients; i++) + _client_socket[i] = 0; +} +Socket::~Socket() { + close(_master_socket); + cout << "Socket destroyed!\n"; +} +int Socket::launch() { int opt = 1; _master_socket = socket(AF_INET, SOCK_STREAM, 0); if (_master_socket == 0) { cout << "Socket creation: " << strerror(errno) << "\n"; - exit(EXIT_FAILURE); + return (EXIT_FAILURE); } int opt_ret = setsockopt(_master_socket, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt)); if (opt_ret < 0) { cout << "Sockopt: " << strerror(errno) << "\n"; - exit(EXIT_FAILURE); + return (EXIT_FAILURE); } _address.sin_family = AF_INET; - _address.sin_addr.s_addr = INADDR_ANY; + _address.sin_addr.s_addr = inet_addr(_ip.c_str()); _address.sin_port = htons(_port); if (bind(_master_socket, (struct sockaddr *)&_address, sizeof(_address)) < 0) { cout << "Bind: " << strerror(errno) << "\n"; - cout << "Socket destroyed!\n"; - return; - exit(EXIT_FAILURE); + return (EXIT_FAILURE); } - cout << "Listener on port " << _port << "\n"; + cout << "Listener " << _ip << " on port " << _port << "\n"; if (listen(_master_socket, 3) < 0) { cout << "Listen: " << strerror(errno) << "\n"; - exit(EXIT_FAILURE); + return (EXIT_FAILURE); } - for (int i = 0; i < _max_clients; i++) - _client_socket[i] = 0; + cout << "Socket::_master_socket: " << _master_socket << "\n"; + if (_master_socket < _min_sd) + _min_sd = _master_socket; + _amount++; + return (EXIT_SUCCESS); } -void Socket::check() { - int new_socket, activity, i, valread, sd; - char buffer[1024]; - char r404[72] = - "HTTP/1.1 200 OK\nContent-Type: text/plain\nContent-Length: 4\n\n404!"; - fd_set readfds; - int max_sd = _master_socket; +void Socket::check() { + int sd; - int addrlen = sizeof(_address); - FD_ZERO(&readfds); - FD_SET(_master_socket, &readfds); + FD_SET(_master_socket, &_readfds); + cout << "sd_set " << _master_socket << "\n"; - for (i = 0; i < _max_clients; i++) { + for (int i = 0; i < _max_clients; i++) { sd = _client_socket[i]; + //cout << "id: " << i << " -> sd: " << sd << "\n"; if (sd > 0) - FD_SET(sd, &readfds); - if (sd > max_sd) - max_sd = sd; + { + FD_SET(sd, &_readfds); + cout << "fd_set " << sd << "\n"; + } + if (sd > _max_sd) + _max_sd = sd; } + cout << "Socket checked\n"; +} - activity = select(max_sd + 1, &readfds, NULL, NULL, NULL); - - if ((activity < 0) && (errno != EINTR)) - cout << "Select: " << strerror(errno) << "\n"; - if (FD_ISSET(_master_socket, &readfds)) { - new_socket = accept(_master_socket, (struct sockaddr *)&_address, +void Socket::answer() { + int i, sd, valread; + int addrlen = sizeof(_address); + char buffer[1024]; + char r404[72] = + "HTTP/1.1 200 OK\nContent-Type: text/plain\nContent-Length: 4\n\n404!"; + if (FD_ISSET(_master_socket, &_readfds)) { + int new_socket = accept(_master_socket, (struct sockaddr *)&_address, (socklen_t *)&addrlen); if (new_socket < 0) { cout << "Accept: " << strerror(errno) << "\n"; @@ -94,7 +105,7 @@ void Socket::check() { cout << "Socket: " << _ip << ":" << _port << "\n"; for (i = 0; i < _max_clients; i++) { sd = _client_socket[i]; - if (FD_ISSET(sd, &readfds)) { + if (FD_ISSET(sd, &_readfds)) { cout << "Client " << i << ": set\n"; valread = read(sd, buffer, 1024); if (valread == 0) { @@ -114,4 +125,5 @@ void Socket::check() { } } } + cout << "Socket answered\n"; } diff --git a/srcs/webserv.cpp b/srcs/webserv.cpp index 8d76b9c..f40f562 100644 --- a/srcs/webserv.cpp +++ b/srcs/webserv.cpp @@ -1,4 +1,10 @@ #include "webserv.hpp" +#include + +fd_set Socket::_readfds; +int Socket::_max_sd; +int Socket::_min_sd = INT_MAX; +int Socket::_amount = 0; int main(int ac, char **av) { @@ -11,6 +17,17 @@ int main(int ac, char **av) { cout << "Setting environment...\n"; Env env(conf); - } + while (1) { + cout << "Cycling...\n"; + FD_ZERO(&Socket::_readfds); + Socket::_max_sd = Socket::_min_sd; + env.listen(); + cout << "Socket::_max_sd " << Socket::_max_sd << "\n"; + int activity = select(Socket::_max_sd + Socket::_amount, &(Socket::_readfds), NULL, NULL, NULL); + if ((activity < 0) && (errno != EINTR)) + cout << "Select: " << strerror(errno) << "\n"; + env.answer(); + } + } return (0); }