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