Browse Source

update comments

master
nicolas-arnaud 2 years ago
parent
commit
8bcb4a6880
  1. 1
      includes/Env.hpp
  2. 3
      includes/Master.hpp
  3. 18
      srcs/load/Env.cpp
  4. 4
      srcs/sock/Client.cpp
  5. 38
      srcs/sock/Master.cpp

1
includes/Env.hpp

@ -10,5 +10,4 @@ public:
Env(JSONNode *conf);
~Env(void);
void cycle(void);
void post_poll(void);
};

3
includes/Master.hpp

@ -11,7 +11,8 @@ public:
Master(ip_port_t listen);
~Master(void);
void post_poll(Env *env);
void check_socket(void);
void check_childs(Env *env);
Server *choose_server(Env *env, string host);
ip_port_t _listen;

18
srcs/load/Env.cpp

@ -50,24 +50,18 @@ Env::~Env() {
/**
* @brief A Wevserv cycle:
* - append sockets to listen to select list
* - select ehem
* - refresh and handle requests
* - poll the Master::pollfds array
* - for each master socket, call his own post_poll method to check and handle the sockets flaged.
*/
void Env::cycle(void) {
if (!SILENT) cout << "|===||===| Waiting some HTTP request... |===||===|\n";
int pollResult = poll(Master::_pollfds, Master::_poll_id_amount + 1, 5000);
if ((pollResult < 0) && (errno != EINTR)) std::cerr << "Select: " << strerror(errno) << "\n";
if (pollResult > 0) post_poll();
}
/**
* @brief Refresh all master_sockets and their clients datas (disconnect, new
* connection, etc..) and parse requests recieved.
*/
void Env::post_poll() {
if (pollResult > 0) {
if (!SILENT) cout << "==> Handle requests and answers:\n";
for (std::vector<Master *>::iterator it = this->_masters.begin(); it < this->_masters.end(); it++) try {
(*it)->post_poll(this);
(*it)->check_socket();
(*it)->check_childs(this);
} catch (std::exception &e) { std::cerr << e.what(); }
}
}

4
srcs/sock/Client.cpp

@ -22,6 +22,10 @@ Client::Client(int fd, ip_port_t ip_port, Master *parent) : _fd(fd), _ip_port(ip
Client::~Client(void) {
close(_fd);
Master::_pollfds[_poll_id].fd = 0;
Master::_pollfds[_poll_id].events = 0;
Master::_pollfds[_poll_id].revents = 0;
Master::_poll_id_amount--;
_headers.clear();
if (!SILENT) cout << "Host disconnected, ip " << _ip_port.ip << ", port " << _ip_port.port << "\n";
}

38
srcs/sock/Master.cpp

@ -19,7 +19,7 @@ Master::~Master(void) {
/**
* @brief Constructor
* Try to create a socket listening to ip and port defined by input.
* If no exception if thrown, the creation success and the socket is then ready to select for new clients.
* If no exception if thrown, the creation success and the socket is then ready to listen for new clients.
*
* @param list An ip_port_t struct which contain the ip and the port the master listen.
*/
@ -48,15 +48,9 @@ Master::Master(ip_port_t list) : _listen(list) {
}
/**
* @brief Checkk master and his clients sockets after select performed.
* - First look for new clients
* - Then handle clients awaiting action:
* - Disconnect them if nothing's new on the socket (request altready handled)
* - Read and parse request else until it's ready to answer and does it.
*
* @param env The environment object which contain the liste of servers to know which one the client is trying to reach.
* @brief Check master and his clients sockets after poll performed.
*/
void Master::post_poll(Env *env) {
void Master::check_socket(void) {
int addrlen = sizeof(_address);
if (_pollfds[_poll_id].revents & POLLIN) { /// < incomming master request
@ -82,33 +76,37 @@ void Master::post_poll(Env *env) {
}
}
}
}
/**
* @brief Handle master's childs.
* Loop along _childs
* if poll even incomming on the child socket:
* - give 1023 next sockets bits to Client methods to handle.
* - if Request is fully received, does the answer and flag the socket for outcomming event.
* - if request isn't fully received, flag the socket for incomming event.
* - if read returned 0, delete client.
* @param env The environment object.
*/
void Master::check_childs(Env *env) {
int child_fd;
for (std::vector<Client *>::iterator it = _childs.begin(); it < _childs.end(); it++) {
child_fd = (*it)->_fd;
int i = (*it)->_poll_id;
if (_pollfds[i].fd > 0 && _pollfds[i].revents & POLLIN) {
char buffer[128];
int valread = read(child_fd, buffer, 127);
char buffer[1024];
int valread = read(child_fd, buffer, 1023);
buffer[valread] = '\0';
if (valread == 0) {
// getpeername(child_fd, (struct sockaddr *)&_address, (socklen_t *)&addrlen);
delete (*it);
_childs.erase(it);
_pollfds[i].fd = 0;
_pollfds[i].events = 0;
_pollfds[i].revents = 0;
_poll_id_amount--;
} else if ((*it)->getRequest(env, buffer)) {
(*it)->handleRequest();
_pollfds[i].events = POLLOUT;
if ((*it)->_finish) {
delete (*it);
_childs.erase(it);
_pollfds[i].fd = 0;
_pollfds[i].events = 0;
_pollfds[i].revents = 0;
_poll_id_amount--;
}
} else _pollfds[i].events = POLLIN | POLLPRI;
}

Loading…
Cancel
Save