feat: add readSocket

This commit is contained in:
3lswear
2022-01-30 13:54:47 +03:00
parent 307e0abeac
commit a3abe505eb
2 changed files with 28 additions and 7 deletions

View File

@@ -66,6 +66,28 @@ void Server::sendData(Client &client, int fd)
}
void Server::readSocket(int fd, std::map<int, Client> &client_map)
{
int status;
int bytes_read;
char buf[BUFFSIZE + 1] = {0};
bytes_read = recv(fd, buf, BUFFSIZE, 0);
if (bytes_read < BUFFSIZE)
{
client_map[fd].allRead = true;
if (bytes_read == 0)
return;
}
client_map[fd].setRawData(buf);
status = client_map[fd].parseRequest();
client_map[fd].printClientInfo();
std::cout << BLUE << "status is " << Response::getReasonPhrase(status) << RESET << std::endl;
bzero(buf, BUFFSIZE);
}
void Server::setupConfig(void)
{
this->_ip = "127.0.0.1";
@@ -96,11 +118,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 + 1] = {0};
std::map<int, Client> client_map;
std::map<int, Client>::iterator client_it;
int fd;
int status;
int ready_num = 0;
unsigned int client_events = EPOLLIN | EPOLLOUT | EPOLLET;
@@ -123,6 +143,10 @@ void Server::start(void)
std::cout << TURQ << "IN SEND LOOP" << RESET << std::endl;
Client &client = client_it->second;
if (!client.allRead)
{
readSocket(client_it->first, client_map);
}
if (client.readyToSend())
{
epoll_ctl(_epoll_fd, EPOLL_CTL_DEL, client_it->first, NULL);
@@ -169,12 +193,8 @@ void Server::start(void)
else
{
std::cout << TURQ << "IN FOR LOOP" << RESET << std::endl;
assert(recv(fd, buf, BUFFSIZE, 0) >= 0);
client_map[fd].setRawData(buf);
status = client_map[fd].parseRequest();
std::cout << BLUE << "status is " << Response::getReasonPhrase(status) << RESET << std::endl;
bzero(buf, BUFFSIZE);
/* _client--; */
readSocket(fd, client_map);
}
}
ready_num = 0;

View File

@@ -28,6 +28,7 @@ private:
void sendResponse(Client head, int);
void setNonBlock(int fd);
void sendData(Client &client, int fd);
void readSocket(int fd, std::map<int, Client> &client_map);
public:
Server();
Server(std::string path);