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