Browse Source

Master launch -> constructor

master
nicolas-arnaud 2 years ago
parent
commit
57ed15a9fa
  1. 2
      includes/Master.hpp
  2. 20
      srcs/load/Server.cpp
  3. 29
      srcs/sock/Master.cpp

2
includes/Master.hpp

@ -2,7 +2,6 @@
#include "webserv.hpp" #include "webserv.hpp"
class Master { class Master {
// int _clients_amount;
int _fd; int _fd;
std::vector< Client * > _childs; std::vector< Client * > _childs;
struct sockaddr_in _address; struct sockaddr_in _address;
@ -12,7 +11,6 @@ class Master {
Master(int fd, Master *parent); Master(int fd, Master *parent);
~Master(void); ~Master(void);
int launch(void);
void set_fds(void); void set_fds(void);
void refresh(Env *env); void refresh(Env *env);
Server *choose_server(Env *env, string host); Server *choose_server(Env *env, string host);

20
srcs/load/Server.cpp

@ -46,19 +46,23 @@ std::vector< Master * > Server::get_sockets(JSONNode *server) {
cout << "Listen: IPv6 isn't supported\n"; cout << "Listen: IPv6 isn't supported\n";
continue; continue;
} }
_listens.push_back(listen); try {
Master *sock = new Master(listen); Master *sock = new Master(listen);
if (sock->launch() == EXIT_SUCCESS)
ret.push_back(sock); ret.push_back(sock);
else _listens.push_back(listen);
delete sock; } catch (std::exception &e) {
cout << e.what() << '\n';
}
} }
} else { } else {
listen = get_listen_t("localhost:80"); listen = get_listen_t("localhost:80");
Master *sock = new Master(listen);
_listens.push_back(listen); try {
if (sock->launch() == EXIT_SUCCESS) { Master *sock = new Master(listen);
_listens.push_back(listen);
ret.push_back(sock); ret.push_back(sock);
} catch (std::exception &e) {
cout << e.what() << '\n';
} }
} }
return ret; return ret;

29
srcs/sock/Master.cpp

@ -1,47 +1,38 @@
#include "webserv.hpp" #include "webserv.hpp"
Master::Master(listen_t listen) : _listen(listen) {}
Master::~Master(void) { Master::~Master(void) {
close(_fd); close(_fd);
cout << "Destroyed master socket\n"; cout << "Destroyed master socket\n";
} }
int Master::launch(void) { Master::Master(listen_t list) : _listen(list) {
int opt = 1; int opt = 1;
string ip = _listen.ip; string ip = _listen.ip;
int port = _listen.port; int port = _listen.port;
_fd = socket(AF_INET, SOCK_STREAM, 0); _fd = socket(AF_INET, SOCK_STREAM, 0);
if (_fd == 0) { if (_fd == 0)
cout << "Socket creation: " << strerror(errno) << "\n"; throw std::runtime_error("socket() error" + string(strerror(errno)));
return (EXIT_FAILURE);
}
int opt_ret = int opt_ret =
setsockopt(_fd, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt)); setsockopt(_fd, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt));
if (opt_ret < 0) { if (opt_ret < 0)
cout << "Sockopt: " << strerror(errno) << "\n"; throw std::runtime_error("setsockopt() error: " +
return (EXIT_FAILURE); string(strerror(errno)));
}
_address.sin_family = AF_INET; _address.sin_family = AF_INET;
_address.sin_addr.s_addr = inet_addr(ip.c_str()); _address.sin_addr.s_addr = inet_addr(ip.c_str());
_address.sin_port = htons(port); _address.sin_port = htons(port);
if (bind(_fd, (struct sockaddr *)&_address, sizeof(_address)) < 0) { if (bind(_fd, (struct sockaddr *)&_address, sizeof(_address)) < 0)
cout << "Bind: " << strerror(errno) << "\n"; throw std::runtime_error("bind() error: " + string(strerror(errno)));
return (EXIT_FAILURE);
}
cout << "Listener " << ip << " on port " << port << "\n"; cout << "Listener " << ip << " on port " << port << "\n";
if (listen(_fd, 3) < 0) { if (listen(_fd, 3) < 0)
cout << "Listen: " << strerror(errno) << "\n"; throw std::runtime_error("listen() error: " + string(strerror(errno)));
return (EXIT_FAILURE);
}
cout << "Master: " << _fd << "\n"; cout << "Master: " << _fd << "\n";
if (_fd < _min_fd) if (_fd < _min_fd)
_min_fd = _fd; _min_fd = _fd;
_amount++; _amount++;
return (EXIT_SUCCESS);
} }
void Master::set_fds(void) { void Master::set_fds(void) {

Loading…
Cancel
Save