implemented primary event loop for server operations

This commit is contained in:
bettercallous
2024-04-08 20:17:05 +00:00
parent 45567b98b9
commit c315be1dcd
3 changed files with 21 additions and 2 deletions

View File

@@ -39,6 +39,25 @@ void Server::init() {
std::cout << "Waiting for connections..." << std::endl;
}
void Server::run() {
while (!_signal) {
int ret = poll(&_fds[0], _fds.size(), -1);
if (ret == -1)
throw std::runtime_error("Error: poll() failed");
for (size_t i = 0; i < _fds.size(); ++i) {
if (_fds[i].revents & POLLIN) {
if (_fds[i].fd == _serverSocketFd)
// handle new client connection here
continue;
else
// handle existing client incoming data here
continue;
}
}
}
}
void Server::createServerSocket() {
_serverSocketFd = socket(AF_INET, SOCK_STREAM, 0);
if (_serverSocketFd == -1)
@@ -57,7 +76,6 @@ void Server::createServerSocket() {
throw std::runtime_error("Error: listen() failed");
addPollfd(_serverSocketFd, POLLIN, 0);
}
void Server::bindServerSocket() {

View File

@@ -23,11 +23,11 @@ class Server {
void parseArgs(int ac, char **av);
static void receiveSignal(int signum);
void init();
void run();
void createServerSocket();
void bindServerSocket();
void addPollfd(int fd, short events, short revents);
};
#endif

View File

@@ -7,6 +7,7 @@ int main(int ac, char **av)
try {
server.parseArgs(ac, av);
server.init();
server.run();
} catch (std::exception& e) {
std::cerr << e.what() << std::endl;
return 1;