Skip to content

Commit

Permalink
update(Network): Refactor IOMultiplexer class to handle SIGINT signal
Browse files Browse the repository at this point in the history
  • Loading branch information
MasterLaplace committed Jan 8, 2024
1 parent 3ce704e commit f9866b0
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
38 changes: 36 additions & 2 deletions Flakkari/Network/IOMultiplexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,30 @@ PSELECT::PSELECT(int fileDescriptor)

_timeout.tv_sec = 1;
_timeout.tv_nsec = 0; // 100ms

// handle ctrl+c or break
sigemptyset(&_sigmask);
sigaddset(&_sigmask, SIGINT);

struct sigaction sa;
sa.sa_handler = SIG_IGN;
sigaction(SIGINT, &sa, nullptr);
}

PSELECT::PSELECT()
{
FD_ZERO(&_fds);

_timeout.tv_sec = 1;
_timeout.tv_nsec = 0; // 100ms

// handle ctrl+c or break
sigemptyset(&_sigmask);
sigaddset(&_sigmask, SIGINT);

struct sigaction sa;
sa.sa_handler = SIG_IGN;
sigaction(SIGINT, &sa, nullptr);
}

void PSELECT::addSocket(FileDescriptor socket)
Expand Down Expand Up @@ -61,7 +79,7 @@ int PSELECT::wait()
FD_ZERO(&_fds);
for (auto &fd : _sockets)
FD_SET(fd, &_fds);
return ::pselect(_maxFd + 1, &_fds, nullptr, nullptr, &_timeout, nullptr);
return ::pselect(_maxFd + 1, &_fds, nullptr, nullptr, &_timeout, &_sigmask);
}

bool PSELECT::isReady(FileDescriptor socket)
Expand Down Expand Up @@ -90,11 +108,27 @@ PPOLL::PPOLL(int fileDescriptor, event_t events)

_timeout.tv_sec = 1;
_timeout.tv_nsec = 0;// 100000000; // 100ms

// handle ctrl+c or break
sigemptyset(&_sigmask);
sigaddset(&_sigmask, SIGINT);

struct sigaction sa;
sa.sa_handler = SIG_IGN;
sigaction(SIGINT, &sa, nullptr);
}

PPOLL::PPOLL() {
_timeout.tv_sec = 1;
_timeout.tv_nsec = 0;// 100000000; // 100ms

// handle ctrl+c or break
sigemptyset(&_sigmask);
sigaddset(&_sigmask, SIGINT);

struct sigaction sa;
sa.sa_handler = SIG_IGN;
sigaction(SIGINT, &sa, nullptr);
}

void PPOLL::addSocket(FileDescriptor socket)
Expand Down Expand Up @@ -130,7 +164,7 @@ void PPOLL::removeSocket(FileDescriptor socket)
int PPOLL::wait()
{
#ifdef __linux__
return ppoll(_pollfds.data(), _pollfds.size(), &_timeout, nullptr);
return ppoll(_pollfds.data(), _pollfds.size(), &_timeout, &_sigmask);
#elif defined(__APPLE__)
return poll(_pollfds.data(), _pollfds.size(), 100);
#endif
Expand Down
4 changes: 4 additions & 0 deletions Flakkari/Network/IOMultiplexer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class IOMultiplexer {
#include <sys/select.h>
#include <sys/time.h>
#include <sys/types.h>
#include <signal.h>

/**
* @brief PSELECT is a class that represents a PSELECT
Expand Down Expand Up @@ -131,11 +132,13 @@ class PSELECT : public IOMultiplexer {
std::vector<FileDescriptor> _sockets;
FileDescriptor _maxFd = 0;
struct timespec _timeout = {0, 0};
sigset_t _sigmask;
};
#endif

#if defined(_PPOLL_)
#include <sys/poll.h>
#include <signal.h>

/**
* @brief PPOLL is a class that represents a PPOLL
Expand Down Expand Up @@ -235,6 +238,7 @@ class PPOLL : public IOMultiplexer {
private:
std::vector<pollfd> _pollfds;
struct timespec _timeout = {0, 0};
sigset_t _sigmask;
};
#endif

Expand Down

0 comments on commit f9866b0

Please sign in to comment.