mirror of
https://github.com/3lswear/webserv.git
synced 2025-10-29 13:27:59 +03:00
feat: refactor for consistency, some additions
This commit is contained in:
@@ -21,6 +21,18 @@ Server::Server(std::string path)
|
|||||||
_client = 0;
|
_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--------------------------------------------------------------------------------------------
|
//----------------------------------------------Send--------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
//----------------------------------------------Configuration-----------------------------------------------------------------------------------
|
//----------------------------------------------Configuration-----------------------------------------------------------------------------------
|
||||||
@@ -67,7 +79,7 @@ void Server::sendData(Client &client, int fd)
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Server::readSocket(int fd, std::map<int, Client> &client_map)
|
void Server::readSocket(Client &client, int fd)
|
||||||
{
|
{
|
||||||
|
|
||||||
int status;
|
int status;
|
||||||
@@ -78,22 +90,22 @@ void Server::readSocket(int fd, std::map<int, Client> &client_map)
|
|||||||
bytes_read = recv(fd, buf, BUFFSIZE, 0);
|
bytes_read = recv(fd, buf, BUFFSIZE, 0);
|
||||||
if (bytes_read == 0)
|
if (bytes_read == 0)
|
||||||
{
|
{
|
||||||
client_map[fd].allRead = true;
|
client.allRead = true;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
client_map[fd].setRawData(buf);
|
client.setRawData(buf);
|
||||||
client_map[fd].increaseRecvCounter(bytes_read);
|
client.increaseRecvCounter(bytes_read);
|
||||||
status = client_map[fd].parseRequest();
|
status = client.parseRequest();
|
||||||
// client_map[fd].printClientInfo();
|
// 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 << "recvCounter " << client.getRecvCounter() << std::endl;
|
||||||
std::cerr << "contentLength " << client_map[fd].getRequest().getContentLength() << std::endl;
|
std::cerr << "contentLength " << client.getRequest().getContentLength() << std::endl;
|
||||||
std::cerr << "allRead " << client_map[fd].allRead << std::endl;
|
std::cerr << "allRead " << client.allRead << std::endl;
|
||||||
|
|
||||||
std::cout << BLUE << "status is " << Response::getReasonPhrase(status) << RESET << std::endl;
|
std::cout << BLUE << "status is " << Response::getReasonPhrase(status) << RESET << std::endl;
|
||||||
bzero(buf, BUFFSIZE);
|
bzero(buf, BUFFSIZE);
|
||||||
@@ -156,7 +168,7 @@ void Server::start(void)
|
|||||||
|
|
||||||
if (!client.allRead && !client.isEmpty())
|
if (!client.allRead && !client.isEmpty())
|
||||||
{
|
{
|
||||||
readSocket(client_it->first, client_map);
|
readSocket(client, fd);
|
||||||
}
|
}
|
||||||
if (client.readyToSend())
|
if (client.readyToSend())
|
||||||
{
|
{
|
||||||
@@ -170,7 +182,8 @@ void Server::start(void)
|
|||||||
|
|
||||||
if ((client.readyToSend() && client.allSended()) || client.isEmpty())
|
if ((client.readyToSend() && client.allSended()) || client.isEmpty())
|
||||||
{
|
{
|
||||||
client_map[fd].printClientInfo();
|
/* SUS */
|
||||||
|
/* client_map[fd].printClientInfo(); */
|
||||||
close(client_it->first);
|
close(client_it->first);
|
||||||
std::cerr << RED <<
|
std::cerr << RED <<
|
||||||
"deleting client "
|
"deleting client "
|
||||||
@@ -191,6 +204,9 @@ void Server::start(void)
|
|||||||
{
|
{
|
||||||
fd = _events[i].data.fd;
|
fd = _events[i].data.fd;
|
||||||
|
|
||||||
|
DBOUT << "FD is " << fd << ENDL;
|
||||||
|
print_epoll_events(_events[i].events);
|
||||||
|
|
||||||
if (fd == server_sock.getSocketFd())
|
if (fd == server_sock.getSocketFd())
|
||||||
{
|
{
|
||||||
int client_sock = accept(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;
|
std::cout << TURQ << "IN FOR LOOP" << RESET << std::endl;
|
||||||
/* _client--; */
|
/* _client--; */
|
||||||
readSocket(fd, client_map);
|
client_map[fd];
|
||||||
|
readSocket(client_map[fd], fd);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ready_num = 0;
|
ready_num = 0;
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
#include "Socket.hpp"
|
#include "Socket.hpp"
|
||||||
#include "parse.hpp"
|
#include "parse.hpp"
|
||||||
|
|
||||||
|
|
||||||
class Client;
|
class Client;
|
||||||
|
|
||||||
class Server
|
class Server
|
||||||
@@ -21,6 +22,8 @@ private:
|
|||||||
std::vector<ServerConfig *> _configs;
|
std::vector<ServerConfig *> _configs;
|
||||||
void add_to_epoll_list(int fd, unsigned int ep_events);
|
void add_to_epoll_list(int fd, unsigned int ep_events);
|
||||||
|
|
||||||
|
static void print_epoll_events(unsigned int events);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void checkError(int fd, std::string str);
|
void checkError(int fd, std::string str);
|
||||||
void sendFile(std::string str);
|
void sendFile(std::string str);
|
||||||
@@ -28,7 +31,24 @@ private:
|
|||||||
void sendResponse(Client head, int);
|
void sendResponse(Client head, int);
|
||||||
void setNonBlock(int fd);
|
void setNonBlock(int fd);
|
||||||
void sendData(Client &client, int fd);
|
void sendData(Client &client, int fd);
|
||||||
void readSocket(int fd, std::map<int, Client> &client_map);
|
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:
|
public:
|
||||||
Server();
|
Server();
|
||||||
Server(std::string path);
|
Server(std::string path);
|
||||||
|
|||||||
Reference in New Issue
Block a user