wip with both my and luke code

This commit is contained in:
hugogogo
2022-07-13 17:31:18 +02:00
parent 461c10b6a2
commit 6b1fd5a15b
5 changed files with 99 additions and 78 deletions

View File

@@ -8,24 +8,20 @@ Webserv::Webserv()
if (_socket_fd == -1)
{
::perror("err socket(): ");
throw std::runtime_error("Socket init");
// throw std::runtime_error("Socket init");
throw WebservError();
}
}
/* Webserv::Webserv(Webserv const &src)
{
} */
Webserv::~Webserv()
{
std::cout << "Server destroyed\n";
}
/* Webserv & Webserv::operator=(Webserv const &rhs)
{
} */
const char * Webserv::WebservError::what() const throw() {
return ("error");
}
///////////////
// Functions //
@@ -43,7 +39,7 @@ void Webserv::bind(in_port_t port)
if (::bind(_socket_fd, (const sockaddr*)&addr, sizeof addr) == -1)
{
::perror("err bind(): ");
throw std::runtime_error("Socket bind");
// throw std::runtime_error("Socket bind");
}
}
@@ -52,7 +48,7 @@ void Webserv::listen(unsigned int max_connections)
if (::listen(_socket_fd, max_connections) == -1)
{
::perror("err listen(): ");
throw std::runtime_error("Socket listen");
// throw std::runtime_error("Socket listen");
}
}
@@ -67,55 +63,56 @@ void Webserv::start()
int accepted_fd;
// poll (or equivalent)
struct pollfd poll_s;
poll_s.fd = _socket_fd;
poll_s.events = POLLIN;
// 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.
int ret;
// char buf[BUFSIZE]; // WIP buffer. need to try with std::vector or std::string.
// int ret;
std::cout << "Server started\n";
while (1)
{
std::cout << "----------\n";
std::cout << "poll()\n"; // poll (or equivalent)
::poll(&poll_s, 1, -1);
// std::cout << "----------\n";
// std::cout << "poll()\n"; // poll (or equivalent)
// ::poll(&poll_s, 1, -1);
std::cout << "accept()\n";
addr_len = sizeof addr;
accepted_fd = ::accept(_socket_fd, (sockaddr*)&addr, &addr_len);
if (accepted_fd == -1)
{
::perror("err accept(): ");
//throw WebservError();
continue;
}
std::cout << "accept()\n";
// "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.
::fcntl(accepted_fd, F_SETFL, O_NONBLOCK);
std::cout << "recv()\n";
ret = ::recv(accepted_fd, buf, BUFSIZE, 0);
if (ret == -1)
{
::perror("err recv(): ");
if (::send(accepted_fd, MSG_BOUNCE, sizeof MSG_BOUNCE - 1, 0) == -1)
::perror("err send(): ");
::close(accepted_fd);
continue;
}
/*
if (ret == BUFSIZE)
// send error like "request too long" to client
*/
buf[ret] = '\0';
std::cout << "send()\n";
if (::send(accepted_fd, buf, ret, 0) == -1) // echo the read
::perror("err send(): ");
if (::send(accepted_fd, MSG_TEST, sizeof MSG_TEST - 1, 0) == -1)
::perror("err send(): ");
::close(accepted_fd);
// // "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.
// ::fcntl(accepted_fd, F_SETFL, O_NONBLOCK);
//
// std::cout << "recv()\n";
// ret = ::recv(accepted_fd, buf, BUFSIZE, 0);
// if (ret == -1)
// {
// ::perror("err recv(): ");
// if (::send(accepted_fd, MSG_BOUNCE, sizeof MSG_BOUNCE - 1, 0) == -1)
// ::perror("err send(): ");
// ::close(accepted_fd);
// continue;
// }
// /*
// if (ret == BUFSIZE)
// // send error like "request too long" to client
// */
// buf[ret] = '\0';
//
// std::cout << "send()\n";
// if (::send(accepted_fd, buf, ret, 0) == -1) // echo the read
// ::perror("err send(): ");
// if (::send(accepted_fd, MSG_TEST, sizeof MSG_TEST - 1, 0) == -1)
// ::perror("err send(): ");
//
// ::close(accepted_fd);
}
}