SNode.C
Loading...
Searching...
No Matches
SocketServer.h
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#ifndef CORE_SOCKET_STREAM_SOCKETSERVERNEW_H
43#define CORE_SOCKET_STREAM_SOCKETSERVERNEW_H
44
45#include "core/SNodeC.h"
46#include "core/eventreceiver/AcceptEventReceiver.h"
47#include "core/socket/Socket.h" // IWYU pragma: export
48#include "core/socket/State.h" // IWYU pragma: export
49#include "core/socket/stream/SocketContextFactory.h"
50#include "core/timer/Timer.h"
51
52#ifndef DOXYGEN_SHOULD_SKIP_THIS
53
54#include "log/Logger.h"
55#include "utils/Random.h"
56
57#include <algorithm>
58#include <functional> // IWYU pragma: export
59#include <type_traits> // IWYU pragma: export
60
61#endif /* DOXYGEN_SHOULD_SKIP_THIS */
62
63namespace core::socket::stream {
64
65 template <typename SocketAcceptorT, typename SocketContextFactoryT, typename... Args>
66 requires std::is_base_of_v<core::eventreceiver::AcceptEventReceiver, SocketAcceptorT> &&
67 std::is_base_of_v<core::socket::stream::SocketContextFactory, SocketContextFactoryT>
68 class SocketServer : public core::socket::Socket<typename SocketAcceptorT::Config> {
69 private:
70 using SocketAcceptor = SocketAcceptorT;
71 using SocketContextFactory = SocketContextFactoryT;
72
73 using Super = core::socket::Socket<typename SocketAcceptor::Config>;
74
75 public:
78 using Config = typename SocketAcceptor::Config;
79
80 private:
81 SocketServer(const std::shared_ptr<Config>& config,
82 const std::shared_ptr<SocketContextFactory>& socketContextFactory,
83 const std::function<void(SocketConnection*)>& onConnect,
84 const std::function<void(SocketConnection*)>& onConnected,
85 const std::function<void(SocketConnection*)>& onDisconnect)
86 : Super(config)
87 , sharedContext(std::make_shared<Context>(socketContextFactory, onConnect, onConnected, onDisconnect)) {
88 }
89
90 public:
91 SocketServer(const std::string& name,
92 const std::function<void(SocketConnection*)>& onConnect,
93 const std::function<void(SocketConnection*)>& onConnected,
94 const std::function<void(SocketConnection*)>& onDisconnect,
95 Args&&... args)
96 : Super(name)
97 , sharedContext(std::make_shared<Context>(
98 std::make_shared<SocketContextFactory>(std::forward<Args>(args)...),
99 [onConnect](SocketConnection* socketConnection) { // onConnect
100 LOG(DEBUG) << socketConnection->getConnectionName() << ": OnConnect";
101
102 LOG(DEBUG) << " Local: " << socketConnection->getLocalAddress().toString();
103 LOG(DEBUG) << " Peer: " << socketConnection->getRemoteAddress().toString();
104
105 if (onConnect) {
106 onConnect(socketConnection);
107 }
108 },
109 [onConnected](SocketConnection* socketConnection) { // onConnected
110 LOG(DEBUG) << socketConnection->getConnectionName() << ": OnConnected";
111
112 LOG(DEBUG) << " Local: " << socketConnection->getLocalAddress().toString();
113 LOG(DEBUG) << " Peer: " << socketConnection->getRemoteAddress().toString();
114
115 if (onConnected) {
116 onConnected(socketConnection);
117 }
118 },
119 [onDisconnect](SocketConnection* socketConnection) { // onDisconnect
120 LOG(DEBUG) << socketConnection->getConnectionName() << ": OnDisconnect";
121
122 LOG(DEBUG) << " Local: " << socketConnection->getLocalAddress().toString();
123 LOG(DEBUG) << " Peer: " << socketConnection->getRemoteAddress().toString();
124
125 LOG(DEBUG) << " Online Since: " << socketConnection->getOnlineSince();
126 LOG(DEBUG) << " Online Duration: " << socketConnection->getOnlineDuration();
127
128 LOG(DEBUG) << " Total Queued: " << socketConnection->getTotalQueued();
129 LOG(DEBUG) << " Total Sent: " << socketConnection->getTotalSent();
130 LOG(DEBUG) << " Write Delta: " << socketConnection->getTotalQueued() - socketConnection->getTotalSent();
131 LOG(DEBUG) << " Total Read: " << socketConnection->getTotalRead();
132 LOG(DEBUG) << " Total Processed: " << socketConnection->getTotalProcessed();
133 LOG(DEBUG) << " Read Delta: " << socketConnection->getTotalRead() - socketConnection->getTotalProcessed();
134
135 if (onDisconnect) {
136 onDisconnect(socketConnection);
137 }
138 })) {
139 }
140
141 SocketServer(const std::function<void(SocketConnection*)>& onConnect,
142 const std::function<void(SocketConnection*)>& onConnected,
143 const std::function<void(SocketConnection*)>& onDisconnect,
144 Args&&... args)
145 : SocketServer("", onConnect, onConnected, onDisconnect, std::forward<Args>(args)...) {
146 }
147
148 SocketServer(const std::string& name, Args&&... args)
149 : SocketServer(name, {}, {}, {}, std::forward<Args>(args)...) {
150 }
151
152 explicit SocketServer(Args&&... args)
153 : SocketServer("", std::forward<Args>(args)...) {
154 }
155
156 private:
157 const SocketServer& realListen(const std::function<void(const SocketAddress&, core::socket::State)>& onStatus,
158 unsigned int tries,
159 double retryTimeoutScale) const {
160 LOG(DEBUG) << this->config->getInstanceName() << ": Initiating listen";
161
163 [config = this->config, sharedContext = this->sharedContext, onStatus, tries, retryTimeoutScale] {
165 new SocketAcceptor(
166 sharedContext->socketContextFactory,
167 sharedContext->onConnect,
168 sharedContext->onConnected,
169 sharedContext->onDisconnect,
170 [config,
171 onConnect = sharedContext->onConnect,
172 onConnected = sharedContext->onConnected,
173 onDisconnect = sharedContext->onDisconnect,
174 socketContextFactory = sharedContext->socketContextFactory,
175 onStatus,
176 tries,
177 retryTimeoutScale](const SocketAddress& socketAddress, core::socket::State state) {
178 const bool retryFlag = (state & core::socket::State::NO_RETRY) == 0;
179 state &= ~core::socket::State::NO_RETRY;
180 onStatus(socketAddress, state);
181
182 if (retryFlag && config->getRetry() // Shall we potentially retry? In case are the ...
183 && (config->getRetryTries() == 0 ||
184 tries < config->getRetryTries()) // ... limits not reached and has an ...
185 && (state == core::socket::State::ERROR ||
186 (state == core::socket::State::FATAL && config->getRetryOnFatal()))) { // error occurred?
187 double relativeRetryTimeout =
188 config->getRetryLimit() > 0
189 ? std::min<double>(config->getRetryTimeout() * retryTimeoutScale, config->getRetryLimit())
190 : config->getRetryTimeout() * retryTimeoutScale;
191 relativeRetryTimeout -= utils::Random::getInRange(-config->getRetryJitter(), config->getRetryJitter()) *
192 relativeRetryTimeout / 100.;
193
194 LOG(INFO) << config->getInstanceName() << ": Retry listen in " << relativeRetryTimeout << " seconds";
195
197 [config,
198 onConnect,
199 onConnected,
200 onDisconnect,
201 onStatus,
202 tries,
203 retryTimeoutScale,
204 socketContextFactory]() {
205 if (config->getRetry()) {
206 SocketServer(config, socketContextFactory, onConnect, onConnected, onDisconnect)
207 .realListen(onStatus, tries + 1, retryTimeoutScale * config->getRetryBase());
208 } else {
209 LOG(INFO) << config->getInstanceName() << ": Retry listen disabled during wait";
210 }
211 },
212 relativeRetryTimeout);
213 }
214 },
215 config);
216 }
217 });
218
219 return *this;
220 }
221
222 public:
223 const SocketServer& listen(const std::function<void(const SocketAddress&, core::socket::State)>& onStatus) const {
224 return realListen(onStatus, 0, 1);
225 }
226
227 const SocketServer& listen(const SocketAddress& localAddress,
228 const std::function<void(const SocketAddress&, core::socket::State)>& onStatus) const {
229 Super::config->Local::setSocketAddress(localAddress);
230
231 return listen(onStatus);
232 }
233
234 const SocketServer& listen(const SocketAddress& localAddress,
235 int backlog,
236 const std::function<void(const SocketAddress&, core::socket::State)>& onStatus) const {
237 Super::config->Local::setBacklog(backlog);
238
239 return listen(localAddress, onStatus);
240 }
241
242 std::function<void(SocketConnection*)>& getOnConnect() const {
243 return sharedContext->onConnect;
244 }
245
246 const SocketServer& setOnConnect(const std::function<void(SocketConnection*)>& onConnect, bool initialize = false) const {
247 sharedContext->onConnect =
248 initialize ? onConnect : [oldOnConnect = sharedContext->onConnect, onConnect](SocketConnection* socketConnection) {
249 oldOnConnect(socketConnection);
250 onConnect(socketConnection);
251 };
252
253 return *this;
254 }
255
256 std::function<void(SocketConnection*)>& getOnConnected() const {
257 return sharedContext->onConnected();
258 }
259
260 const SocketServer& setOnConnected(const std::function<void(SocketConnection*)>& onConnected, bool initialize = false) const {
261 sharedContext->onConnected =
262 initialize ? onConnected : [oldOnConnected = sharedContext->onConnected, onConnected](SocketConnection* socketConnection) {
263 oldOnConnected(socketConnection);
264 onConnected(socketConnection);
265 };
266
267 return *this;
268 }
269
270 std::function<void(SocketConnection*)>& getOnDisconnect() const {
271 return sharedContext->onDisconnect;
272 }
273
274 SocketServer& setOnDisconnect(const std::function<void(SocketConnection*)>& onDisconnect, bool initialize = false) {
275 sharedContext->onDisconnect =
276 initialize ? onDisconnect
277 : [oldOnDisconnect = sharedContext->onDisconnect, onDisconnect](SocketConnection* socketConnection) {
278 oldOnDisconnect(socketConnection);
279 onDisconnect(socketConnection);
280 };
281
282 return *this;
283 }
284
286 return sharedContext->socketContextFactory;
287 }
288
289 private:
290 struct Context {
291 Context(const std::shared_ptr<SocketContextFactory>& socketContextFactory,
292 std::function<void(SocketConnection*)> onConnect,
293 std::function<void(SocketConnection*)> onConnected,
294 std::function<void(SocketConnection*)> onDisconnect)
295 : socketContextFactory(socketContextFactory)
296 , onConnect(onConnect)
297 , onConnected(onConnected)
298 , onDisconnect(onDisconnect) {
299 }
301
302 std::function<void(SocketConnection*)> onConnect;
303 std::function<void(SocketConnection*)> onConnected;
304 std::function<void(SocketConnection*)> onDisconnect;
305 };
306
307 std::shared_ptr<Context> sharedContext;
308 };
309
310 template <typename SocketServer, typename... Args>
311 SocketServer Server(const std::string& instanceName,
312 const std::function<void(typename SocketServer::Config&)>& configurator,
313 Args&&... socketContextFactoryArgs) {
314 const SocketServer socketServer(instanceName, std::forward<Args>(socketContextFactoryArgs)...);
315
316 configurator(socketServer.getConfig());
317
318 return socketServer;
319 }
320
321 template <typename SocketServer, typename... Args>
322 SocketServer Server(const std::string& instanceName, Args&&... socketContextFactoryArgs) {
323 return SocketServer(instanceName, std::forward<Args>(socketContextFactoryArgs)...);
324 }
325
326} // namespace core::socket::stream
327
328#endif // CORE_SOCKET_STREAM_SOCKETSERVERNEW_H
#define APPLICATION(req, res)
Definition Router.h:68
static void atNextTick(const std::function< void(void)> &callBack)
static State state()
Definition SNodeC.cpp:76
AcceptEventReceiver(const std::string &name, const utils::Timeval &timeout)
Config & getConfig() const
Definition Socket.hpp:65
State & operator&=(int state)
Definition State.cpp:90
static constexpr int DISABLED
Definition State.h:56
bool operator==(const int &state) const
Definition State.cpp:74
State operator&(int state)
Definition State.cpp:106
static constexpr int ERROR
Definition State.h:57
std::string what() const
Definition State.cpp:114
static constexpr int FATAL
Definition State.h:58
static constexpr int OK
Definition State.h:55
static constexpr int NO_RETRY
Definition State.h:59
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
const SocketServer & listen(const SocketAddress &localAddress, const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const
std::shared_ptr< SocketContextFactory > getSocketContextFactory() const
SocketServer(const std::string &name, const std::function< void(SocketConnection *)> &onConnect, const std::function< void(SocketConnection *)> &onConnected, const std::function< void(SocketConnection *)> &onDisconnect, Args &&... args)
typename SocketAcceptor::SocketAddress SocketAddress
SocketServer(const std::string &name, Args &&... args)
SocketServer(const std::function< void(SocketConnection *)> &onConnect, const std::function< void(SocketConnection *)> &onConnected, const std::function< void(SocketConnection *)> &onDisconnect, Args &&... args)
core::socket::Socket< typename SocketAcceptor::Config > Super
std::function< void(SocketConnection *)> & getOnDisconnect() const
const SocketServer & realListen(const std::function< void(const SocketAddress &, core::socket::State)> &onStatus, unsigned int tries, double retryTimeoutScale) const
std::function< void(SocketConnection *)> & getOnConnected() const
const SocketServer & setOnConnected(const std::function< void(SocketConnection *)> &onConnected, bool initialize=false) const
const SocketServer & setOnConnect(const std::function< void(SocketConnection *)> &onConnect, bool initialize=false) const
const SocketServer & listen(const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const
std::shared_ptr< Context > sharedContext
typename SocketAcceptor::SocketConnection SocketConnection
SocketServer & setOnDisconnect(const std::function< void(SocketConnection *)> &onDisconnect, bool initialize=false)
std::function< void(SocketConnection *)> & getOnConnect() const
SocketContextFactoryT SocketContextFactory
SocketServer(const std::shared_ptr< Config > &config, const std::shared_ptr< SocketContextFactory > &socketContextFactory, const std::function< void(SocketConnection *)> &onConnect, const std::function< void(SocketConnection *)> &onConnected, const std::function< void(SocketConnection *)> &onDisconnect)
const SocketServer & listen(const SocketAddress &localAddress, int backlog, const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const
typename SocketAcceptor::Config Config
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)
static Timer singleshotTimer(const std::function< void()> &dispatcher, const utils::Timeval &timeout)
Definition Timer.cpp:57
Controller(const std::shared_ptr< web::http::server::Request > &request, const std::shared_ptr< web::http::server::Response > &response)
void send(const std::string &chunk)
Definition Response.cpp:169
WebAppT(const std::string &name, const Router &router)
Definition WebAppT.h:80
typename Server::SocketConnection SocketConnection
Definition WebAppT.h:69
ServerT Server
Definition WebAppT.h:63
express::Response Response
Definition WebAppT.h:67
express::Request Request
Definition WebAppT.h:66
typename Server::SocketAddress SocketAddress
Definition WebAppT.h:70
WebAppT(const std::string &name)
Definition WebAppT.h:76
static void free()
Definition WebApp.cpp:72
static void init(int argc, char *argv[])
Definition WebApp.cpp:56
static void stop()
Definition WebApp.cpp:64
static core::TickStatus tick(const utils::Timeval &timeOut=0)
Definition WebApp.cpp:68
static core::State state()
Definition WebApp.cpp:76
static int start(const utils::Timeval &timeOut={LONG_MAX, 0})
Definition WebApp.cpp:60
WebApp(const Router &router)
Definition WebApp.cpp:52
const std::string & getInstanceName() const
std::string toString(bool expanded=true) const override
const Super & listen(const std::string &ipOrHostname, uint16_t port, int backlog, const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const
const Super & listen(uint16_t port, const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const
const Super & listen(uint16_t port, int backlog, const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const
const Super & listen(const std::string &ipOrHostname, uint16_t port, const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const
core::socket::stream:: SocketServer< SocketAcceptorT< net::in::phy::stream::PhysicalSocketServer, ConfigSocketServerT >, SocketContextFactoryT, Args... > Super
void getAttribute(const std::function< void(Attribute &)> &onFound, const std::function< void(const std::string &)> &onNotFound, const std::string &subKey="") const
static double getInRange(double ll, double ul)
Definition Random.cpp:52
Timeval(const std::initializer_list< time_t > &initList) noexcept
Definition Timeval.cpp:59
int main(int argc, char *argv[])
SocketServer Server(const std::string &instanceName, const std::function< void(typename SocketServer::Config &)> &configurator, Args &&... socketContextFactoryArgs)
SocketServer Server(const std::string &instanceName, Args &&... socketContextFactoryArgs)
Definition Timer.h:59
TickStatus
Definition TickStatus.h:51
State
Definition State.h:51
@ RUNNING
Definition State.h:51
@ INITIALIZED
Definition State.h:51
SocketServer< SocketContextFactory, SocketContextFactoryArgs... > Server(const std::string &instanceName, const std::function< void(typename SocketServer< SocketContextFactory, SocketContextFactoryArgs... >::Config &)> &configurator, SocketContextFactoryArgs &&... socketContextFactoryArgs)
SocketServer< SocketContextFactory, SocketContextFactoryArgs... > Server(const std::string &instanceName, SocketContextFactoryArgs &&... socketContextFactoryArgs)
net::in::stream::SocketServer< core::socket::stream::legacy::SocketAcceptor, net::in::stream::legacy::config::ConfigSocketServer, SocketContextFactoryT, Args... > SocketServer
Definition Config.h:59
web::http::server::Server< net::in::stream::legacy::SocketServer > Server
Definition Server.h:54
std::shared_ptr< SocketContextFactory > socketContextFactory
std::function< void(SocketConnection *)> onConnected
std::function< void(SocketConnection *)> onConnect
std::function< void(SocketConnection *)> onDisconnect
Context(const std::shared_ptr< SocketContextFactory > &socketContextFactory, std::function< void(SocketConnection *)> onConnect, std::function< void(SocketConnection *)> onConnected, std::function< void(SocketConnection *)> onDisconnect)