Browse Source

save 22-11-6-1

test
nicolas-arnaud 2 years ago
parent
commit
d3b9b68341
  1. 4
      includes/Env.hpp
  2. 4
      includes/Server.hpp
  3. 13
      includes/Socket.hpp
  4. 2
      includes/webserv.hpp
  5. 11
      srcs/load/Env.cpp
  6. 10
      srcs/load/Server.cpp
  7. 91
      srcs/load/Socket.cpp
  8. 14
      srcs/webserv.cpp

4
includes/Env.hpp

@ -5,6 +5,6 @@ class Env {
std::vector<Server *> _servers; std::vector<Server *> _servers;
public: public:
Env(JSONNode *conf); Env(JSONNode *conf);
void listen(); void set_fds();
void answer(); void refresh();
}; };

4
includes/Server.hpp

@ -9,6 +9,6 @@ class Server {
public: public:
Server(JSONNode *server); Server(JSONNode *server);
~Server(); ~Server();
void check(); void set_fds();
void answer(); void refresh();
}; };

13
includes/Socket.hpp

@ -6,18 +6,19 @@ class Socket {
int _port; int _port;
int _master_socket; int _master_socket;
struct sockaddr_in _address; struct sockaddr_in _address;
int _max_clients; int _clients_amount;
int _client_socket[30]; std::vector<int> _clients;
public: public:
static fd_set _readfds; static fd_set _readfds;
static int _max_sd; static int _max_fd;
static int _min_sd; static int _min_fd;
static int _amount; static int _amount;
Socket(string def); Socket(string def);
~Socket(); ~Socket();
int launch(); int launch();
void check(); void set_fds();
void answer(); void refresh();
void answer(int fd, string request);
/* /*
Socket& operator=(Socket &src) { Socket& operator=(Socket &src) {
_ip = src._ip; _ip = src._ip;

2
includes/webserv.hpp

@ -16,9 +16,9 @@
#include <cstring> #include <cstring>
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <map>
#include <sstream> #include <sstream>
#include <string> #include <string>
#include <map>
#include <vector> #include <vector>
#define DEBUG 0 #define DEBUG 0

11
srcs/load/Env.cpp

@ -15,19 +15,16 @@ Env::Env(JSONNode *conf) {
} }
// delete conf; // delete conf;
} }
void Env::listen() { void Env::set_fds() {
for (std::vector<Server *>::iterator it = _servers.begin(); for (std::vector<Server *>::iterator it = _servers.begin();
it < _servers.end(); it++) { it < _servers.end(); it++) {
(*it)->check(); (*it)->set_fds();
} }
cout << "finished env listen\n";
} }
void Env::answer() { void Env::refresh() {
cout << "env start answer\n";
for (std::vector<Server *>::iterator it = _servers.begin(); for (std::vector<Server *>::iterator it = _servers.begin();
it < _servers.end(); it++) { it < _servers.end(); it++) {
(*it)->answer(); (*it)->refresh();
} }
cout << "finished env answer\n";
} }

10
srcs/load/Server.cpp

@ -22,18 +22,16 @@ Server::~Server() {
cout << "Server destroyed!\n"; cout << "Server destroyed!\n";
} }
void Server::check() { void Server::set_fds() {
for (std::vector<Socket *>::iterator it = _sockets.begin(); for (std::vector<Socket *>::iterator it = _sockets.begin();
it < _sockets.end(); it++) { it < _sockets.end(); it++) {
(*it)->check(); (*it)->set_fds();
} }
cout << "finished serv listen\n";
} }
void Server::answer() { void Server::refresh() {
for (std::vector<Socket *>::iterator it = _sockets.begin(); for (std::vector<Socket *>::iterator it = _sockets.begin();
it < _sockets.end(); it++) { it < _sockets.end(); it++) {
(*it)->answer(); (*it)->refresh();
} }
cout << "finished serv answer\n";
} }

91
srcs/load/Socket.cpp

@ -2,16 +2,17 @@
#include "webserv.hpp" #include "webserv.hpp"
Socket::Socket(string def) { Socket::Socket(string def) {
size_t split = def.rfind(':'); size_t sep_pos = def.rfind(':');
size_t ip6_endpos = def.rfind(']');
string tmp = def.substr(0, split); string tmp = def.substr(0, sep_pos);
if (ip6_endpos > sep_pos)
_ip = def;
else
_ip = isInt(tmp) || tmp == "localhost" ? "127.0.0.1" : tmp; _ip = isInt(tmp) || tmp == "localhost" ? "127.0.0.1" : tmp;
tmp = def.substr(split + 1, def.length() - split - 1).c_str(); tmp = def.substr(sep_pos + 1, def.length() - sep_pos - 1).c_str();
_port = !isInt(tmp) ? 80 : std::atoi(tmp.c_str()); _port = !isInt(tmp) ? 80 : std::atoi(tmp.c_str());
_clients_amount = 0;
_max_clients = 30;
for (int i = 0; i < _max_clients; i++)
_client_socket[i] = 0;
} }
Socket::~Socket() { Socket::~Socket() {
close(_master_socket); close(_master_socket);
@ -48,39 +49,28 @@ int Socket::launch() {
return (EXIT_FAILURE); return (EXIT_FAILURE);
} }
cout << "Socket::_master_socket: " << _master_socket << "\n"; cout << "Socket::_master_socket: " << _master_socket << "\n";
if (_master_socket < _min_sd) if (_master_socket < _min_fd)
_min_sd = _master_socket; _min_fd = _master_socket;
_amount++; _amount++;
return (EXIT_SUCCESS); return (EXIT_SUCCESS);
} }
void Socket::check() { void Socket::set_fds() {
int sd;
FD_SET(_master_socket, &_readfds); FD_SET(_master_socket, &_readfds);
cout << "sd_set " << _master_socket << "\n";
for (int i = 0; i < _max_clients; i++) { for (std::vector<int>::iterator it = _clients.begin(); it < _clients.end();
sd = _client_socket[i]; it++) {
//cout << "id: " << i << " -> sd: " << sd << "\n"; FD_SET(*it, &_readfds);
if (sd > 0) if (*it > _max_fd)
{ _max_fd = *it;
FD_SET(sd, &_readfds);
cout << "fd_set " << sd << "\n";
}
if (sd > _max_sd)
_max_sd = sd;
} }
cout << "Socket checked\n";
} }
void Socket::refresh() {
void Socket::answer() { std::vector<int>::iterator it;
int i, sd, valread; int valread;
int addrlen = sizeof(_address); int addrlen = sizeof(_address);
char buffer[1024]; 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)) { if (FD_ISSET(_master_socket, &_readfds)) {
int new_socket = accept(_master_socket, (struct sockaddr *)&_address, int new_socket = accept(_master_socket, (struct sockaddr *)&_address,
(socklen_t *)&addrlen); (socklen_t *)&addrlen);
@ -94,36 +84,31 @@ void Socket::answer() {
cout << "New connection, socket fd is " << new_socket cout << "New connection, socket fd is " << new_socket
<< ", ip is : " << inet_ntoa(_address.sin_addr) << ", ip is : " << inet_ntoa(_address.sin_addr)
<< ", port : " << ntohs(_address.sin_port) << "\n"; << ", port : " << ntohs(_address.sin_port) << "\n";
for (i = 0; i < _max_clients; i++) { _clients.push_back(new_socket);
if (_client_socket[i] == 0) {
_client_socket[i] = new_socket;
cout << "Adding to list of sockets as " << i << "\n";
break;
}
} }
} for (it = _clients.begin(); it < _clients.end(); it++) {
cout << "Socket: " << _ip << ":" << _port << "\n"; if (FD_ISSET(*it, &_readfds)) {
for (i = 0; i < _max_clients; i++) { valread = read(*it, buffer, 1024);
sd = _client_socket[i];
if (FD_ISSET(sd, &_readfds)) {
cout << "Client " << i << ": set\n";
valread = read(sd, buffer, 1024);
if (valread == 0) { if (valread == 0) {
getpeername(sd, (struct sockaddr *)&_address, getpeername(*it, (struct sockaddr *)&_address,
(socklen_t *)&addrlen); (socklen_t *)&addrlen);
cout << "Host disconnected, ip " << inet_ntoa(_address.sin_addr) cout << "Host disconnected, ip " << inet_ntoa(_address.sin_addr)
<< ", port " << ntohs(_address.sin_port) << "\n"; << ", port " << ntohs(_address.sin_port) << "\n";
close(sd); close(*it);
_client_socket[i] = 0; _clients.erase(it);
} else { } else
cout << buffer << "\n"; this->answer(*it, buffer);
}
}
}
void Socket::answer(int fd, string request) {
char r404[72] =
"HTTP/1.1 200 OK\nContent-Type: text/plain\nContent-Length: 4\n\n404!";
cout << request << "\n";
#ifdef __linux__ #ifdef __linux__
send(sd, r404, strlen(r404), MSG_NOSIGNAL); send(fd, r404, strlen(r404), MSG_NOSIGNAL);
#elif __APPLE__ #elif __APPLE__
send(sd, r404, strlen(r404), 0); send(fd, r404, strlen(r404), 0);
#endif #endif
}
}
}
cout << "Socket answered\n";
} }

14
srcs/webserv.cpp

@ -2,8 +2,8 @@
#include <time.h> #include <time.h>
fd_set Socket::_readfds; fd_set Socket::_readfds;
int Socket::_max_sd; int Socket::_max_fd;
int Socket::_min_sd = INT_MAX; int Socket::_min_fd = INT_MAX;
int Socket::_amount = 0; int Socket::_amount = 0;
int main(int ac, char **av) { int main(int ac, char **av) {
@ -18,15 +18,13 @@ int main(int ac, char **av) {
cout << "Setting environment...\n"; cout << "Setting environment...\n";
Env env(conf); Env env(conf);
while (1) { while (1) {
cout << "Cycling...\n";
FD_ZERO(&Socket::_readfds); FD_ZERO(&Socket::_readfds);
Socket::_max_sd = Socket::_min_sd; Socket::_max_fd = Socket::_min_fd;
env.listen(); env.set_fds();
cout << "Socket::_max_sd " << Socket::_max_sd << "\n"; int activity = select(Socket::_max_fd + Socket::_amount, &(Socket::_readfds), NULL, NULL, NULL);
int activity = select(Socket::_max_sd + Socket::_amount, &(Socket::_readfds), NULL, NULL, NULL);
if ((activity < 0) && (errno != EINTR)) if ((activity < 0) && (errno != EINTR))
cout << "Select: " << strerror(errno) << "\n"; cout << "Select: " << strerror(errno) << "\n";
env.answer(); env.refresh();
} }
} }
return (0); return (0);

Loading…
Cancel
Save