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:
76 using SocketConnection = typename SocketAcceptor::SocketConnection;
77 using SocketAddress = typename SocketAcceptor::SocketAddress;
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 , socketContextFactory(socketContextFactory)
88 , onConnect(onConnect)
89 , onConnected(onConnected)
90 , onDisconnect(onDisconnect) {
91 }
92
93 public:
94 SocketServer(const std::string& name,
95 const std::function<void(SocketConnection*)>& onConnect,
96 const std::function<void(SocketConnection*)>& onConnected,
97 const std::function<void(SocketConnection*)>& onDisconnect,
98 Args&&... args)
99 : Super(name)
100 , socketContextFactory(std::make_shared<SocketContextFactory>(std::forward<Args>(args)...))
101 , onConnect([onConnect](SocketConnection* socketConnection) { // onConnect
102 LOG(DEBUG) << socketConnection->getConnectionName() << ": OnConnect";
103
104 LOG(DEBUG) << " Local: " << socketConnection->getLocalAddress().toString();
105 LOG(DEBUG) << " Peer: " << socketConnection->getRemoteAddress().toString();
106
107 if (onConnect) {
108 onConnect(socketConnection);
109 }
110 })
111 , onConnected([onConnected](SocketConnection* socketConnection) { // onConnected
112 LOG(DEBUG) << socketConnection->getConnectionName() << ": OnConnected";
113
114 LOG(DEBUG) << " Local: " << socketConnection->getLocalAddress().toString();
115 LOG(DEBUG) << " Peer: " << socketConnection->getRemoteAddress().toString();
116
117 if (onConnected) {
118 onConnected(socketConnection);
119 }
120 })
121 , onDisconnect([onDisconnect](SocketConnection* socketConnection) { // onDisconnect
122 LOG(DEBUG) << socketConnection->getConnectionName() << ": OnDisconnect";
123
124 LOG(DEBUG) << " Local: " << socketConnection->getLocalAddress().toString();
125 LOG(DEBUG) << " Peer: " << socketConnection->getRemoteAddress().toString();
126
127 LOG(DEBUG) << " Online Since: " << socketConnection->getOnlineSince();
128 LOG(DEBUG) << " Online Duration: " << socketConnection->getOnlineDuration();
129
130 LOG(DEBUG) << " Total Queued: " << socketConnection->getTotalQueued();
131 LOG(DEBUG) << " Total Sent: " << socketConnection->getTotalSent();
132 LOG(DEBUG) << " Write Delta: " << socketConnection->getTotalQueued() - socketConnection->getTotalSent();
133 LOG(DEBUG) << " Total Read: " << socketConnection->getTotalRead();
134 LOG(DEBUG) << " Total Processed: " << socketConnection->getTotalProcessed();
135 LOG(DEBUG) << " Read Delta: " << socketConnection->getTotalRead() - socketConnection->getTotalProcessed();
136
137 if (onDisconnect) {
138 onDisconnect(socketConnection);
139 }
140 }) {
141 }
142
143 SocketServer(const std::function<void(SocketConnection*)>& onConnect,
144 const std::function<void(SocketConnection*)>& onConnected,
145 const std::function<void(SocketConnection*)>& onDisconnect,
146 Args&&... args)
147 : SocketServer("", onConnect, onConnected, onDisconnect, std::forward<Args>(args)...) {
148 }
149
150 // VLOG() is used hire as this are log messages for the application
151 SocketServer(const std::string& name, Args&&... args)
152 : SocketServer(name, {}, {}, {}, std::forward<Args>(args)...) {
153 }
154
155 explicit SocketServer(Args&&... args)
156 : SocketServer("", std::forward<Args>(args)...) {
157 }
158
159 private:
160 void realListen(const std::function<void(const SocketAddress&, core::socket::State)>& onStatus,
161 unsigned int tries,
162 double retryTimeoutScale) const {
164 new SocketAcceptor(
166 onConnect,
169 [config = this->config,
170 onConnect = this->onConnect,
171 onConnected = this->onConnected,
172 onDisconnect = this->onDisconnect,
173 socketContextFactory = this->socketContextFactory,
174 onStatus,
175 tries,
176 retryTimeoutScale](const SocketAddress& socketAddress, core::socket::State state) mutable {
177 const bool retryFlag = (state & core::socket::State::NO_RETRY) == 0;
178 state &= ~core::socket::State::NO_RETRY;
179 onStatus(socketAddress, state);
180
181 if (retryFlag && config->getRetry() // Shall we potentially retry? In case are the ...
182 && (config->getRetryTries() == 0 || tries < config->getRetryTries()) // ... limits not reached and has an ...
183 && (state == core::socket::State::ERROR ||
184 (state == core::socket::State::FATAL && config->getRetryOnFatal()))) { // error occurred?
185 double relativeRetryTimeout =
186 config->getRetryLimit() > 0
187 ? std::min<double>(config->getRetryTimeout() * retryTimeoutScale, config->getRetryLimit())
188 : config->getRetryTimeout() * retryTimeoutScale;
189 relativeRetryTimeout -= utils::Random::getInRange(-config->getRetryJitter(), config->getRetryJitter()) *
190 relativeRetryTimeout / 100.;
191
192 LOG(INFO) << config->getInstanceName() << ": Retry listen in " << relativeRetryTimeout << " seconds";
193
195 [config,
196 onConnect,
197 onConnected,
198 onDisconnect,
199 onStatus,
200 tries,
201 retryTimeoutScale,
202 socketContextFactory]() mutable {
203 if (config->getRetry()) {
204 SocketServer(config, socketContextFactory, onConnect, onConnected, onDisconnect)
205 .realListen(onStatus, tries + 1, retryTimeoutScale * config->getRetryBase());
206 } else {
207 LOG(INFO) << config->getInstanceName() << ": Retry listen disabled during wait";
208 }
209 },
210 relativeRetryTimeout);
211 }
212 },
213 Super::config);
214 }
215 }
216
217 public:
218 void listen(const std::function<void(const SocketAddress&, core::socket::State)>& onStatus) const {
219 realListen(onStatus, 0, 1);
220 }
221
222 void listen(const SocketAddress& localAddress,
223 const std::function<void(const SocketAddress&, core::socket::State)>& onStatus) const {
224 Super::config->Local::setSocketAddress(localAddress);
225
226 listen(onStatus);
227 }
228
229 void listen(const SocketAddress& localAddress,
230 int backlog,
231 const std::function<void(const SocketAddress&, core::socket::State)>& onStatus) const {
232 Super::config->Local::setBacklog(backlog);
233
234 listen(localAddress, onStatus);
235 }
236
237 std::function<void(SocketConnection*)>& getOnConnect() {
238 return onConnect;
239 }
240
241 std::function<void(SocketConnection*)> setOnConnect(const std::function<void(SocketConnection*)>& onConnect,
242 bool initialize = false) {
243 std::function<void(SocketConnection*)> oldOnConnect = this->onConnect;
244
245 this->onConnect = initialize ? onConnect : [oldOnConnect, onConnect](SocketConnection* socketConnection) {
246 oldOnConnect(socketConnection);
247 onConnect(socketConnection);
248 };
249
250 return oldOnConnect;
251 }
252
253 std::function<void(SocketConnection*)>& getOnConnected() {
254 return onConnected();
255 }
256
257 std::function<void(SocketConnection*)> setOnConnected(const std::function<void(SocketConnection*)>& onConnected,
258 bool initialize = false) {
259 std::function<void(SocketConnection*)> oldOnConnected = this->onConnected;
260
261 this->onConnected = initialize ? onConnected : [oldOnConnected, onConnected](SocketConnection* socketConnection) {
262 oldOnConnected(socketConnection);
263 onConnected(socketConnection);
264 };
265
266 return oldOnConnected;
267 }
268
269 std::function<void(SocketConnection*)>& getOnDisconnect() {
270 return onDisconnect;
271 }
272
273 std::function<void(SocketConnection*)> setOnDisconnect(const std::function<void(SocketConnection*)>& onDisconnect,
274 bool initialize = false) {
275 std::function<void(SocketConnection*)> oldOnDisconnect = this->onDisconnect;
276
277 this->onDisconnect = initialize ? onDisconnect : [oldOnDisconnect, onDisconnect](SocketConnection* socketConnection) {
278 oldOnDisconnect(socketConnection);
279 onDisconnect(socketConnection);
280 };
281
282 return oldOnDisconnect;
283 }
284
285 std::shared_ptr<SocketContextFactory> getSocketContextFactory() {
287 }
288
289 private:
290 std::shared_ptr<SocketContextFactory> socketContextFactory;
291
292 std::function<void(SocketConnection*)> onConnect;
293 std::function<void(SocketConnection*)> onConnected;
294 std::function<void(SocketConnection*)> onDisconnect;
295 };
296
297 template <typename SocketServer, typename... Args>
298 SocketServer Server(const std::string& instanceName,
299 const std::function<void(typename SocketServer::Config&)>& configurator,
300 Args&&... socketContextFactoryArgs) {
301 const SocketServer socketServer(instanceName, std::forward<Args>(socketContextFactoryArgs)...);
302
303 configurator(socketServer.getConfig());
304
305 return socketServer;
306 }
307
308 template <typename SocketServer, typename... Args>
309 SocketServer Server(const std::string& instanceName, Args&&... socketContextFactoryArgs) {
310 return SocketServer(instanceName, std::forward<Args>(socketContextFactoryArgs)...);
311 }
312
313} // namespace core::socket::stream
314
315#endif // CORE_SOCKET_STREAM_SOCKETSERVERNEW_H
#define APPLICATION(req, res)
Definition Router.h:68
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
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
std::function< void(SocketConnection *)> & getOnConnect()
std::function< void(SocketConnection *)> setOnConnected(const std::function< void(SocketConnection *)> &onConnected, bool initialize=false)
void realListen(const std::function< void(const SocketAddress &, core::socket::State)> &onStatus, unsigned int tries, double retryTimeoutScale) 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)
SocketServer(const std::string &name, Args &&... args)
std::function< void(SocketConnection *)> onConnect
SocketServer(const std::function< void(SocketConnection *)> &onConnect, const std::function< void(SocketConnection *)> &onConnected, const std::function< void(SocketConnection *)> &onDisconnect, Args &&... args)
std::function< void(SocketConnection *)> setOnConnect(const std::function< void(SocketConnection *)> &onConnect, bool initialize=false)
std::function< void(SocketConnection *)> & getOnDisconnect()
std::function< void(SocketConnection *)> setOnDisconnect(const std::function< void(SocketConnection *)> &onDisconnect, bool initialize=false)
std::function< void(SocketConnection *)> onDisconnect
void listen(const SocketAddress &localAddress, int backlog, const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const
void listen(const SocketAddress &localAddress, const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const
std::shared_ptr< SocketContextFactory > socketContextFactory
void listen(const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const
std::function< void(SocketConnection *)> onConnected
std::function< void(SocketConnection *)> & getOnConnected()
std::shared_ptr< SocketContextFactory > getSocketContextFactory()
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)
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
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
void listen(const std::string &ipOrHostname, uint16_t port, const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const
void listen(uint16_t port, int backlog, const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const
void listen(const std::string &ipOrHostname, uint16_t port, int backlog, const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const
void listen(uint16_t port, const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const
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
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)
Definition Client.h:52