From 744c84f544bfa742645f19c270bf038c87954dee Mon Sep 17 00:00:00 2001 From: 3lswear Date: Sat, 29 Jan 2022 23:53:44 +0300 Subject: [PATCH] feat: first working sendData --- src/Server/Server.cpp | 67 ++++++++++++++++++++++++++++++++++++++----- src/Server/Server.hpp | 1 + 2 files changed, 61 insertions(+), 7 deletions(-) diff --git a/src/Server/Server.cpp b/src/Server/Server.cpp index 2bdf729..d91122a 100644 --- a/src/Server/Server.cpp +++ b/src/Server/Server.cpp @@ -44,6 +44,29 @@ void Server::readConfig(void) } } +void Server::sendData(Client &client, int fd) +{ + std::string tmp = client.getStrToSend(); + unsigned int size_diff = tmp.size() - client.getCounter(); + + std::cout << tmp.size() << std::endl; + + 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"; @@ -76,6 +99,7 @@ void Server::start(void) Socket server_sock(AF_INET, SOCK_STREAM, 0, _port, "127.0.0.1"); char buf[BUFFSIZE] = {0}; std::map client_map; + std::map::iterator client_it; int fd; int status; int ready_num = 0; @@ -92,7 +116,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++) @@ -114,16 +171,12 @@ void Server::start(void) { std::cout << TURQ << "IN FOR LOOP" << RESET << std::endl; assert(recv(fd, buf, BUFFSIZE, 0) >= 0); + std::cout << TURQ << "setRawData" << RESET << std::endl; 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; diff --git a/src/Server/Server.hpp b/src/Server/Server.hpp index b88f6a5..4ec4316 100644 --- a/src/Server/Server.hpp +++ b/src/Server/Server.hpp @@ -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);