Browse Source

launch multiples serv

test
nicolas-arnaud 2 years ago
parent
commit
5e41648698
  1. 9
      srcs/load/Env.hpp
  2. 143
      srcs/load/Server.hpp

9
srcs/load/Env.hpp

@ -3,7 +3,7 @@
class Env { class Env {
//int _max_clients; //int _max_clients;
std::vector<Server> _servers; std::vector<Server *> _servers;
public: public:
Env(JSONNode *conf) { Env(JSONNode *conf) {
JSONList servers = conf->obj()["servers"]->lst(); JSONList servers = conf->obj()["servers"]->lst();
@ -13,10 +13,17 @@ class Env {
it < servers.end(); it++) { it < servers.end(); it++) {
Server *server = new Server(*it); Server *server = new Server(*it);
server->launch(); server->launch();
_servers.push_back(server);
//delete *it; //delete *it;
cout << th[i] << " server launched.\n"; cout << th[i] << " server launched.\n";
i++; i++;
} }
while(1) {
for (std::vector<Server *>::iterator it = _servers.begin();
it < _servers.end(); it++) {
(*it)->check();
}
}
//delete conf; //delete conf;
} }
}; };

143
srcs/load/Server.hpp

@ -6,6 +6,8 @@ class Server {
std::vector<string> _listens; std::vector<string> _listens;
int _port; int _port;
std::map<string, Route *> _routes; std::map<string, Route *> _routes;
int _master_socket;
struct sockaddr_in _address;
public: public:
Server(JSONNode *server) { Server(JSONNode *server) {
@ -18,116 +20,119 @@ class Server {
for (JSONList::iterator i = listens.begin(); i < listens.end(); i++) for (JSONList::iterator i = listens.begin(); i < listens.end(); i++)
_listens.push_back((*i)->str()); _listens.push_back((*i)->str());
_port = std::atoi(_listens.front().c_str()); _port = std::atoi(_listens.front().c_str());
} }
//_routes["default"] = new Route(datas["root"], datas["return"], datas["index"], datas["autoindex"]); //_routes["default"] = new Route(datas["root"], datas["return"], datas["index"], datas["autoindex"]);
} }
void launch() { void launch() {
int opt = 1; int opt = 1;
int master_socket, addrlen, new_socket, client_socket[30],
max_clients = 30, activity, i, valread, sd;
struct sockaddr_in address;
char buffer[30000];
char _404[72] =
"HTTP/1.1 200 OK\nContent-Type: text/plain\nContent-Length: 4\n\n404!";
int max_sd;
fd_set readfds;
for (i = 0; i < max_clients; i++)
client_socket[i] = 0;
if ((master_socket = socket(AF_INET, SOCK_STREAM, 0)) == 0) { _master_socket = socket(AF_INET, SOCK_STREAM, 0);
if (_master_socket == 0) {
cout << strerror(errno) << "\n"; cout << strerror(errno) << "\n";
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (setsockopt(master_socket, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, if (setsockopt(_master_socket, SOL_SOCKET, SO_REUSEADDR, (char *)&opt,
sizeof(opt)) < 0) { sizeof(opt)) < 0) {
cout << strerror(errno) << "\n"; cout << strerror(errno) << "\n";
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
address.sin_family = AF_INET; _address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY; _address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(_port); _address.sin_port = htons(_port);
if (bind(master_socket, (struct sockaddr *)&address, sizeof(address)) < 0) { if (bind(_master_socket, (struct sockaddr *)&_address, sizeof(_address)) < 0) {
cout << strerror(errno) << "\n"; cout << strerror(errno) << "\n";
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
cout << "Listener on port " << _port << "\n"; cout << "Listener on port " << _port << "\n";
if (listen(master_socket, 3) < 0) { if (listen(_master_socket, 3) < 0) {
cout << strerror(errno) << "\n"; cout << strerror(errno) << "\n";
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
addrlen = sizeof(address);
cout << "Waiting for connections ..." << "\n"; cout << "Waiting for connections ..." << "\n";
}
while (1) { void check() {
FD_ZERO(&readfds); int new_socket, activity, i, valread, sd;
FD_SET(master_socket, &readfds); char buffer[30000];
max_sd = master_socket; char _404[72] =
"HTTP/1.1 200 OK\nContent-Type: text/plain\nContent-Length: 4\n\n404!";
for (i = 0; i < max_clients; i++) { int max_clients = 30;
sd = client_socket[i]; int client_socket[30];
if (sd > 0) fd_set readfds;
FD_SET(sd, &readfds); int max_sd = _master_socket;
if (sd > max_sd)
max_sd = sd;
}
activity = select(max_sd + 1, &readfds, NULL, NULL, NULL);
if ((activity < 0) && (errno != EINTR))
cout << strerror(errno) << "\n";
if (FD_ISSET(master_socket, &readfds)) { int addrlen = sizeof(_address);
if ((new_socket = accept(master_socket, (struct sockaddr *)&address, for (i = 0; i < max_clients; i++)
(socklen_t *)&addrlen)) < 0) { client_socket[i] = 0;
cout << strerror(errno) << "\n"; FD_ZERO(&readfds);
exit(EXIT_FAILURE); FD_SET(_master_socket, &readfds);
}
for (i = 0; i < max_clients; i++) {
sd = client_socket[i];
if (sd > 0)
FD_SET(sd, &readfds);
if (sd > max_sd)
max_sd = sd;
}
activity = select(max_sd + 1, &readfds, NULL, NULL, NULL);
if ((activity < 0) && (errno != EINTR))
cout << strerror(errno) << "\n";
if (FD_ISSET(_master_socket, &readfds)) {
if ((new_socket = accept(_master_socket, (struct sockaddr *)&_address,
(socklen_t *)&addrlen)) < 0) {
cout << strerror(errno) << "\n";
exit(EXIT_FAILURE);
}
#ifdef __APPLE__ #ifdef __APPLE__
fcntl(new_socket, F_GETNOSIGPIPE); fcntl(new_socket, F_GETNOSIGPIPE);
#endif #endif
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++) { for (i = 0; i < max_clients; i++) {
if (client_socket[i] == 0) { if (client_socket[i] == 0) {
client_socket[i] = new_socket; client_socket[i] = new_socket;
cout << "Adding to list of sockets as " << i cout << "Adding to list of sockets as " << i
<< "\n"; << "\n";
break; break;
}
} }
} }
}
for (i = 0; i < max_clients; i++) { for (i = 0; i < max_clients; i++) {
sd = client_socket[i]; sd = client_socket[i];
if (FD_ISSET(sd, &readfds)) { if (FD_ISSET(sd, &readfds)) {
valread = read(sd, buffer, 30000); valread = read(sd, buffer, 30000);
if (valread == 0) { if (valread == 0) {
getpeername(sd, (struct sockaddr *)&address, getpeername(sd, (struct sockaddr *)&_address,
(socklen_t *)&addrlen); (socklen_t *)&addrlen);
cout << "Host disconnected, ip " cout << "Host disconnected, ip "
<< inet_ntoa(address.sin_addr) << ", port " << inet_ntoa(_address.sin_addr) << ", port "
<< ntohs(address.sin_port) << "\n"; << ntohs(_address.sin_port) << "\n";
close(sd); close(sd);
client_socket[i] = 0; client_socket[i] = 0;
} else } else
#ifdef __linux__ #ifdef __linux__
send(sd, _404, strlen(_404), MSG_NOSIGNAL); send(sd, _404, strlen(_404), MSG_NOSIGNAL);
#elif __APPLE__ #elif __APPLE__
send(sd, _404, strlen(_404), 0); send(sd, _404, strlen(_404), 0);
#endif #endif
} }
} }
} }
}
}; };

Loading…
Cancel
Save