67 lines
1.4 KiB
C++
67 lines
1.4 KiB
C++
|
|
#include <iostream>
|
|
#include <exception>
|
|
#include <stdexcept>
|
|
#include "Webserv.hpp"
|
|
|
|
int main(void)
|
|
{
|
|
try
|
|
{
|
|
Webserv serv;
|
|
|
|
// https://security.stackexchange.com/questions/169213/how-to-chose-a-port-to-run-an-application-on-localhost
|
|
serv.bind(4040);
|
|
serv.listen(512); // 512 max connections arbitrary
|
|
serv.start();
|
|
}
|
|
catch (std::exception& e)
|
|
{
|
|
std::cout << e.what() << '\n';
|
|
}
|
|
return (0);
|
|
}
|
|
|
|
/*
|
|
|
|
|
|
______
|
|
listen_fd = SOCKET() : create a listening socket
|
|
__________
|
|
SETSOCKOPT() : Allow socket descriptor to be reuseable
|
|
_____
|
|
IOCTL() : set listen_fd and all incoming socket to be non-blocking
|
|
____
|
|
BIND(port) : associate listen_fd to a port
|
|
______
|
|
LISTEN(nb_queue) : queue the incoming connections to listen_fd, up to a chosen number
|
|
|
|
fds[1] = listen_fd
|
|
|
|
loop
|
|
. ____
|
|
. POLL(fds[]) :
|
|
.
|
|
. loop through fds[]
|
|
. .
|
|
. . POLLIN && listen_fd ? : readable socket and this is the listening one
|
|
. . . ______
|
|
. . . new_fd = ACCEPT() : extract first connection request in queue of listen_fd
|
|
. . . and creates a new socket that is connected
|
|
. . .
|
|
. . . fds[] += new_fd
|
|
. .
|
|
. . POLLIN ? : readable socket and this is an active one
|
|
. . . ____
|
|
. . . RECV() : read data in socket created by accept()
|
|
. . . ____
|
|
. . . SEND() : write data in socket created by accept()
|
|
|
|
loop through fds[] :
|
|
. _____
|
|
. CLOSE(fds[])
|
|
|
|
|
|
*/
|
|
|