SNode.C
Loading...
Searching...
No Matches
SocketAcceptor.hpp
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/*
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/State.h"
43#include "core/socket/stream/SocketAcceptor.h"
44
45#ifndef DOXYGEN_SHOULD_SKIP_THIS
46
47#include "log/Logger.h"
48#include "utils/PreserveErrno.h"
49
50#include <iomanip>
51#include <string>
52#include <utility>
53
54#endif // DOXYGEN_SHOULD_SKIP_THIS
55
56namespace core::socket::stream {
57
58 template <typename PhysicalSocketServer,
59 typename Config,
60 template <typename ConfigT, typename PhysicalSocketServerT> typename SocketConnection>
61 SocketAcceptor<PhysicalSocketServer, Config, SocketConnection>::SocketAcceptor(
62 const std::shared_ptr<core::socket::stream::SocketContextFactory>& socketContextFactory,
63 const std::function<void(SocketConnection*)>& onConnect,
64 const std::function<void(SocketConnection*)>& onConnected,
65 const std::function<void(SocketConnection*)>& onDisconnect,
66 const std::function<void(const SocketAddress&, core::socket::State)>& onStatus,
67 const std::shared_ptr<Config>& config)
68 : core::eventreceiver::AcceptEventReceiver(config->getInstanceName() + " SocketAcceptor", 0)
69 , socketContextFactory(socketContextFactory)
70 , onConnect(onConnect)
71 , onConnected(onConnected)
72 , onDisconnect(onDisconnect)
73 , onStatus(onStatus)
74 , config(config) {
75 atNextTick([this]() {
76 if (core::eventLoopState() == core::State::RUNNING) {
77 init();
78 } else {
80 }
81 });
82 }
83
84 template <typename PhysicalSocketServer,
85 typename Config,
86 template <typename ConfigT, typename PhysicalSocketServerT> typename SocketConnection>
87 SocketAcceptor<PhysicalSocketServer, Config, SocketConnection>::SocketAcceptor(const SocketAcceptor& socketAcceptor)
88 : core::eventreceiver::AcceptEventReceiver(socketAcceptor.config->getInstanceName() + " SocketAcceptor", 0)
90 , onConnect(socketAcceptor.onConnect)
91 , onConnected(socketAcceptor.onConnected)
92 , onDisconnect(socketAcceptor.onDisconnect)
93 , onStatus(socketAcceptor.onStatus)
94 , config(socketAcceptor.config) {
95 atNextTick([this]() {
97 init();
98 } else {
100 }
102 }
103
104 template <typename PhysicalSocketServer,
105 typename Config,
106 template <typename ConfigT, typename PhysicalSocketServerT> typename SocketConnection>
107 SocketAcceptor<PhysicalSocketServer, Config, SocketConnection>::~SocketAcceptor() {
108 }
109
110 template <typename PhysicalSocketServer,
111 typename Config,
112 template <typename ConfigT, typename PhysicalSocketServerT> typename SocketConnection>
113 void SocketAcceptor<PhysicalSocketServer, Config, SocketConnection>::init() {
114 if (!config->getDisabled()) {
115 try {
116 core::socket::State state = core::socket::STATE_OK;
117
118 LOG(TRACE) << config->getInstanceName() << ": Starting";
119
120 bindAddress = config->Local::getSocketAddress();
121
122 if (physicalServerSocket.open(config->getSocketOptions(), PhysicalServerSocket::Flags::NONBLOCK) < 0) {
123 PLOG(ERROR) << config->getInstanceName() << " open " << bindAddress.toString();
124
125 switch (errno) {
126 case EMFILE:
127 case ENFILE:
128 case ENOBUFS:
129 case ENOMEM:
130 state = core::socket::STATE_ERROR;
131 break;
132 default:
133 state = core::socket::STATE_FATAL;
134 break;
135 }
136 } else {
137 LOG(DEBUG) << config->getInstanceName() << " open " << bindAddress.toString() << ": success";
138
139 if (physicalServerSocket.bind(bindAddress) < 0) {
140 PLOG(ERROR) << config->getInstanceName() << " bind " << bindAddress.toString();
141
142 switch (errno) {
143 case EADDRINUSE:
144
145 state = core::socket::STATE_ERROR;
146 break;
147 default:
148
149 state = core::socket::STATE_FATAL;
150 break;
151 }
152 } else {
153 LOG(DEBUG) << config->getInstanceName() << " bind " << bindAddress.toString() << ": success";
154
155 if (physicalServerSocket.listen(config->getBacklog()) < 0) {
156 PLOG(ERROR) << config->getInstanceName() << " listen " << bindAddress.toString();
157
158 switch (errno) {
159 case EADDRINUSE:
160 state = core::socket::STATE_ERROR;
161 break;
162 default:
163 state = core::socket::STATE_FATAL;
164 break;
165 }
166 } else {
167 LOG(DEBUG) << config->getInstanceName() << " listen " << bindAddress.toString() << ": success";
168
169 if (enable(physicalServerSocket.getFd())) {
170 LOG(DEBUG) << config->getInstanceName() << " enable " << bindAddress.toString() << ": success";
171 } else {
172 LOG(ERROR) << config->getInstanceName() << " enable " << bindAddress.toString()
173 << ": failed. No valid descriptor created";
174
175 state = core::socket::STATE(core::socket::STATE_FATAL, ECANCELED, "SocketAcceptor not enabled");
176 }
177 }
178 }
179 }
180
181 SocketAddress currentLocalAddress = bindAddress;
182 if (bindAddress.useNext()) {
183 onStatus(currentLocalAddress, (state | core::socket::State::NO_RETRY));
184
185 LOG(DEBUG) << config->getInstanceName()
186 << ": Using next SocketAddress: " << config->Local::getSocketAddress().toString();
187
189 } else {
190 onStatus(currentLocalAddress, state);
191 }
192 } catch (const typename SocketAddress::BadSocketAddress& badSocketAddress) {
193 core::socket::State state =
194 core::socket::STATE(badSocketAddress.getState(), badSocketAddress.getErrnum(), badSocketAddress.what());
195
196 LOG(ERROR) << state.what();
197
198 onStatus({}, state);
199 }
200 } else {
201 LOG(DEBUG) << config->getInstanceName() << ": disabled";
202
203 onStatus({}, core::socket::STATE_DISABLED);
204 }
205
206 if (isEnabled()) {
208 } else {
210 }
211 }
212
213 template <typename PhysicalSocketServer,
214 typename Config,
215 template <typename ConfigT, typename PhysicalSocketServerT> typename SocketConnection>
216 void SocketAcceptor<PhysicalSocketServer, Config, SocketConnection>::acceptEvent() {
217 int acceptsPerTick = config->getAcceptsPerTick();
218
219 do {
220 PhysicalServerSocket connectedPhysicalSocket(physicalServerSocket.accept4(PhysicalServerSocket::Flags::NONBLOCK), bindAddress);
221
222 if (connectedPhysicalSocket.isValid()) {
223 SocketConnection* socketConnection = new SocketConnection(std::move(connectedPhysicalSocket), onDisconnect, config);
224
225 LOG(DEBUG) << config->getInstanceName() << " accept " << bindAddress.toString() << ": success";
226 LOG(DEBUG) << " " << socketConnection->getRemoteAddress().toString() << " -> "
227 << socketConnection->getLocalAddress().toString();
228
229 onConnect(socketConnection);
230 onConnected(socketConnection);
231 } else if (errno != EINTR && errno != EAGAIN && errno != EWOULDBLOCK) {
232 PLOG(WARNING) << config->getInstanceName() << " accept " << bindAddress.toString();
233 }
234 } while (--acceptsPerTick > 0);
235 }
236
237 template <typename PhysicalSocketServer,
238 typename Config,
239 template <typename ConfigT, typename PhysicalSocketServerT> typename SocketConnection>
240 void SocketAcceptor<PhysicalSocketServer, Config, SocketConnection>::unobservedEvent() {
242 }
243
244 template <typename PhysicalSocketServer,
245 typename Config,
246 template <typename ConfigT, typename PhysicalSocketServerT> typename SocketConnection>
247 void SocketAcceptor<PhysicalSocketServer, Config, SocketConnection>::destruct() {
248 delete this;
249 }
250
251} // namespace core::socket::stream
void setTimeout(const utils::Timeval &timeout)
static void atNextTick(const std::function< void(void)> &callBack)
std::string what() const
Definition State.cpp:114
static constexpr int NO_RETRY
Definition State.h:59
State operator|(int state)
Definition State.cpp:102
SocketAcceptor(const std::shared_ptr< core::socket::stream::SocketContextFactory > &socketContextFactory, const std::function< void(SocketConnection *)> &onConnect, const std::function< void(SocketConnection *)> &onConnected, const std::function< void(SocketConnection *)> &onDisconnect, const std::function< void(const SocketAddress &, core::socket::State)> &onStatus, const std::shared_ptr< Config > &config)
std::function< void(SocketConnection *)> onConnected
SocketAcceptor(const SocketAcceptor &socketAcceptor)
std::function< void(SocketConnection *)> onConnect
std::function< void(SocketConnection *)> onDisconnect
std::shared_ptr< core::socket::stream::SocketContextFactory > socketContextFactory
std::shared_ptr< Config > config
PhysicalServerSocket physicalServerSocket
std::function< void(const SocketAddress &, core::socket::State)> onStatus
SocketAcceptor(const SocketAcceptor &socketAcceptor)
SocketAcceptor(const std::shared_ptr< core::socket::stream::SocketContextFactory > &socketContextFactory, const std::function< void(SocketConnection *)> &onConnect, const std::function< void(SocketConnection *)> &onConnected, const std::function< void(SocketConnection *)> &onDisconnect, const std::function< void(const SocketAddress &, core::socket::State)> &onStatus, const std::shared_ptr< Config > &config)
State
Definition State.h:51
State eventLoopState()
Definition State.cpp:52