mirror of
https://github.com/3lswear/webserv.git
synced 2025-10-29 13:27:59 +03:00
refactor: some cleanups
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
#include "Server.hpp"
|
#include "Server.hpp"
|
||||||
#include <exception>
|
#include <exception>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
#include <cstdio>
|
||||||
|
#include <map>
|
||||||
|
|
||||||
#define THREAD_NUM 100
|
#define THREAD_NUM 100
|
||||||
#define MAX_EVENTS
|
#define MAX_EVENTS
|
||||||
@@ -76,6 +78,11 @@ void Server::add_to_epoll_list(int fd)
|
|||||||
|
|
||||||
epoll_ctl(_epoll_fd, EPOLL_CTL_ADD, fd, &ev);
|
epoll_ctl(_epoll_fd, EPOLL_CTL_ADD, fd, &ev);
|
||||||
_client++;
|
_client++;
|
||||||
|
std::cerr << RED
|
||||||
|
<< "add client_sock "
|
||||||
|
<< fd
|
||||||
|
<< " to epoll_list"
|
||||||
|
<< RESET << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* void Server::add_to_epoll_list(int fd, unsigned int ep_events) */
|
/* void Server::add_to_epoll_list(int fd, unsigned int ep_events) */
|
||||||
@@ -94,14 +101,13 @@ 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 + 1] = {0};
|
||||||
Header header[MAX_CLIENT];
|
Header header[MAX_CLIENT];
|
||||||
|
std::map<int, Header> header_map;
|
||||||
int fd;
|
int fd;
|
||||||
/* int n; */
|
/* int n; */
|
||||||
/* int nfds; */
|
/* int nfds; */
|
||||||
int client_sock;
|
int client_sock;
|
||||||
int status;
|
int status;
|
||||||
int epoll_ret = 0;
|
int epoll_ret = 0;
|
||||||
/* int epollfd; */
|
|
||||||
|
|
||||||
|
|
||||||
/* struct epoll_event ev; */
|
/* struct epoll_event ev; */
|
||||||
|
|
||||||
@@ -116,6 +122,7 @@ void Server::start(void)
|
|||||||
setNonBlock(_epoll_fd);
|
setNonBlock(_epoll_fd);
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
|
/* std::cout << RED << "IN MAIN LOOP" << RESET << std::endl; */
|
||||||
client_sock = accept(server_sock.getSocketFd(),
|
client_sock = accept(server_sock.getSocketFd(),
|
||||||
server_sock.getSockaddr(), server_sock.getSocklen());
|
server_sock.getSockaddr(), server_sock.getSocklen());
|
||||||
if (client_sock > 0)
|
if (client_sock > 0)
|
||||||
@@ -123,20 +130,25 @@ void Server::start(void)
|
|||||||
if (_client > 0)
|
if (_client > 0)
|
||||||
epoll_ret = epoll_wait(_epoll_fd, _events, MAX_CLIENT, -1);
|
epoll_ret = epoll_wait(_epoll_fd, _events, MAX_CLIENT, -1);
|
||||||
if (epoll_ret < 0)
|
if (epoll_ret < 0)
|
||||||
|
{
|
||||||
|
perror("epoll_ret");
|
||||||
throw std::logic_error("epoll_ret");
|
throw std::logic_error("epoll_ret");
|
||||||
|
}
|
||||||
for (int i = 0; i < epoll_ret; i++)
|
for (int i = 0; i < epoll_ret; i++)
|
||||||
{
|
{
|
||||||
/* if (_events[i].events == 0) */
|
/* if (_events[i].events == 0) */
|
||||||
/* continue; */
|
/* continue; */
|
||||||
|
|
||||||
|
std::cout << TURQ << "IN FOR LOOP" << RESET << std::endl;
|
||||||
fd = _events[i].data.fd;
|
fd = _events[i].data.fd;
|
||||||
checkError(recv(fd, buf, BUFFSIZE, 0), "Receive msg from client");
|
assert(recv(fd, buf, BUFFSIZE, 0) >= 0);
|
||||||
header[fd].setRawData(buf);
|
header[fd].setRawData(buf);
|
||||||
status = header[fd].parseRequest();
|
status = header[fd].parseRequest();
|
||||||
header[fd].printInfo();
|
header[fd].printInfo();
|
||||||
header[fd].printInfo();
|
|
||||||
header[fd].sendResponse(fd);
|
header[fd].sendResponse(fd);
|
||||||
header[fd].clear();
|
header[fd].clear();
|
||||||
std::cout << BLUE << header[fd].getReasonPhrase(status) << ZERO_C << std::endl;
|
std::cout << BLUE << "status is " << Header::getReasonPhrase(status) << RESET << std::endl;
|
||||||
|
bzero(buf, BUFFSIZE);
|
||||||
close(fd);
|
close(fd);
|
||||||
_client--;
|
_client--;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user