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 LOG(TRACE) << config->getInstanceName() << " Starting";
117
118 localAddress = config->Local::getSocketAddress();
119
120 core::socket::State state = core::socket::STATE_OK;
121
122 if (physicalServerSocket.open(config->getSocketOptions(), PhysicalServerSocket::Flags::NONBLOCK) < 0) {
123 switch (errno) {
124 case EMFILE:
125 case ENFILE:
126 case ENOBUFS:
127 case ENOMEM:
128 PLOG(DEBUG) << config->getInstanceName() << " open: '" << localAddress.toString() << "'";
129
130 state = core::socket::STATE_ERROR;
131 break;
132 default:
133 PLOG(DEBUG) << config->getInstanceName() << " open: '" << localAddress.toString() << "'";
134
135 state = core::socket::STATE_FATAL;
136 break;
137 }
138 } else if (physicalServerSocket.bind(localAddress) < 0) {
139 switch (errno) {
140 case EADDRINUSE:
141 PLOG(DEBUG) << config->getInstanceName() << " bind: '" << localAddress.toString() << "'";
142
143 state = core::socket::STATE_ERROR;
144 break;
145 default:
146 PLOG(DEBUG) << config->getInstanceName() << " bind: '" << localAddress.toString() << "'";
147
148 state = core::socket::STATE_FATAL;
149 break;
150 }
151 } else if (physicalServerSocket.listen(config->getBacklog()) < 0) {
152 switch (errno) {
153 case EADDRINUSE:
154 PLOG(DEBUG) << config->getInstanceName() << " listen: '" << localAddress.toString() << "'";
155
156 state = core::socket::STATE_ERROR;
157 break;
158 default:
159 PLOG(DEBUG) << config->getInstanceName() << " listen: '" << localAddress.toString() << "'";
160
161 state = core::socket::STATE_FATAL;
162 break;
163 }
164 } else {
165 if (enable(physicalServerSocket.getFd())) {
166 LOG(DEBUG) << config->getInstanceName() << " enabled: '" << localAddress.toString() << "' success";
167 } else {
168 LOG(DEBUG) << config->getInstanceName() << " enabled: '" << localAddress.toString() << "' failed";
169
170 state = core::socket::STATE(core::socket::STATE_FATAL, ECANCELED, "SocketAcceptor not enabled");
171 }
172 }
173
174 SocketAddress currentLocalAddress = localAddress;
175 if (localAddress.useNext()) {
176 onStatus(currentLocalAddress, (state | core::socket::State::NO_RETRY));
177
178 LOG(DEBUG) << config->getInstanceName() << " using next SocketAddress: '"
179 << config->Local::getSocketAddress().toString() << "'";
180
182 } else {
183 onStatus(currentLocalAddress, state);
184 }
185 } catch (const typename SocketAddress::BadSocketAddress& badSocketAddress) {
186 LOG(DEBUG) << config->getInstanceName() << " " << badSocketAddress.what();
187
188 onStatus({}, core::socket::STATE(badSocketAddress.getState(), badSocketAddress.getErrnum(), badSocketAddress.what()));
189 }
190 } else {
191 LOG(DEBUG) << config->getInstanceName() << " disabled";
192
193 onStatus({}, core::socket::STATE_DISABLED);
194 }
195
196 if (isEnabled()) {
198 } else {
200 }
201 }
202
203 template <typename PhysicalSocketServer,
204 typename Config,
205 template <typename ConfigT, typename PhysicalSocketServerT> typename SocketConnection>
206 void SocketAcceptor<PhysicalSocketServer, Config, SocketConnection>::acceptEvent() {
207 int acceptsPerTick = config->getAcceptsPerTick();
208
209 do {
210 PhysicalServerSocket connectedPhysicalSocket(physicalServerSocket.accept4(PhysicalServerSocket::Flags::NONBLOCK),
211 physicalServerSocket.getBindAddress());
212 if (connectedPhysicalSocket.isValid()) {
213 LOG(DEBUG) << "[" << connectedPhysicalSocket.getFd() << " ]" << config->getInstanceName() << ": accept success: '"
214 << connectedPhysicalSocket.getBindAddress().toString() << "'";
215
216 SocketConnection* socketConnection = new SocketConnection(std::move(connectedPhysicalSocket), onDisconnect, config);
217
218 onConnect(socketConnection);
219 onConnected(socketConnection);
220 } else if (errno != EINTR && errno != EAGAIN && errno != EWOULDBLOCK) {
221 PLOG(WARNING) << config->getInstanceName() << " accept failed: '" << physicalServerSocket.getBindAddress().toString()
222 << "'";
223 }
224 } while (--acceptsPerTick > 0);
225 }
226
227 template <typename PhysicalSocketServer,
228 typename Config,
229 template <typename ConfigT, typename PhysicalSocketServerT> typename SocketConnection>
230 void SocketAcceptor<PhysicalSocketServer, Config, SocketConnection>::unobservedEvent() {
232 }
233
234 template <typename PhysicalSocketServer,
235 typename Config,
236 template <typename ConfigT, typename PhysicalSocketServerT> typename SocketConnection>
237 void SocketAcceptor<PhysicalSocketServer, Config, SocketConnection>::destruct() {
238 delete this;
239 }
240
241} // namespace core::socket::stream
void setTimeout(const utils::Timeval &timeout)
static void atNextTick(const std::function< void(void)> &callBack)
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