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, 2026
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/*
21 * MIT License
22 *
23 * Permission is hereby granted, free of charge, to any person obtaining a copy
24 * of this software and associated documentation files (the "Software"), to deal
25 * in the Software without restriction, including without limitation the rights
26 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
27 * copies of the Software, and to permit persons to whom the Software is
28 * furnished to do so, subject to the following conditions:
29 *
30 * The above copyright notice and this permission notice shall be included in
31 * all copies or substantial portions of the Software.
32 *
33 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
39 * THE SOFTWARE.
40 */
41
42#include "core/multiplexer/poll/EventMultiplexer.h"
43
44#include "core/DescriptorEventReceiver.h"
45#include "core/multiplexer/poll/DescriptorEventPublisher.h"
46
47#ifndef DOXYGEN_SHOULD_SKIP_THIS
48
49#include "log/Logger.h"
50#include "utils/Timeval.h"
51
52#include <algorithm>
53#include <array>
54#include <cstdint>
55#include <ctime>
56#include <string>
57#include <utility>
58
59#endif /* DOXYGEN_SHOULD_SKIP_THIS */
60
61core::EventMultiplexer& EventMultiplexer() {
62 static core::multiplexer::poll::EventMultiplexer eventMultiplexer;
63
64 return eventMultiplexer;
65}
66
67namespace core::multiplexer::poll {
68
70 pollfds.resize(1, {-1, 0, 0});
71 pollFdIndices.reserve(1);
72 }
73
74 void PollFdsManager::muxAdd(core::DescriptorEventReceiver* eventReceiver, short event) {
75 const int fd = eventReceiver->getRegisteredFd();
76
77 if (!pollFdIndices.contains(fd)) {
78 pollfds[nextIndex].events = event;
79 pollfds[nextIndex].fd = fd;
80
81 pollFdIndices[fd].index = nextIndex;
82 pollFdIndices[fd].events = event;
83
84 ++nextIndex;
85
86 if (nextIndex == pollfds.size()) {
87 pollfds.resize(pollfds.size() * 2, {-1, 0, 0});
88 pollFdIndices.reserve(pollfds.size());
89 }
90 } else {
91 PollFdIndex& pollFdIndex = pollFdIndices[fd];
92
93 pollfds[pollFdIndex.index].events |= event;
94 pollFdIndex.events |= event;
95 }
96 }
97
98 void PollFdsManager::muxDel(int fd, short event) {
99 const pollfdindex_map::iterator itPollFdIndex = pollFdIndices.find(fd);
100
101 PollFdIndex& pollFdIndex = itPollFdIndex->second;
102
103 pollfds[pollFdIndex.index].events &= static_cast<short>(~event); // tilde promotes to int
104 pollFdIndex.events &= static_cast<short>(~event); // tilde promotes to int
105
106 if (pollFdIndex.events == 0) {
107 pollfds[pollFdIndex.index].fd = -1; // Compress will keep track of that descriptor
108 pollFdIndices.erase(fd);
109
110 if (pollfds.size() > (pollFdIndices.size() * 2) + 1) {
112 }
113 }
114 }
115
116 void PollFdsManager::muxOn(const DescriptorEventReceiver* eventReceiver, short event) {
117 pollfds[pollFdIndices.find(eventReceiver->getRegisteredFd())->second.index].events |= event;
118 }
119
120 void PollFdsManager::muxOff(const DescriptorEventReceiver* eventReceiver, short event) {
121 pollfds[pollFdIndices.find(eventReceiver->getRegisteredFd())->second.index].events &=
122 static_cast<short>(~event); // Tilde promotes to int
123 }
124
126 (void) std::remove_if(pollfds.begin(), pollfds.end(), [](const pollfd& pollFd) -> bool {
127 return pollFd.fd < 0;
128 });
129
130 pollfds.resize(pollFdIndices.size() + 1, {-1, 0, 0});
131
132 pollFdIndices.reserve(pollFdIndices.size() + 1);
133
134 for (uint32_t i = 0; i < pollFdIndices.size(); i++) {
135 pollFdIndices[pollfds[i].fd].index = i;
136 }
137
138 nextIndex = pollFdIndices.size();
139 }
140
142 return pollfds.data();
143 }
144
146 return pollFdIndices;
147 }
148
150 return nextIndex;
151 }
152
156 POLLIN,
160 POLLOUT,
161 POLLOUT),
164 POLLPRI,
165 POLLPRI)) {
166 LOG(DEBUG) << "Core::multiplexer: poll";
167 }
168
169 int EventMultiplexer::monitorDescriptors(utils::Timeval& tickTimeOut, const sigset_t& sigMask) {
170 const timespec timeSpec = tickTimeOut.getTimespec();
171
172 return core::system::ppoll(pollFdsManager.getEvents(), pollFdsManager.getCurrentSize(), &timeSpec, &sigMask);
173 }
174
175 void EventMultiplexer::spanActiveEvents(int activeDescriptorCount) {
176 if (activeDescriptorCount > 0) {
177 for (core::DescriptorEventPublisher* const descriptorEventPublisher : descriptorEventPublishers) {
178 descriptorEventPublisher->spanActiveEvents();
179 }
180 }
181 }
182
183} // namespace core::multiplexer::poll
#define LOG(level)
Definition Logger.h:148
int monitorDescriptors(utils::Timeval &tickTimeOut, const sigset_t &sigMask) override
void spanActiveEvents(int activeDescriptorCount) override
void muxOff(const DescriptorEventReceiver *eventReceiver, short event)
void muxAdd(core::DescriptorEventReceiver *eventReceiver, short event)
const pollfdindex_map & getPollFdIndices() const
void muxOn(const core::DescriptorEventReceiver *eventReceiver, short event)
LogMessage(Level level, int verboseLevel=-1, bool withErrno=false)
Definition Logger.cpp:280
timespec getTimespec() const
Definition Timeval.cpp:173