mirror of
https://github.com/3lswear/webserv.git
synced 2025-10-29 13:27:59 +03:00
feat: add server_socket to epoll
This commit is contained in:
@@ -64,8 +64,8 @@ void Server::add_to_epoll_list(int fd, unsigned int ep_events)
|
|||||||
|
|
||||||
assert(epoll_ctl(_epoll_fd, EPOLL_CTL_ADD, fd, &ev) == 0);
|
assert(epoll_ctl(_epoll_fd, EPOLL_CTL_ADD, fd, &ev) == 0);
|
||||||
_client++;
|
_client++;
|
||||||
std::cerr << RED
|
std::cerr << YELLO
|
||||||
<< "add client_sock "
|
<< "add socket "
|
||||||
<< fd
|
<< fd
|
||||||
<< " to epoll_list"
|
<< " to epoll_list"
|
||||||
<< RESET << std::endl;
|
<< RESET << std::endl;
|
||||||
@@ -74,50 +74,58 @@ void Server::add_to_epoll_list(int fd, unsigned int ep_events)
|
|||||||
void Server::start(void)
|
void Server::start(void)
|
||||||
{
|
{
|
||||||
Socket server_sock(AF_INET, SOCK_STREAM, 0, _port, "127.0.0.1");
|
Socket server_sock(AF_INET, SOCK_STREAM, 0, _port, "127.0.0.1");
|
||||||
char buf[BUFFSIZE + 1] = {0};
|
char buf[BUFFSIZE] = {0};
|
||||||
std::map<int, Client> Client_map;
|
std::map<int, Client> client_map;
|
||||||
int fd;
|
int fd;
|
||||||
int client_sock;
|
|
||||||
int status;
|
int status;
|
||||||
int ready_num = 0;
|
int ready_num = 0;
|
||||||
|
|
||||||
unsigned int client_events = EPOLLIN | EPOLLOUT | EPOLLET;
|
unsigned int client_events = EPOLLIN | EPOLLOUT | EPOLLET;
|
||||||
|
unsigned int server_events = EPOLLIN | EPOLLOUT | EPOLLET;
|
||||||
|
|
||||||
_epoll_fd = epoll_create1(0);
|
_epoll_fd = epoll_create(1337);
|
||||||
checkError(server_sock.init(MAX_CLIENT), "Socket init");
|
checkError(server_sock.init(MAX_CLIENT), "Socket init");
|
||||||
setNonBlock(server_sock.getSocketFd());
|
setNonBlock(server_sock.getSocketFd());
|
||||||
setNonBlock(_epoll_fd);
|
setNonBlock(_epoll_fd);
|
||||||
|
|
||||||
|
std::cerr << YELLO << "adding server_sock..." << RESET << std::endl;
|
||||||
|
add_to_epoll_list(server_sock.getSocketFd(), server_events);
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
client_sock = accept(server_sock.getSocketFd(),
|
|
||||||
server_sock.getSockaddr(), server_sock.getSocklen());
|
|
||||||
if (client_sock > 0)
|
|
||||||
add_to_epoll_list(client_sock, client_events);
|
|
||||||
if (_client > 0)
|
|
||||||
ready_num = epoll_wait(_epoll_fd, _events, MAX_CLIENT, -1);
|
ready_num = epoll_wait(_epoll_fd, _events, MAX_CLIENT, -1);
|
||||||
/* std::cout << GREEN << "after epoll_wait" << RESET << std::endl; */
|
|
||||||
if (ready_num < 0)
|
if (ready_num < 0)
|
||||||
{
|
|
||||||
perror("epoll_ret");
|
|
||||||
throw std::logic_error("epoll_ret");
|
throw std::logic_error("epoll_ret");
|
||||||
}
|
|
||||||
for (int i = 0; i < ready_num; i++)
|
for (int i = 0; i < ready_num; i++)
|
||||||
{
|
{
|
||||||
/* if (_events[i].events == 0) */
|
/* if (_events[i].events == 0) */
|
||||||
/* continue; */
|
/* continue; */
|
||||||
fd = _events[i].data.fd;
|
fd = _events[i].data.fd;
|
||||||
|
|
||||||
|
if (fd == server_sock.getSocketFd())
|
||||||
|
{
|
||||||
|
int client_sock = accept(server_sock.getSocketFd(),
|
||||||
|
server_sock.getSockaddr(), server_sock.getSocklen());
|
||||||
|
if (client_sock > 0)
|
||||||
|
add_to_epoll_list(client_sock, client_events);
|
||||||
|
else
|
||||||
|
throw std::logic_error("accept didnt work");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
std::cout << TURQ << "IN FOR LOOP" << RESET << std::endl;
|
std::cout << TURQ << "IN FOR LOOP" << RESET << std::endl;
|
||||||
assert(recv(fd, buf, BUFFSIZE, 0) >= 0);
|
assert(recv(fd, buf, BUFFSIZE, 0) >= 0);
|
||||||
Client_map[fd].setRawData(buf);
|
client_map[fd].setRawData(buf);
|
||||||
status = Client_map[fd].parseRequest();
|
status = client_map[fd].parseRequest();
|
||||||
Client_map[fd].printClientInfo();
|
client_map[fd].printClientInfo();
|
||||||
Client_map[fd].sendResponse(fd);
|
client_map[fd].sendResponse(fd);
|
||||||
Client_map[fd].clear();
|
client_map[fd].clear();
|
||||||
|
client_map.erase(fd);
|
||||||
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);
|
||||||
close(fd);
|
close(fd);
|
||||||
_client--;
|
_client--;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
ready_num = 0;
|
ready_num = 0;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user