From a3abe505eb1a37a7af0539500337d3b2f7aeff15 Mon Sep 17 00:00:00 2001 From: 3lswear Date: Sun, 30 Jan 2022 13:54:47 +0300 Subject: [PATCH] feat: add readSocket --- src/Server/Server.cpp | 34 +++++++++++++++++++++++++++------- src/Server/Server.hpp | 1 + 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/Server/Server.cpp b/src/Server/Server.cpp index 316c140..3797c84 100644 --- a/src/Server/Server.cpp +++ b/src/Server/Server.cpp @@ -66,6 +66,28 @@ void Server::sendData(Client &client, int fd) } +void Server::readSocket(int fd, std::map &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 client_map; std::map::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; diff --git a/src/Server/Server.hpp b/src/Server/Server.hpp index 4ec4316..e418a9d 100644 --- a/src/Server/Server.hpp +++ b/src/Server/Server.hpp @@ -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 &client_map); public: Server(); Server(std::string path);