refacotor(server): clean-ups

This commit is contained in:
3lswear
2022-02-20 16:10:24 +03:00
parent 4ad745a9e9
commit a57ba161ce
3 changed files with 68 additions and 132 deletions

View File

@@ -7,64 +7,19 @@
#define THREAD_NUM 100 #define THREAD_NUM 100
#define MAX_EVENTS #define MAX_EVENTS
//----------------------------------------------Constructors-----------------------------------------------------------------------------------
Server::Server() Server::Server()
{ {
bzero(_events, sizeof(_events)); bzero(_events, sizeof(_events));
} }
Server::Server(std::string path)
{
(void)path;
}
void Server::print_epoll_events(unsigned int events)
{
DBOUT << "Epoll events: ";
if (events & EPOLLIN)
DBOUT << "EPOLLIN ";
if (events & EPOLLOUT)
DBOUT << "EPOLLOUT ";
if (events & EPOLLET)
DBOUT << "EPOLLET ";
DBOUT << ENDL;
}
//----------------------------------------------Send--------------------------------------------------------------------------------------------
//----------------------------------------------Configuration-----------------------------------------------------------------------------------
void Server::readConfig(char *filename) void Server::readConfig(char *filename)
{ {
// TOMLMap *root = NULL;
// root = parse(filename);
config::TOMLParser parser(filename); config::TOMLParser parser(filename);
parser.parse(); parser.parse();
_root = parser.root; _root = parser.root;
DBOUT << RED << "PARSED !!!" << ENDL; DBOUT << RED << "PARSED !!!" << ENDL;
// catch (std::domain_error &e)
// {
// std::cerr << RED << "FATAL: ";
// std::cerr << e.what() << RESET << std::endl;
// // root->clear();
// // config::clean_parsed(parser.root);
// // delete parser.root;
// // exit(-1);
// return;
//
// }
// catch (config::Tokenizer::InvalidToken &e)
// {
// std::cerr << RED << "FATAL: ";
// std::cerr << e.what() << RESET << std::endl;
// config::clean_parsed(parser.root);
// // root->clear();
// // delete parser.root;
// exit(-1);
// }
/* TOMLMap *map; */
TOMLMap::iterator it1; TOMLMap::iterator it1;
TOMLMapArray *arr; TOMLMapArray *arr;
TOMLMapArray::iterator it; TOMLMapArray::iterator it;
@@ -79,12 +34,11 @@ void Server::readConfig(char *filename)
++it; ++it;
} }
DBOUT << RED << "GONNA CLEAN_PARSED" << ENDL; DBOUT << RED << "done processing parsed data" << ENDL;
} }
void Server::sendData(Client &client, int fd) void Server::sendData(Client &client, int fd)
{ {
/* std::string tmp = client.getStrToSend(); */
char *tmp = client.getStrToSend(); char *tmp = client.getStrToSend();
size_t size_diff = client.response_len - client.getCounter(); size_t size_diff = client.response_len - client.getCounter();
size_t send_len; size_t send_len;
@@ -98,14 +52,12 @@ void Server::sendData(Client &client, int fd)
/* DBOUT << GREEN << client.getCounter() << ENDL; */ /* DBOUT << GREEN << client.getCounter() << ENDL; */
DBOUT << "sent " << send_len << " to client " << fd << ENDL; DBOUT << "sent " << send_len << " to client " << fd << ENDL;
if (send(fd, tmp + client.getCounter(), send_len, MSG_NOSIGNAL) < 0) if (send(fd, tmp + client.getCounter(), send_len, MSG_NOSIGNAL) < 0)
{ {
DBOUT << RED << "SEND FAILED !@!!!" << ENDL; DBOUT << RED << "SEND FAILED !@!!!" << ENDL;
client.done = true; client.done = true;
} }
client.increaseCounter(); client.increaseCounter();
} }
void Server::readSocket(Client &client, int fd) void Server::readSocket(Client &client, int fd)
@@ -151,7 +103,7 @@ int Server::delete_client(std::map<int, Client *> &client_map, int fd)
ret = epoll_ctl(_epoll_fd, EPOLL_CTL_DEL, fd, NULL); 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);
DBOUT << RED << DBOUT << RED <<
"deleting client " "deleting client "
@@ -160,54 +112,10 @@ int Server::delete_client(std::map<int, Client *> &client_map, int fd)
return (ret); return (ret);
} }
void Server::setupConfig(void) void Server::setup_server_socks(std::map<int, Socket> &configurations_map)
{ {
this->_ip = "127.0.0.1";
this->_port = 8080;
}
void Server::setNonBlock(int fd)
{
fcntl(fd, F_SETFL, O_NONBLOCK);
}
void Server::add_to_epoll_list(int fd, unsigned int ep_events)
{
struct epoll_event ev;
ev.events = ep_events;
ev.data.fd = fd;
assert(epoll_ctl(_epoll_fd, EPOLL_CTL_ADD, fd, &ev) == 0);
setNonBlock(fd);
DBOUT << YELLO
<< "add socket "
<< fd
<< " to epoll_list"
<< ENDL;
}
void sigHandler(int sig)
{
if (sig == SIGINT)
throw ConfigException("SIGINT called. Server shutdown!");
}
void Server::start(void)
{
/* Socket server_sock(AF_INET, SOCK_STREAM, 0, _port, "127.0.0.1"); */
std::map<int, Client*> client_map;
std::map<int, Socket> configurations_map;
int fd;
int ready_num = 0;
unsigned int client_events = EPOLLIN;
unsigned int server_events = EPOLLIN; unsigned int server_events = EPOLLIN;
std::signal(SIGINT, sigHandler);
_epoll_fd = epoll_create(1337);
for (std::vector<ServerConfig *>::iterator it = _configs.begin(); for (std::vector<ServerConfig *>::iterator it = _configs.begin();
it != _configs.end(); ++it) it != _configs.end(); ++it)
{ {
@@ -221,7 +129,7 @@ void Server::start(void)
DBOUT << YELLO << "adding server_sock..." << ENDL; DBOUT << YELLO << "adding server_sock..." << ENDL;
add_to_epoll_list(server_sock.getSocketFd(), server_events); add_to_epoll_list(server_sock.getSocketFd(), server_events);
DBOUT << GREEN std::cerr << GREEN
<< config->getServerName() << config->getServerName()
<< " started on " << " started on "
<< config->getHost() << config->getHost()
@@ -231,7 +139,7 @@ void Server::start(void)
} }
else else
{ {
DBOUT << RED std::cerr << RED
<< config->getServerName() << config->getServerName()
<< " failed to bind to " << " failed to bind to "
<< config->getHost() << config->getHost()
@@ -239,25 +147,57 @@ void Server::start(void)
<< config->getPort() << config->getPort()
<< ENDL; << ENDL;
} }
} }
/* checkError(server_sock.init(MAX_CLIENT), "Socket init"); */ if (configurations_map.empty())
/* setNonBlock(server_sock.getSocketFd()); */ throw std::domain_error("No servers were set up. Exiting.");
}
void Server::add_to_epoll_list(int fd, unsigned int ep_events)
{
struct epoll_event ev;
ev.events = ep_events;
ev.data.fd = fd;
assert(epoll_ctl(_epoll_fd, EPOLL_CTL_ADD, fd, &ev) == 0);
setNonBlock(fd);
DBOUT << GREEN
<< "add socket "
<< fd
<< " to epoll_list"
<< ENDL;
}
void sigHandler(int sig)
{
if (sig == SIGINT)
throw ConfigException("SIGINT called. Server shutdown!");
}
void Server::run(void)
{
std::map<int, Client*> client_map;
std::map<int, Socket> configurations_map;
unsigned int client_events = EPOLLIN;
std::signal(SIGINT, sigHandler);
_epoll_fd = epoll_create(1337);
setNonBlock(_epoll_fd); setNonBlock(_epoll_fd);
/* DBOUT << YELLO << "adding server_sock..." << ENDL; */ setup_server_socks(configurations_map);
/* add_to_epoll_list(server_sock.getSocketFd(), server_events); */
while (1) while (1)
{ {
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; // DBOUT << TURQ << "ready_num " << ready_num << ENDL;
if (ready_num < 0) if (ready_num < 0)
throw std::logic_error("epoll_ret"); throw std::logic_error("epoll_ret");
for (int i = 0; i < ready_num; i++) for (int i = 0; i < ready_num; i++)
{ {
fd = _events[i].data.fd; int fd = _events[i].data.fd;
unsigned int events = _events[i].events; unsigned int events = _events[i].events;
std::map<int, Socket>::iterator sock_it; std::map<int, Socket>::iterator sock_it;
@@ -267,7 +207,6 @@ void Server::start(void)
if ((events & EPOLLIN) if ((events & EPOLLIN)
&& (sock_it = configurations_map.find(fd)) != configurations_map.end()) && (sock_it = configurations_map.find(fd)) != configurations_map.end())
{ {
/* = configurations_map.find(fd); */
int client_sock = accept(fd, int client_sock = accept(fd,
(sock_it->second).getSockaddr(), (sock_it->second).getSocklen()); (sock_it->second).getSockaddr(), (sock_it->second).getSocklen());
if (client_sock > 0) if (client_sock > 0)
@@ -285,7 +224,9 @@ void Server::start(void)
if (events & EPOLLIN) if (events & EPOLLIN)
{ {
readSocket(*client_map[fd], fd); readSocket(*client_map[fd], fd);
if (client_map[fd]->readyToSend()) if (client_map[fd]->done || client_map[fd]->isEmpty())
delete_client(client_map, fd);
else if (client_map[fd]->readyToSend())
{ {
client_map[fd]->generateRespons(_configs); client_map[fd]->generateRespons(_configs);
@@ -296,13 +237,11 @@ void Server::start(void)
assert( epoll_ctl(_epoll_fd, EPOLL_CTL_MOD, fd, &ev) == 0); assert( epoll_ctl(_epoll_fd, EPOLL_CTL_MOD, fd, &ev) == 0);
DBOUT << GREEN << "rearmed to EPOLLOUT" << ENDL; DBOUT << GREEN << "rearmed to EPOLLOUT" << ENDL;
} }
/* if (client_map[fd]->isEmpty()) */
/* delete_client(client_map, fd); */
} }
else if (events & EPOLLOUT) else if (events & EPOLLOUT)
{ {
/* DBOUT << GREEN << "doing sendData" << ENDL; */ /* DBOUT << GREEN << "doing sendData" << ENDL; */
client_map[fd]->printClientInfo(); // client_map[fd]->printClientInfo();
sendData(*client_map[fd], fd); sendData(*client_map[fd], fd);
if (client_map[fd]->allSended()) if (client_map[fd]->allSended())
{ {
@@ -317,24 +256,24 @@ void Server::start(void)
DBOUT << RED << "end;" << ENDL; DBOUT << RED << "end;" << ENDL;
} }
void Server::end(void)
{
}
//----------------------------------------------Other------------------------------------------------------------------------------------------------ //----------------------------------------------Other------------------------------------------------------------------------------------------------
void Server::checkError(int fd, std::string str)
void Server::setNonBlock(int fd)
{ {
if (fd < 0) fcntl(fd, F_SETFL, O_NONBLOCK);
{
DBOUT << RED << "Server ERROR: " << str << ENDL;
exit(1);
}
else
DBOUT << GREEN << "Server SUCCESS: " << str << ENDL;
} }
void Server::print_epoll_events(unsigned int events)
{
DBOUT << "Epoll events: ";
if (events & EPOLLIN)
DBOUT << "EPOLLIN ";
if (events & EPOLLOUT)
DBOUT << "EPOLLOUT ";
if (events & EPOLLET)
DBOUT << "EPOLLET ";
DBOUT << ENDL;
}
Server::~Server() Server::~Server()
{ {
@@ -357,7 +296,6 @@ Server::~Server()
pri++; pri++;
} }
config::clean_parsed(_root); config::clean_parsed(_root);
} }

View File

@@ -54,12 +54,10 @@ class Server
public: public:
Server(); Server();
Server(std::string path);
void readConfig(char *filename); void readConfig(char *filename);
void setupConfig(void); void setup_server_socks(std::map<int, Socket> &configurations_map);
void start(void); void run(void);
void end(void);
~Server(); ~Server();
}; };

View File

@@ -17,7 +17,7 @@ int main(int argc, char **argv)
server.readConfig(argv[1]); server.readConfig(argv[1]);
else else
server.readConfig(path); server.readConfig(path);
server.start(); server.run();
} }
catch(const ConfigException& e) catch(const ConfigException& e)
{ {