tmp: multiple server_sockets wip

This commit is contained in:
3lswear
2022-02-08 00:16:14 +03:00
parent 5cfd1b1a9d
commit adf57cd03f
3 changed files with 55 additions and 14 deletions

View File

@@ -1,5 +1,5 @@
[[server]] [[server]]
name = "serv1" name = "pohek1.org"
host = "127.0.0.1" host = "127.0.0.1"
port = 8080 port = 8080
body_size_limit = 10 body_size_limit = 10
@@ -21,7 +21,7 @@
autoindex = false autoindex = false
directory_file = "oops.html" directory_file = "oops.html"
[[server]] [[server]]
name = "2222" name = "pohek2.org"
host = "10.0.0.1" host = "10.0.0.1"
port = 8081 port = 8081
body_size_limit = 10 body_size_limit = 10

View File

@@ -71,6 +71,7 @@ void Server::sendData(Client &client, int fd)
/* DBOUT << YELLO << tmp << ENDL; */ /* DBOUT << YELLO << tmp << ENDL; */
/* DBOUT << GREEN << client.getCounter() << ENDL; */ /* DBOUT << GREEN << client.getCounter() << ENDL; */
DBOUT << "sent " << send_len << " to client " << fd << ENDL;
if (send(fd, tmp + client.getCounter(), send_len, MSG_NOSIGNAL) < 0) if (send(fd, tmp + client.getCounter(), send_len, MSG_NOSIGNAL) < 0)
@@ -149,6 +150,7 @@ void Server::add_to_epoll_list(int fd, unsigned int ep_events)
ev.data.fd = fd; ev.data.fd = fd;
assert(epoll_ctl(_epoll_fd, EPOLL_CTL_ADD, fd, &ev) == 0); assert(epoll_ctl(_epoll_fd, EPOLL_CTL_ADD, fd, &ev) == 0);
/* setNonBlock(fd); */
DBOUT << YELLO DBOUT << YELLO
<< "add socket " << "add socket "
<< fd << fd
@@ -158,8 +160,9 @@ void Server::add_to_epoll_list(int fd, unsigned int ep_events)
void Server::start(void) 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"); */
std::map<int, Client*> client_map; std::map<int, Client*> client_map;
std::map<int, struct serverListen> configurations_map;
int fd; int fd;
int ready_num = 0; int ready_num = 0;
@@ -167,16 +170,54 @@ void Server::start(void)
unsigned int server_events = EPOLLIN; unsigned int server_events = EPOLLIN;
_epoll_fd = epoll_create(1337); _epoll_fd = epoll_create(1337);
checkError(server_sock.init(MAX_CLIENT), "Socket init");
setNonBlock(server_sock.getSocketFd());
for (std::vector<ServerConfig *>::iterator it = _configs.begin();
it != _configs.end(); ++it)
{
ServerConfig *config = *it;
Socket server_sock(AF_INET, SOCK_STREAM, 0, config->getPort(), config->getHost());
if (server_sock.init(MAX_CLIENT) >= 0)
{
struct serverListen listenparams;
listenparams.ip = config->getHost();
listenparams.port = config->getPort();
setNonBlock(server_sock.getSocketFd());
configurations_map[server_sock.getSocketFd()] = listenparams;
DBOUT << YELLO << "adding server_sock..." << ENDL;
add_to_epoll_list(server_sock.getSocketFd(), server_events);
DBOUT << GREEN
<< config->getServerName()
<< " started on "
<< config->getHost()
<< ":"
<< config->getPort()
<< ENDL;
}
else
{
DBOUT << RED
<< config->getServerName()
<< " failed to bind to "
<< config->getHost()
<< ":"
<< config->getPort()
<< ENDL;
}
}
/* checkError(server_sock.init(MAX_CLIENT), "Socket init"); */
/* setNonBlock(server_sock.getSocketFd()); */
setNonBlock(_epoll_fd); setNonBlock(_epoll_fd);
DBOUT << YELLO << "adding server_sock..." << ENDL; /* DBOUT << YELLO << "adding server_sock..." << ENDL; */
add_to_epoll_list(server_sock.getSocketFd(), server_events); /* add_to_epoll_list(server_sock.getSocketFd(), server_events); */
while (1) while (1)
{ {
ready_num = epoll_wait(_epoll_fd, _events, MAX_CLIENT, -1); ready_num = epoll_wait(_epoll_fd, _events, MAX_CLIENT, 5000);
/* DBOUT << TURQ << "ready_num " << ready_num << ENDL; */ /* DBOUT << TURQ << "ready_num " << ready_num << ENDL; */
if (ready_num < 0) if (ready_num < 0)
@@ -189,10 +230,10 @@ void Server::start(void)
/* DBOUT << "FD is " << fd << ENDL; */ /* DBOUT << "FD is " << fd << ENDL; */
/* print_epoll_events(events); */ /* print_epoll_events(events); */
if (fd == server_sock.getSocketFd()) if (configurations_map.find(fd) != configurations_map.end())
{ {
int client_sock = accept(server_sock.getSocketFd(), int client_sock = accept(fd,
server_sock.getSockaddr(), server_sock.getSocklen()); configurations_map[fd].getSockaddr(), server_sock.getSocklen());
if (client_sock > 0) if (client_sock > 0)
add_to_epoll_list(client_sock, client_events); add_to_epoll_list(client_sock, client_events);
else else

View File

@@ -65,7 +65,7 @@ int Socket::bindingSocket(void)
int res; int res;
res = bind(_socketFd, (struct sockaddr *)&_addres, sizeof(_addres)); res = bind(_socketFd, (struct sockaddr *)&_addres, sizeof(_addres));
checkError(res, "Bind Socket"); /* checkError(res, "Bind Socket"); */
return (res); return (res);
} }
@@ -74,7 +74,7 @@ int Socket::listeningSocket(int nbr)
int res; int res;
res = listen(_socketFd, nbr); res = listen(_socketFd, nbr);
checkError(res, "Listen Socket"); /* checkError(res, "Listen Socket"); */
return (res); return (res);
} }
@@ -98,4 +98,4 @@ void Socket::checkError(int fd, std::string str)
} }
else else
std::cout << GREEN << "Socket SUCCESS: " << str << ZERO_C << std::endl; std::cout << GREEN << "Socket SUCCESS: " << str << ZERO_C << std::endl;
} }