From 282224fd127f780e446691ddf0c1d869478f08f3 Mon Sep 17 00:00:00 2001 From: 3lswear Date: Sat, 5 Feb 2022 01:53:42 +0300 Subject: [PATCH] feat: refactor for consistency, some additions --- src/Server/Server.cpp | 43 ++++++++++++++++++-------- src/Server/Server.hpp | 70 +++++++++++++++++++++++++++---------------- 2 files changed, 75 insertions(+), 38 deletions(-) diff --git a/src/Server/Server.cpp b/src/Server/Server.cpp index d314b23..3c3e7d8 100644 --- a/src/Server/Server.cpp +++ b/src/Server/Server.cpp @@ -21,6 +21,18 @@ Server::Server(std::string path) _client = 0; } +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----------------------------------------------------------------------------------- @@ -67,7 +79,7 @@ void Server::sendData(Client &client, int fd) } -void Server::readSocket(int fd, std::map &client_map) +void Server::readSocket(Client &client, int fd) { int status; @@ -78,22 +90,22 @@ void Server::readSocket(int fd, std::map &client_map) bytes_read = recv(fd, buf, BUFFSIZE, 0); if (bytes_read == 0) { - client_map[fd].allRead = true; + client.allRead = true; return; } - client_map[fd].setRawData(buf); - client_map[fd].increaseRecvCounter(bytes_read); - status = client_map[fd].parseRequest(); + client.setRawData(buf); + client.increaseRecvCounter(bytes_read); + status = client.parseRequest(); // client_map[fd].printClientInfo(); - if ((bytes_read < BUFFSIZE) && client_map[fd].allRecved()) + if ((bytes_read < BUFFSIZE) && client.allRecved()) { - client_map[fd].allRead = true; + client.allRead = true; } - std::cerr << "recvCounter " << client_map[fd].getRecvCounter() << std::endl; - std::cerr << "contentLength " << client_map[fd].getRequest().getContentLength() << std::endl; - std::cerr << "allRead " << client_map[fd].allRead << std::endl; + std::cerr << "recvCounter " << client.getRecvCounter() << std::endl; + std::cerr << "contentLength " << client.getRequest().getContentLength() << std::endl; + std::cerr << "allRead " << client.allRead << std::endl; std::cout << BLUE << "status is " << Response::getReasonPhrase(status) << RESET << std::endl; bzero(buf, BUFFSIZE); @@ -156,7 +168,7 @@ void Server::start(void) if (!client.allRead && !client.isEmpty()) { - readSocket(client_it->first, client_map); + readSocket(client, fd); } if (client.readyToSend()) { @@ -170,7 +182,8 @@ void Server::start(void) if ((client.readyToSend() && client.allSended()) || client.isEmpty()) { - client_map[fd].printClientInfo(); + /* SUS */ + /* client_map[fd].printClientInfo(); */ close(client_it->first); std::cerr << RED << "deleting client " @@ -191,6 +204,9 @@ void Server::start(void) { fd = _events[i].data.fd; + DBOUT << "FD is " << fd << ENDL; + print_epoll_events(_events[i].events); + if (fd == server_sock.getSocketFd()) { int client_sock = accept(server_sock.getSocketFd(), @@ -204,7 +220,8 @@ void Server::start(void) { std::cout << TURQ << "IN FOR LOOP" << RESET << std::endl; /* _client--; */ - readSocket(fd, client_map); + client_map[fd]; + readSocket(client_map[fd], fd); } } ready_num = 0; diff --git a/src/Server/Server.hpp b/src/Server/Server.hpp index e418a9d..0c2ebc3 100644 --- a/src/Server/Server.hpp +++ b/src/Server/Server.hpp @@ -7,37 +7,57 @@ #include "Socket.hpp" #include "parse.hpp" + class Client; class Server { -private: - int _port; - int _epoll_fd; - int _client; - struct epoll_event _events[MAX_CLIENT]; - struct sockaddr_in _addres; - std::string _ip; - std::vector _configs; - void add_to_epoll_list(int fd, unsigned int ep_events); + private: + int _port; + int _epoll_fd; + int _client; + struct epoll_event _events[MAX_CLIENT]; + struct sockaddr_in _addres; + std::string _ip; + std::vector _configs; + void add_to_epoll_list(int fd, unsigned int ep_events); -private: - void checkError(int fd, std::string str); - void sendFile(std::string str); - void sendClient(Client head, int); - 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); + static void print_epoll_events(unsigned int events); - void readConfig(void); - void setupConfig(void); - void start(void); - void end(void); - ~Server(); + private: + void checkError(int fd, std::string str); + void sendFile(std::string str); + void sendClient(Client head, int); + void sendResponse(Client head, int); + void setNonBlock(int fd); + void sendData(Client &client, int fd); + void readSocket(Client &client, int fd); + + enum e_req_status + { + READING, + WRITING, + ENDED + }; + + typedef struct s_client_status + { + int fd; + int serverfd; + size_t readn; + size_t left; + enum e_req_status req_status; + } t_client_status; + + public: + Server(); + Server(std::string path); + + void readConfig(void); + void setupConfig(void); + void start(void); + void end(void); + ~Server(); };