change to poll() call

This commit is contained in:
LuckyLaszlo
2022-07-12 19:37:47 +02:00
parent a9b1843eba
commit eb5e719750
2 changed files with 12 additions and 15 deletions

View File

@@ -2,7 +2,7 @@ NAME = webserv
CXX = c++ CXX = c++
CXXFLAGS = -Wall -Wextra -Werror CXXFLAGS = -Wall -Wextra #-Werror
CXXFLAGS += -std=c++98 CXXFLAGS += -std=c++98
CXXFLAGS += -I$(HEADERS_D) CXXFLAGS += -I$(HEADERS_D)
CXXFLAGS += -g CXXFLAGS += -g

View File

@@ -4,7 +4,8 @@
Webserv::Webserv() Webserv::Webserv()
{ {
std::cout << "Server init\n"; std::cout << "Server init\n";
_socket_fd = ::socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0); // _socket_fd = ::socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
_socket_fd = ::socket(AF_INET, SOCK_STREAM, 0);
if (_socket_fd == -1) if (_socket_fd == -1)
{ {
::perror("err socket(): "); ::perror("err socket(): ");
@@ -38,7 +39,7 @@ void Webserv::bind(in_port_t port)
struct sockaddr_in addr; struct sockaddr_in addr;
addr.sin_family = AF_INET; addr.sin_family = AF_INET;
addr.sin_port = ::htons(port); addr.sin_port = ::htons(port);
addr.sin_addr.s_addr = ::htonl(INADDR_ANY); // htonl useless with 0 value (INADDR_ANY) addr.sin_addr.s_addr = ::htonl(INADDR_ANY); // htonl useless with 0 value (INADDR_ANY) ?
if (::bind(_socket_fd, (const sockaddr*)&addr, sizeof addr) == -1) if (::bind(_socket_fd, (const sockaddr*)&addr, sizeof addr) == -1)
{ {
@@ -65,12 +66,7 @@ void Webserv::start()
struct sockaddr_in addr; struct sockaddr_in addr;
socklen_t addr_len; socklen_t addr_len;
int accepted_fd; int accepted_fd;
// poll (or equivalent)
struct pollfd poll_s; struct pollfd poll_s;
poll_s.fd = _socket_fd;
poll_s.events = POLLIN;
char buf[BUFSIZE]; // WIP buffer. need to try with std::vector or std::string. char buf[BUFSIZE]; // WIP buffer. need to try with std::vector or std::string.
int ret; int ret;
@@ -78,9 +74,6 @@ void Webserv::start()
while (1) while (1)
{ {
std::cout << "----------\n"; std::cout << "----------\n";
std::cout << "poll()\n"; // poll (or equivalent)
::poll(&poll_s, 1, -1);
std::cout << "accept()\n"; std::cout << "accept()\n";
addr_len = sizeof addr; addr_len = sizeof addr;
accepted_fd = ::accept(_socket_fd, (sockaddr*)&addr, &addr_len); accepted_fd = ::accept(_socket_fd, (sockaddr*)&addr, &addr_len);
@@ -89,11 +82,15 @@ void Webserv::start()
::perror("err accept(): "); ::perror("err accept(): ");
continue; continue;
} }
// "Your server must never block and the client can be bounced properly if necessary". // "Your server must never block and the client can be bounced properly if necessary".
// NO-Block OK, but how to handle it ? Miss the bouncing part. // NO-Block OK, but how to handle it ? Miss the bouncing part.
::fcntl(accepted_fd, F_SETFL, O_NONBLOCK); ::fcntl(accepted_fd, F_SETFL, O_NONBLOCK);
std::cout << "poll()\n"; // poll (or equivalent)
poll_s.fd = accepted_fd;
poll_s.events = POLLIN; // We need a way to valid POLLOUT and POLLIN at the same time (both, not one of them)
::poll(&poll_s, 1, -1);
std::cout << "recv()\n"; std::cout << "recv()\n";
ret = ::recv(accepted_fd, buf, BUFSIZE, 0); ret = ::recv(accepted_fd, buf, BUFSIZE, 0);
if (ret == -1) if (ret == -1)