From f9866b0970832e77c256d78a10518d99f80230dd Mon Sep 17 00:00:00 2001 From: Master_Laplace Date: Mon, 8 Jan 2024 09:15:00 +0100 Subject: [PATCH] update(Network): Refactor IOMultiplexer class to handle SIGINT signal --- Flakkari/Network/IOMultiplexer.cpp | 38 ++++++++++++++++++++++++++++-- Flakkari/Network/IOMultiplexer.hpp | 4 ++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/Flakkari/Network/IOMultiplexer.cpp b/Flakkari/Network/IOMultiplexer.cpp index 9a32e879..fb14cb9e 100644 --- a/Flakkari/Network/IOMultiplexer.cpp +++ b/Flakkari/Network/IOMultiplexer.cpp @@ -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) @@ -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) @@ -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) @@ -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 diff --git a/Flakkari/Network/IOMultiplexer.hpp b/Flakkari/Network/IOMultiplexer.hpp index 931bf4c6..f466dea2 100644 --- a/Flakkari/Network/IOMultiplexer.hpp +++ b/Flakkari/Network/IOMultiplexer.hpp @@ -56,6 +56,7 @@ class IOMultiplexer { #include #include #include +#include /** * @brief PSELECT is a class that represents a PSELECT @@ -131,11 +132,13 @@ class PSELECT : public IOMultiplexer { std::vector _sockets; FileDescriptor _maxFd = 0; struct timespec _timeout = {0, 0}; + sigset_t _sigmask; }; #endif #if defined(_PPOLL_) #include +#include /** * @brief PPOLL is a class that represents a PPOLL @@ -235,6 +238,7 @@ class PPOLL : public IOMultiplexer { private: std::vector _pollfds; struct timespec _timeout = {0, 0}; + sigset_t _sigmask; }; #endif