diff --git a/includes/Master.hpp b/includes/Master.hpp index 9e1c9d2..f2c89ec 100644 --- a/includes/Master.hpp +++ b/includes/Master.hpp @@ -2,7 +2,6 @@ #include "webserv.hpp" class Master { - // int _clients_amount; int _fd; std::vector< Client * > _childs; struct sockaddr_in _address; @@ -12,7 +11,6 @@ class Master { Master(int fd, Master *parent); ~Master(void); - int launch(void); void set_fds(void); void refresh(Env *env); Server *choose_server(Env *env, string host); diff --git a/srcs/load/Server.cpp b/srcs/load/Server.cpp index e5e7987..0030f33 100644 --- a/srcs/load/Server.cpp +++ b/srcs/load/Server.cpp @@ -46,19 +46,23 @@ std::vector< Master * > Server::get_sockets(JSONNode *server) { cout << "Listen: IPv6 isn't supported\n"; continue; } - _listens.push_back(listen); - Master *sock = new Master(listen); - if (sock->launch() == EXIT_SUCCESS) + try { + Master *sock = new Master(listen); ret.push_back(sock); - else - delete sock; + _listens.push_back(listen); + } catch (std::exception &e) { + cout << e.what() << '\n'; + } } } else { listen = get_listen_t("localhost:80"); - Master *sock = new Master(listen); - _listens.push_back(listen); - if (sock->launch() == EXIT_SUCCESS) { + + try { + Master *sock = new Master(listen); + _listens.push_back(listen); ret.push_back(sock); + } catch (std::exception &e) { + cout << e.what() << '\n'; } } return ret; diff --git a/srcs/sock/Master.cpp b/srcs/sock/Master.cpp index 98e91e5..53cfaeb 100644 --- a/srcs/sock/Master.cpp +++ b/srcs/sock/Master.cpp @@ -1,47 +1,38 @@ #include "webserv.hpp" -Master::Master(listen_t listen) : _listen(listen) {} Master::~Master(void) { close(_fd); cout << "Destroyed master socket\n"; } -int Master::launch(void) { +Master::Master(listen_t list) : _listen(list) { int opt = 1; string ip = _listen.ip; int port = _listen.port; _fd = socket(AF_INET, SOCK_STREAM, 0); - if (_fd == 0) { - cout << "Socket creation: " << strerror(errno) << "\n"; - return (EXIT_FAILURE); - } + if (_fd == 0) + throw std::runtime_error("socket() error" + string(strerror(errno))); int opt_ret = setsockopt(_fd, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt)); - if (opt_ret < 0) { - cout << "Sockopt: " << strerror(errno) << "\n"; - return (EXIT_FAILURE); - } + if (opt_ret < 0) + throw std::runtime_error("setsockopt() error: " + + string(strerror(errno))); _address.sin_family = AF_INET; _address.sin_addr.s_addr = inet_addr(ip.c_str()); _address.sin_port = htons(port); - if (bind(_fd, (struct sockaddr *)&_address, sizeof(_address)) < 0) { - cout << "Bind: " << strerror(errno) << "\n"; - return (EXIT_FAILURE); - } + if (bind(_fd, (struct sockaddr *)&_address, sizeof(_address)) < 0) + throw std::runtime_error("bind() error: " + string(strerror(errno))); cout << "Listener " << ip << " on port " << port << "\n"; - if (listen(_fd, 3) < 0) { - cout << "Listen: " << strerror(errno) << "\n"; - return (EXIT_FAILURE); - } + if (listen(_fd, 3) < 0) + throw std::runtime_error("listen() error: " + string(strerror(errno))); cout << "Master: " << _fd << "\n"; if (_fd < _min_fd) _min_fd = _fd; _amount++; - return (EXIT_SUCCESS); } void Master::set_fds(void) {