change to poll() call
This commit is contained in:
2
Makefile
2
Makefile
@@ -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
|
||||||
|
|||||||
@@ -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,22 +66,14 @@ 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;
|
||||||
|
struct pollfd poll_s;
|
||||||
// poll (or equivalent)
|
char buf[BUFSIZE]; // WIP buffer. need to try with std::vector or std::string.
|
||||||
struct pollfd poll_s;
|
int ret;
|
||||||
poll_s.fd = _socket_fd;
|
|
||||||
poll_s.events = POLLIN;
|
|
||||||
|
|
||||||
char buf[BUFSIZE]; // WIP buffer. need to try with std::vector or std::string.
|
|
||||||
int ret;
|
|
||||||
|
|
||||||
std::cout << "Server started\n";
|
std::cout << "Server started\n";
|
||||||
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)
|
||||||
|
|||||||
Reference in New Issue
Block a user