SNode.C
Loading...
Searching...
No Matches
EventMultiplexer.cpp
Go to the documentation of this file.
1/*
2 * SNode.C - a slim toolkit for network communication
3 * Copyright (C) Volker Christian <me@vchrist.at>
4 * 2020, 2021, 2022, 2023, 2024, 2025
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published
8 * by the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "core/multiplexer/epoll/EventMultiplexer.h"
21
22#include "core/multiplexer/epoll/DescriptorEventPublisher.h"
23
24#ifndef DOXYGEN_SHOULD_SKIP_THIS
25
26#include "log/Logger.h"
27#include "utils/Timeval.h"
28
29#include <array>
30
31#endif /* DOXYGEN_SHOULD_SKIP_THIS */
32
34 static core::multiplexer::epoll::EventMultiplexer eventMultiplexer;
35
36 return eventMultiplexer;
37}
38
39namespace core::multiplexer::epoll {
40
41 EventMultiplexer::EventMultiplexer()
42 : core::EventMultiplexer(new core::multiplexer::epoll::DescriptorEventPublisher("READ", //
43 epfds[core::EventMultiplexer::DISP_TYPE::RD],
44 EPOLLIN,
45 EPOLLIN | EPOLLHUP | EPOLLRDHUP | EPOLLERR),
46 new core::multiplexer::epoll::DescriptorEventPublisher("WRITE", //
47 epfds[core::EventMultiplexer::DISP_TYPE::WR],
48 EPOLLOUT,
49 EPOLLOUT),
50 new core::multiplexer::epoll::DescriptorEventPublisher("EXCEPT", //
51 epfds[core::EventMultiplexer::DISP_TYPE::EX],
52 EPOLLPRI,
53 EPOLLPRI))
54 , epfd(core::system::epoll_create1(EPOLL_CLOEXEC)) {
55 epoll_event event{};
56 event.events = EPOLLIN;
57
58 event.data.ptr = descriptorEventPublishers[core::EventMultiplexer::DISP_TYPE::RD];
59 core::system::epoll_ctl(epfd, EPOLL_CTL_ADD, epfds[core::EventMultiplexer::DISP_TYPE::RD], &event);
60
61 event.data.ptr = descriptorEventPublishers[core::EventMultiplexer::DISP_TYPE::WR];
62 core::system::epoll_ctl(epfd, EPOLL_CTL_ADD, epfds[core::EventMultiplexer::DISP_TYPE::WR], &event);
63
64 event.data.ptr = descriptorEventPublishers[core::EventMultiplexer::DISP_TYPE::EX];
65 core::system::epoll_ctl(epfd, EPOLL_CTL_ADD, epfds[core::EventMultiplexer::DISP_TYPE::EX], &event);
66
67 LOG(DEBUG) << "Core::multiplexer: epoll";
68 }
69
70 int EventMultiplexer::monitorDescriptors(utils::Timeval& tickTimeout, const sigset_t& sigMask) {
71 return core::system::epoll_pwait(epfd, ePollEvents, 3, tickTimeout.getMs(), &sigMask);
72 }
73
74 void EventMultiplexer::spanActiveEvents(int activeDescriptorCount) {
75 for (int i = 0; i < activeDescriptorCount; i++) {
76 if ((ePollEvents[i].events & EPOLLIN) != 0) {
77 static_cast<core::DescriptorEventPublisher*>(ePollEvents[i].data.ptr)->spanActiveEvents();
78 }
79 }
80 }
81
82} // namespace core::multiplexer::epoll
core::EventMultiplexer & EventMultiplexer()