Merge remote-tracking branch 'origin/roman' into fara

This commit is contained in:
Talyx
2022-01-30 12:37:19 +03:00
7 changed files with 86 additions and 29 deletions

View File

@@ -13,7 +13,7 @@
#define RESET "\033[0m"
#define HOME "www"
#define BUFFSIZE 1024
#define BUFFSIZE 65536
#define MAX_CLIENT 1000
#define WHITESPACE " \n\r\t\f\v"

View File

@@ -47,6 +47,11 @@ int Client::getFd(void)
return _fd;
}
unsigned int Client::getCounter(void) const
{
return _sended;
}
void Client::setRawData(char *str)
{
this->_buff = str;

View File

@@ -30,6 +30,7 @@ public:
Request getRequest(void);
Response getResponse(void);
std::string getStrToSend(void);
unsigned int getCounter(void) const;
void setRawData(char *);
void setFd(int);
int getFd(void);

View File

@@ -44,6 +44,28 @@ void Server::readConfig(void)
}
}
void Server::sendData(Client &client, int fd)
{
std::string tmp = client.getStrToSend();
unsigned int size_diff = tmp.size() - client.getCounter();
if (size_diff < BUFFSIZE)
{
tmp = tmp.substr(client.getCounter(), size_diff);
}
else
tmp = tmp.substr(client.getCounter(), BUFFSIZE);
/* std::cout << YELLO << tmp << RESET << std::endl; */
/* std::cout << GREEN << client.getCounter() << RESET << std::endl; */
send(fd, tmp.c_str(), tmp.size(), 0);
client.increaseCounter();
}
void Server::setupConfig(void)
{
this->_ip = "127.0.0.1";
@@ -74,8 +96,9 @@ void Server::add_to_epoll_list(int fd, unsigned int ep_events)
void Server::start(void)
{
Socket server_sock(AF_INET, SOCK_STREAM, 0, _port, "127.0.0.1");
char buf[BUFFSIZE] = {0};
char buf[BUFFSIZE + 1] = {0};
std::map<int, Client> client_map;
std::map<int, Client>::iterator client_it;
int fd;
int status;
int ready_num = 0;
@@ -92,7 +115,40 @@ void Server::start(void)
add_to_epoll_list(server_sock.getSocketFd(), server_events);
while (1)
{
ready_num = epoll_wait(_epoll_fd, _events, MAX_CLIENT, -1);
// sending
for (client_it = client_map.begin();
client_it != client_map.end(); ++client_it)
{
std::cout << TURQ << "IN SEND LOOP" << RESET << std::endl;
Client &client = client_it->second;
if (client.readyToSend())
{
epoll_ctl(_epoll_fd, EPOLL_CTL_DEL, client_it->first, NULL);
std::cout << TURQ << "readyToSend " << client_it->first << RESET << std::endl;
client.generateRespons();
sendData(client, client_it->first);
}
if (client.allSended())
{
close(client_it->first);
std::cerr << RED <<
"deleting client "
<< client_it->first
<<
RESET
<< std::endl;
client_map.erase(client_it);
}
}
ready_num = epoll_wait(_epoll_fd, _events, MAX_CLIENT, 100);
/* std::cout << TURQ << "ready_num " << ready_num << RESET << std::endl; */
if (ready_num < 0)
throw std::logic_error("epoll_ret");
for (int i = 0; i < ready_num; i++)
@@ -116,14 +172,9 @@ void Server::start(void)
assert(recv(fd, buf, BUFFSIZE, 0) >= 0);
client_map[fd].setRawData(buf);
status = client_map[fd].parseRequest();
client_map[fd].printClientInfo();
client_map[fd].sendResponse(fd);
client_map[fd].clear();
client_map.erase(fd);
std::cout << BLUE << "status is " << Response::getReasonPhrase(status) << RESET << std::endl;
bzero(buf, BUFFSIZE);
close(fd);
_client--;
/* _client--; */
}
}
ready_num = 0;

View File

@@ -27,6 +27,7 @@ private:
void sendClient(Client head, int);
void sendResponse(Client head, int);
void setNonBlock(int fd);
void sendData(Client &client, int fd);
public:
Server();
Server(std::string path);

View File

@@ -30,7 +30,7 @@ namespace config
void TOMLParser::processMap(void)
{
std::cerr << "Processing map" << std::endl;
/* std::cerr << "Processing map" << std::endl; */
toml_node *map_node;
s_token current;
@@ -49,7 +49,7 @@ namespace config
else
throw std::logic_error("unexpected token in processMap");
std::cout << current.value << std::endl;
/* std::cout << current.value << std::endl; */
std::vector<std::string> full_name;
@@ -61,7 +61,7 @@ namespace config
toml_node *TOMLParser::parseMap(void)
{
std::cerr << "Parsing map" << std::endl;
/* std::cerr << "Parsing map" << std::endl; */
toml_node *node = new toml_node;
TOMLMap *mapObject = new TOMLMap;
bool completed = false;
@@ -89,7 +89,7 @@ namespace config
break;
}
std::string key = nextToken.value;
std::cerr << key << std::endl;
/* std::cerr << key << std::endl; */
if (tokenizer.getToken().type != ASSIGN)
throw std::logic_error("EXPECTED assign!");
nextToken = tokenizer.getToken();
@@ -120,7 +120,7 @@ namespace config
}
case MAPARRAY_DECL:
{
std::cerr << "reached MAPARRAY_DECL in parseMap" << std::endl;
/* std::cerr << "reached MAPARRAY_DECL in parseMap" << std::endl; */
completed = true;
break;
}
@@ -153,7 +153,7 @@ namespace config
void TOMLParser::processMapArray(void)
{
std::cerr << "Parsing MapArray" << std::endl;
/* std::cerr << "Parsing MapArray" << std::endl; */
toml_node *map_node;
s_token current;
@@ -172,7 +172,7 @@ namespace config
else
throw std::logic_error("unexpected token in processMapArray");
std::cout << current.value << std::endl;
/* std::cout << current.value << std::endl; */
std::vector<std::string> full_name;
@@ -188,7 +188,7 @@ namespace config
toml_node *node = new toml_node;
std::string *sValue;
std::cerr << "Parsing string" << std::endl;
/* std::cerr << "Parsing string" << std::endl; */
s_token token = tokenizer.getToken();
sValue = new std::string(token.value);
node->setString(sValue);
@@ -201,7 +201,7 @@ namespace config
toml_node *node = new toml_node;
int value;
std::cerr << "Parsing number" << std::endl;
/* std::cerr << "Parsing number" << std::endl; */
s_token token = tokenizer.getToken();
value = std::atoi(token.value.c_str());
node->setNumber(value);
@@ -211,7 +211,7 @@ namespace config
toml_node *TOMLParser::parseArray(void)
{
std::cerr << "Parsing array" << std::endl;
/* std::cerr << "Parsing array" << std::endl; */
toml_node *node;
toml_node *result = new toml_node;
TOMLArray *array = new TOMLArray;
@@ -280,7 +280,7 @@ namespace config
toml_node *node = new toml_node;
bool value;
std::cerr << "Parsing bool" << std::endl;
/* std::cerr << "Parsing bool" << std::endl; */
s_token token = tokenizer.getToken();
if (token.value == "true")
value = true;
@@ -296,7 +296,7 @@ namespace config
toml_node *TOMLParser::parseNil(void)
{
toml_node *node = new toml_node;
std::cerr << "Parsing NIL" << std::endl;
/* std::cerr << "Parsing NIL" << std::endl; */
node->setNil();
return (node);
}
@@ -304,7 +304,7 @@ namespace config
/* parse tha root ! */
TOMLMap *TOMLParser::parse(void)
{
std::cerr << "Parsing ROOT" << std::endl;
/* std::cerr << "Parsing ROOT" << std::endl; */
root = new TOMLMap;
bool completed = false;
while (!completed)
@@ -327,7 +327,7 @@ namespace config
}
else if (current.type == MAP_DECL)
{
std::cerr << "MAP_DECL value: " << current.value << std::endl;
/* std::cerr << "MAP_DECL value: " << current.value << std::endl; */
tokenizer.set_last(NEWLINE);
tokenizer.rollBackToken();
/* if (tokenizer.getToken().type != NEWLINE) */
@@ -339,7 +339,7 @@ namespace config
else
{
std::string key = current.value;
std::cerr << key << std::endl;
/* std::cerr << key << std::endl; */
if (tokenizer.getToken().type != ASSIGN)
throw std::logic_error("EXPECTED assign!");
current = tokenizer.getToken();
@@ -405,15 +405,14 @@ namespace config
dot = name.find('.');
if (dot == std::string::npos)
break;
/* std::cout << dot << std::endl; */
full_name.push_back(name.substr(0, dot));
name.erase(0, dot + 1);
}
full_name.push_back(name);
for (size_t i = 0; i < full_name.size(); i++)
std::cout << full_name[i] << ", ";
std::cout << std::endl;
/* for (size_t i = 0; i < full_name.size(); i++) */
/* std::cout << full_name[i] << ", "; */
/* std::cout << std::endl; */
return (full_name);
}

View File

@@ -43,6 +43,6 @@ TOMLMap *parse(void)
std::string filename = "config/simple.toml";
config::TOMLParser parser(filename);
TOMLMap *root = parser.parse();
config::display(root);
/* config::display(root); */
return (root);
}