mirror of
https://github.com/3lswear/webserv.git
synced 2025-10-28 21:07:59 +03:00
feat: add epoll_handle and cleanup
This commit is contained in:
@@ -1,6 +1,5 @@
|
|||||||
#include "Server.hpp"
|
#include "Server.hpp"
|
||||||
#include <exception>
|
#include <exception>
|
||||||
#include <cassert>
|
|
||||||
|
|
||||||
#define THREAD_NUM 100
|
#define THREAD_NUM 100
|
||||||
#define MAX_EVENTS
|
#define MAX_EVENTS
|
||||||
@@ -49,28 +48,27 @@ void Server::sendData(Client &client, int fd)
|
|||||||
else
|
else
|
||||||
send_len = BUFFSIZE;
|
send_len = BUFFSIZE;
|
||||||
|
|
||||||
/* DBOUT << YELLO << tmp << ENDL; */
|
|
||||||
/* DBOUT << GREEN << client.getCounter() << ENDL; */
|
|
||||||
DBOUT << WARNING << getDebugTime() << OKCYAN << " sent " << send_len << " to client " << fd << ENDL;
|
|
||||||
sent = send(fd, tmp + client.getCounter(), send_len, MSG_NOSIGNAL);
|
sent = send(fd, tmp + client.getCounter(), send_len, MSG_NOSIGNAL);
|
||||||
if (sent < 0)
|
DBOUT << getDebugTime()
|
||||||
|
<< " sent "
|
||||||
|
<< send_len <<
|
||||||
|
"/" << sent <<
|
||||||
|
" to client " << fd << ENDL;
|
||||||
|
if (sent <= 0)
|
||||||
{
|
{
|
||||||
DBOUT << WARNING << getDebugTime() << FAIL << " SEND FAILED !@!!!" << ENDL;
|
DBOUT << getDebugTime() << FAIL << " SEND FAILED" << ENDL;
|
||||||
client.done = true;
|
client.done = true;
|
||||||
}
|
}
|
||||||
else if (sent > 0)
|
else if (sent > 0)
|
||||||
client.increaseCounter();
|
client.increaseCounter();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Server::readSocket(Client &client, int fd)
|
void Server::readSocket(Client &client, int fd)
|
||||||
{
|
{
|
||||||
int status;
|
|
||||||
int bytes_read;
|
int bytes_read;
|
||||||
std::string stringBUF(BUFFSIZE, 0);
|
std::string stringBUF(BUFFSIZE, 0);
|
||||||
|
|
||||||
DBOUT << TURQ << "IN readSocket" << ENDL;
|
DBOUT << TURQ << "IN readSocket" << ENDL;
|
||||||
// DBOUT << "client in readSocket "<< &client << ENDL;
|
|
||||||
bytes_read = recv(fd, &stringBUF[0], BUFFSIZE, 0);
|
bytes_read = recv(fd, &stringBUF[0], BUFFSIZE, 0);
|
||||||
if (bytes_read == 0)
|
if (bytes_read == 0)
|
||||||
{
|
{
|
||||||
@@ -80,7 +78,6 @@ void Server::readSocket(Client &client, int fd)
|
|||||||
else if (bytes_read == -1)
|
else if (bytes_read == -1)
|
||||||
{
|
{
|
||||||
DBOUT << WARNING << getDebugTime() << FAIL << " bytes_read -1" << ENDL;
|
DBOUT << WARNING << getDebugTime() << FAIL << " bytes_read -1" << ENDL;
|
||||||
// client.allRead = true;
|
|
||||||
client.done = true;
|
client.done = true;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -88,18 +85,10 @@ void Server::readSocket(Client &client, int fd)
|
|||||||
stringBUF.erase(bytes_read, stringBUF.size());
|
stringBUF.erase(bytes_read, stringBUF.size());
|
||||||
client.setRawData(stringBUF);
|
client.setRawData(stringBUF);
|
||||||
client.increaseRecvCounter(bytes_read);
|
client.increaseRecvCounter(bytes_read);
|
||||||
status = client.parseRequest();
|
client.parseRequest();
|
||||||
// client.printClientInfo();
|
|
||||||
if (client.allRecved())
|
if (client.allRecved())
|
||||||
client.allRead = true;
|
client.allRead = true;
|
||||||
|
|
||||||
// DBOUT << GREEN << "recvCounter " << client.getRecvCounter() << ENDL;
|
|
||||||
// DBOUT << GREEN << "contentLength " << client.getRequest().getContentLength() << ENDL;
|
|
||||||
// DBOUT << GREEN << "allRead " << client.allRead << ENDL;
|
|
||||||
|
|
||||||
// DBOUT << BLUE << "status is " << Response::getReasonPhrase(status) << ENDL;
|
|
||||||
}
|
}
|
||||||
(void)status;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inline int Server::delete_client_force(std::map<int, Client *> &client_map, int fd)
|
inline int Server::delete_client_force(std::map<int, Client *> &client_map, int fd)
|
||||||
@@ -109,14 +98,12 @@ inline int Server::delete_client_force(std::map<int, Client *> &client_map, int
|
|||||||
<< fd
|
<< fd
|
||||||
<< ENDL;
|
<< ENDL;
|
||||||
|
|
||||||
int ret;
|
epoll_handle(EPOLL_CTL_DEL, fd, 0);
|
||||||
ret = epoll_ctl(_epoll_fd, EPOLL_CTL_DEL, fd, NULL);
|
|
||||||
assert(ret == 0);
|
|
||||||
close(fd);
|
close(fd);
|
||||||
client_map[fd]->clear();
|
client_map[fd]->clear();
|
||||||
delete (client_map[fd]);
|
delete (client_map[fd]);
|
||||||
client_map.erase(fd);
|
client_map.erase(fd);
|
||||||
return (ret);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -124,56 +111,45 @@ inline int Server::delete_client(std::map<int, Client *> &client_map, int fd)
|
|||||||
{
|
{
|
||||||
if (client_map[fd]->getRequest().getConnection() == "close")
|
if (client_map[fd]->getRequest().getConnection() == "close")
|
||||||
{
|
{
|
||||||
DBOUT << WARNING << getDebugTime() << OKCYAN
|
DBOUT << WARNING << getDebugTime()
|
||||||
<< " completely deleting client "
|
<< " completely deleting client "
|
||||||
<< fd
|
<< fd
|
||||||
<< ENDL;
|
<< ENDL;
|
||||||
|
|
||||||
int ret;
|
epoll_handle(EPOLL_CTL_DEL, fd, 0);
|
||||||
ret = epoll_ctl(_epoll_fd, EPOLL_CTL_DEL, fd, NULL);
|
|
||||||
close(fd);
|
close(fd);
|
||||||
client_map[fd]->clear();
|
client_map[fd]->clear();
|
||||||
delete (client_map[fd]);
|
delete (client_map[fd]);
|
||||||
client_map.erase(fd);
|
client_map.erase(fd);
|
||||||
return (ret);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
DBOUT << WARNING << getDebugTime() << OKCYAN
|
DBOUT << WARNING << getDebugTime()
|
||||||
<< " deleting only client "
|
<< " deleting only client "
|
||||||
<< fd
|
<< fd
|
||||||
<< ENDL;
|
<< ENDL;
|
||||||
|
|
||||||
int ret;
|
t_fd_info *tmp_fd;
|
||||||
struct epoll_event ev;
|
|
||||||
t_tmp_fd *tmp_fd;
|
|
||||||
|
|
||||||
ev.events = EPOLLIN;
|
epoll_handle(EPOLL_CTL_MOD, fd, EPOLLIN);
|
||||||
ev.data.fd = fd;
|
|
||||||
ret = epoll_ctl(_epoll_fd, EPOLL_CTL_MOD, fd, &ev);
|
|
||||||
|
|
||||||
//Добавляю фд в список
|
tmp_fd = new t_fd_info;
|
||||||
tmp_fd = new t_tmp_fd;
|
|
||||||
tmp_fd->ip_port = client_map[fd]->getIpPort();
|
tmp_fd->ip_port = client_map[fd]->getIpPort();
|
||||||
gettimeofday(&tmp_fd->last_modif, NULL);
|
gettimeofday(&tmp_fd->last_modif, NULL);
|
||||||
vacant_fds[fd] = tmp_fd;
|
vacant_fds[fd] = tmp_fd;
|
||||||
|
|
||||||
//Очищаю клиента
|
|
||||||
client_map[fd]->clear();
|
client_map[fd]->clear();
|
||||||
// delete (client_map[fd]);
|
|
||||||
client_map[fd]->~Client();
|
client_map[fd]->~Client();
|
||||||
// client_map.erase(fd);
|
|
||||||
return (ret);
|
|
||||||
}
|
}
|
||||||
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline int Server::delete_fd(std::map<int, t_tmp_fd *> &map,
|
inline int Server::delete_fd(std::map<int, t_fd_info *> &map,
|
||||||
std::map<int, t_tmp_fd *>::iterator &it,
|
std::map<int, t_fd_info *>::iterator &it,
|
||||||
std::map<int, Client *> &client_map)
|
std::map<int, Client *> &client_map)
|
||||||
{
|
{
|
||||||
int ret;
|
epoll_handle(EPOLL_CTL_DEL, it->first, 0);
|
||||||
ret = epoll_ctl(_epoll_fd, EPOLL_CTL_DEL, it->first, NULL);
|
DBOUT << WARNING << getDebugTime()
|
||||||
DBOUT << WARNING << getDebugTime() << OKCYAN
|
|
||||||
<< " deleting fd "
|
<< " deleting fd "
|
||||||
<< it->first
|
<< it->first
|
||||||
<< ENDL;
|
<< ENDL;
|
||||||
@@ -182,7 +158,7 @@ inline int Server::delete_fd(std::map<int, t_tmp_fd *> &map,
|
|||||||
client_map.erase(it->first);
|
client_map.erase(it->first);
|
||||||
map.erase(it++);
|
map.erase(it++);
|
||||||
|
|
||||||
return (ret);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -236,13 +212,23 @@ void Server::setup_server_socks(std::map<int, Socket> &configurations_map)
|
|||||||
throw std::domain_error("No servers were set up. Exiting.");
|
throw std::domain_error("No servers were set up. Exiting.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline void Server::epoll_handle(int op, int fd, int event)
|
||||||
|
{
|
||||||
|
struct epoll_event ev;
|
||||||
|
ev.data.fd = fd;
|
||||||
|
ev.events = event;
|
||||||
|
|
||||||
|
if (epoll_ctl(_epoll_fd, op, fd, &ev))
|
||||||
|
{
|
||||||
|
std::cerr << getDebugTime() << FAIL;
|
||||||
|
perror("epoll_ctl");
|
||||||
|
std::cerr << RESET;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
inline void Server::add_to_epoll_list(int fd, unsigned int ep_events)
|
inline void Server::add_to_epoll_list(int fd, unsigned int ep_events)
|
||||||
{
|
{
|
||||||
struct epoll_event ev;
|
epoll_handle(EPOLL_CTL_ADD, fd, ep_events);
|
||||||
ev.events = ep_events;
|
|
||||||
ev.data.fd = fd;
|
|
||||||
|
|
||||||
assert(epoll_ctl(_epoll_fd, EPOLL_CTL_ADD, fd, &ev) == 0);
|
|
||||||
setNonBlock(fd);
|
setNonBlock(fd);
|
||||||
DBOUT << GREEN
|
DBOUT << GREEN
|
||||||
<< "add socket "
|
<< "add socket "
|
||||||
@@ -277,7 +263,7 @@ void Server::run(void)
|
|||||||
std::map<int, Client*> client_map;
|
std::map<int, Client*> client_map;
|
||||||
std::map<int, Socket> configurations_map;
|
std::map<int, Socket> configurations_map;
|
||||||
|
|
||||||
std::map<int, t_tmp_fd *>::iterator fd_it;
|
std::map<int, t_fd_info *>::iterator fd_it;
|
||||||
|
|
||||||
unsigned int client_events = EPOLLIN;
|
unsigned int client_events = EPOLLIN;
|
||||||
|
|
||||||
@@ -290,12 +276,14 @@ void Server::run(void)
|
|||||||
|
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
|
|
||||||
int ready_num = epoll_wait(_epoll_fd, _events, MAX_CLIENT, 5000);
|
int ready_num = epoll_wait(_epoll_fd, _events, MAX_CLIENT, 5000);
|
||||||
DBOUT << TURQ << "ready_num " << ready_num << ENDL;
|
|
||||||
|
|
||||||
if (ready_num < 0)
|
if (ready_num < 0)
|
||||||
throw std::logic_error("epoll_ret");
|
{
|
||||||
|
std::cerr << getDebugTime() << FAIL;
|
||||||
|
perror("epoll_wait");
|
||||||
|
std::cerr << RESET;
|
||||||
|
}
|
||||||
|
|
||||||
for (int i = 0; i < ready_num; i++)
|
for (int i = 0; i < ready_num; i++)
|
||||||
{
|
{
|
||||||
@@ -304,7 +292,7 @@ void Server::run(void)
|
|||||||
std::map<int, Socket>::iterator sock_it;
|
std::map<int, Socket>::iterator sock_it;
|
||||||
|
|
||||||
DBOUT << "FD is " << fd << ENDL;
|
DBOUT << "FD is " << fd << ENDL;
|
||||||
/* print_epoll_events(events); */
|
print_epoll_events(events);
|
||||||
|
|
||||||
if ((events & EPOLLIN)
|
if ((events & EPOLLIN)
|
||||||
&& (sock_it = configurations_map.find(fd)) != configurations_map.end())
|
&& (sock_it = configurations_map.find(fd)) != configurations_map.end())
|
||||||
@@ -317,45 +305,38 @@ void Server::run(void)
|
|||||||
add_to_epoll_list(client_sock, client_events);
|
add_to_epoll_list(client_sock, client_events);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
throw std::logic_error("accept didnt work");
|
{
|
||||||
|
std::cerr << getDebugTime() << WARNING;
|
||||||
|
perror("Accept");
|
||||||
|
std::cerr << RESET;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
fd_it = vacant_fds.find(fd);
|
if (events & EPOLLIN)
|
||||||
if (fd_it != vacant_fds.end())
|
|
||||||
{
|
|
||||||
// client_map[fd] = new Client(fd_it->second->ip_port);
|
|
||||||
// DBOUT << "addr of client_map[fd] " << client_map[fd] << ENDL;
|
|
||||||
new (client_map[fd]) Client(fd_it->second->ip_port);
|
|
||||||
delete fd_it->second;
|
|
||||||
vacant_fds.erase(fd);
|
|
||||||
}
|
|
||||||
else if (events & EPOLLIN)
|
|
||||||
{
|
{
|
||||||
|
fd_it = vacant_fds.find(fd);
|
||||||
|
if (fd_it != vacant_fds.end())
|
||||||
|
{
|
||||||
|
DBOUT << "creating new client for vacant fd" << ENDL;
|
||||||
|
new (client_map[fd]) Client(fd_it->second->ip_port);
|
||||||
|
delete fd_it->second;
|
||||||
|
vacant_fds.erase(fd);
|
||||||
|
}
|
||||||
readSocket(*client_map[fd], fd);
|
readSocket(*client_map[fd], fd);
|
||||||
DBOUT << "left redsocket " << ENDL;
|
DBOUT << "left redsocket " << ENDL;
|
||||||
if (client_map[fd]->done)
|
if (client_map[fd]->done)
|
||||||
{
|
delete_client_force(client_map, fd);
|
||||||
delete_client(client_map, fd);
|
|
||||||
std::map<int, t_tmp_fd *>::iterator it = vacant_fds.find(fd);
|
|
||||||
delete_fd(vacant_fds, it, client_map);
|
|
||||||
}
|
|
||||||
else if (client_map[fd]->readyToSend())
|
else if (client_map[fd]->readyToSend())
|
||||||
{
|
{
|
||||||
client_map[fd]->generateRespons(_configs);
|
client_map[fd]->generateRespons(_configs);
|
||||||
|
|
||||||
struct epoll_event ev;
|
epoll_handle(EPOLL_CTL_MOD, fd, EPOLLOUT);
|
||||||
|
|
||||||
ev.events = EPOLLOUT;
|
|
||||||
ev.data.fd = fd;
|
|
||||||
assert( epoll_ctl(_epoll_fd, EPOLL_CTL_MOD, fd, &ev) == 0);
|
|
||||||
DBOUT << WARNING << getDebugTime() << OKCYAN << " rearmed to EPOLLOUT" << ENDL;
|
DBOUT << WARNING << getDebugTime() << OKCYAN << " rearmed to EPOLLOUT" << ENDL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (events & EPOLLOUT)
|
else if (events & EPOLLOUT)
|
||||||
{
|
{
|
||||||
DBOUT << GREEN << "doing sendData" << ENDL;
|
|
||||||
// client_map[fd]->printClientInfo();
|
|
||||||
sendData(*client_map[fd], fd);
|
sendData(*client_map[fd], fd);
|
||||||
if (client_map[fd]->allSended())
|
if (client_map[fd]->allSended())
|
||||||
{
|
{
|
||||||
@@ -365,10 +346,8 @@ void Server::run(void)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
fd_it = vacant_fds.begin();
|
fd_it = vacant_fds.begin();
|
||||||
DBOUT << "entering cleaning fd loop" << ENDL;
|
|
||||||
while (fd_it != vacant_fds.end())
|
while (fd_it != vacant_fds.end())
|
||||||
{
|
{
|
||||||
DBOUT << "loop iteration" <<ENDL;
|
|
||||||
if (TimeToDie(fd_it->second->last_modif, LIFE_TIME))
|
if (TimeToDie(fd_it->second->last_modif, LIFE_TIME))
|
||||||
{
|
{
|
||||||
free(client_map[fd_it->first]);
|
free(client_map[fd_it->first]);
|
||||||
@@ -378,7 +357,6 @@ void Server::run(void)
|
|||||||
++fd_it;
|
++fd_it;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
DBOUT << RED << "end;" << ENDL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//----------------------------------------------Other------------------------------------------------------------------------------------------------
|
//----------------------------------------------Other------------------------------------------------------------------------------------------------
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ class Server
|
|||||||
std::string _ip;
|
std::string _ip;
|
||||||
std::vector<ServerConfig *> _configs;
|
std::vector<ServerConfig *> _configs;
|
||||||
void add_to_epoll_list(int fd, unsigned int ep_events);
|
void add_to_epoll_list(int fd, unsigned int ep_events);
|
||||||
|
void epoll_handle(int op, int fd, int event);
|
||||||
|
|
||||||
static void print_epoll_events(unsigned int events);
|
static void print_epoll_events(unsigned int events);
|
||||||
|
|
||||||
@@ -31,6 +32,7 @@ class Server
|
|||||||
void sendData(Client &client, int fd);
|
void sendData(Client &client, int fd);
|
||||||
void readSocket(Client &client, int fd);
|
void readSocket(Client &client, int fd);
|
||||||
int delete_client(std::map<int,Client *> &map, int fd);
|
int delete_client(std::map<int,Client *> &map, int fd);
|
||||||
|
int delete_client_force(std::map<int,Client *> &map, int fd);
|
||||||
|
|
||||||
|
|
||||||
enum e_req_status
|
enum e_req_status
|
||||||
@@ -48,17 +50,17 @@ class Server
|
|||||||
size_t left;
|
size_t left;
|
||||||
enum e_req_status req_status;
|
enum e_req_status req_status;
|
||||||
} t_client_status;
|
} t_client_status;
|
||||||
typedef struct s_tmp_fd
|
typedef struct s_fd_info
|
||||||
{
|
{
|
||||||
serverListen ip_port;
|
serverListen ip_port;
|
||||||
struct timeval last_modif;
|
struct timeval last_modif;
|
||||||
}t_tmp_fd;
|
} t_fd_info;
|
||||||
bool TimeToDie(struct timeval &last_modif, int lifeTime);
|
bool TimeToDie(struct timeval &last_modif, int lifeTime);
|
||||||
int delete_fd(std::map<int, t_tmp_fd *> &map,
|
int delete_fd(std::map<int, t_fd_info *> &map,
|
||||||
std::map<int, t_tmp_fd *>::iterator &it,
|
std::map<int, t_fd_info *>::iterator &it,
|
||||||
std::map<int, Client *> &client_map);
|
std::map<int, Client *> &client_map);
|
||||||
|
|
||||||
std::map<int, t_tmp_fd *> vacant_fds;
|
std::map<int, t_fd_info *> vacant_fds;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Server();
|
Server();
|
||||||
|
|||||||
Reference in New Issue
Block a user