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/socket/Socket.h" // IWYU pragma: export
47#include "core/socket/State.h" // IWYU pragma: export
48#include "core/socket/stream/SocketContextFactory.h"
49#include "core/timer/Timer.h"
50
51#ifndef DOXYGEN_SHOULD_SKIP_THIS
52
53#include "log/Logger.h"
54#include "utils/Random.h"
55
56#include <algorithm>
57#include <functional> // IWYU pragma: export
58#include <type_traits> // IWYU pragma: export
59
60#endif /* DOXYGEN_SHOULD_SKIP_THIS */
61
62namespace core::socket::stream {
63
64 template <typename SocketAcceptorT, typename SocketContextFactoryT, typename... Args>
65 requires std::is_base_of_v<core::socket::stream::SocketContextFactory, SocketContextFactoryT>
66 class SocketServer : public core::socket::Socket<typename SocketAcceptorT::Config> {
67 private:
68 using SocketAcceptor = SocketAcceptorT;
69 using SocketContextFactory = SocketContextFactoryT;
70
71 using Super = core::socket::Socket<typename SocketAcceptor::Config>;
72
73 public:
74 using SocketConnection = typename SocketAcceptor::SocketConnection;
75 using SocketAddress = typename SocketAcceptor::SocketAddress;
76
77 SocketServer(const std::string& name,
78 const std::function<void(SocketConnection*)>& onConnect,
79 const std::function<void(SocketConnection*)>& onConnected,
80 const std::function<void(SocketConnection*)>& onDisconnect,
81 Args&&... args)
82 : Super(name)
83 , socketContextFactory(std::make_shared<SocketContextFactory>(std::forward<Args>(args)...))
84 , onConnect(onConnect)
85 , onConnected(onConnected)
86 , onDisconnect(onDisconnect) {
87 }
88
89 SocketServer(const std::function<void(SocketConnection*)>& onConnect,
90 const std::function<void(SocketConnection*)>& onConnected,
91 const std::function<void(SocketConnection*)>& onDisconnect,
92 Args&&... args)
93 : SocketServer("", onConnect, onConnected, onDisconnect, std::forward<Args>(args)...) {
94 }
95
96 // VLOG() is used hire as this are log messages for the application
97 SocketServer(const std::string& name, Args&&... args)
99 name,
100 [name](SocketConnection* socketConnection) { // onConnect
101 VLOG(2) << name << ": OnConnect";
102
103 VLOG(2) << " Local: " << socketConnection->getLocalAddress().toString();
104 VLOG(2) << " Peer: " << socketConnection->getRemoteAddress().toString();
105 },
106 [name](SocketConnection* socketConnection) { // onConnected
107 VLOG(2) << name << ": OnConnected";
108
109 VLOG(2) << " Local: " << socketConnection->getLocalAddress().toString();
110 VLOG(2) << " Peer: " << socketConnection->getRemoteAddress().toString();
111 },
112 [name](SocketConnection* socketConnection) { // onDisconnect
113 VLOG(2) << name << ": OnDisconnect";
114
115 VLOG(2) << " Local: " << socketConnection->getLocalAddress().toString();
116 VLOG(2) << " Peer: " << socketConnection->getRemoteAddress().toString();
117 },
118 std::forward<Args>(args)...) {
119 }
120
121 explicit SocketServer(Args&&... args)
122 : SocketServer("", std::forward<Args>(args)...) {
123 }
124
125 private:
126 void realListen(const std::function<void(const SocketAddress&, core::socket::State)>& onStatus,
127 unsigned int tries,
128 double retryTimeoutScale) const {
130 new SocketAcceptor(
132 onConnect,
135 [server = *this, onStatus, tries, retryTimeoutScale](const SocketAddress& socketAddress,
136 core::socket::State state) mutable {
137 const bool retry = (state & core::socket::State::NO_RETRY) == 0 &&
138 (server.getConfig().getRetryTries() == 0 || tries < server.getConfig().getRetryTries());
139
140 state &= ~core::socket::State::NO_RETRY;
141 onStatus(socketAddress, state);
142
143 if (retry) {
144 switch (state) {
145 case core::socket::State::OK:
146 server.currentOk++;
147 [[fallthrough]];
148 case core::socket::State::DISABLED:
149 break;
150 case core::socket::State::ERROR:
151 server.currentError++;
152 break;
153 case core::socket::State::FATAL:
154 server.currentFatal++;
155 break;
156 }
157
158 if ((server.currentError > 0 && server.getConfig().getRetry()) ||
159 (server.currentFatal > 0 && server.getConfig().getRetry() && server.getConfig().getRetryOnFatal())) {
160 if (server.totalOk < server.currentError + server.currentFatal) {
161 server.totalOk += server.currentOk;
162
163 double relativeRetryTimeout =
164 server.getConfig().getRetryLimit() > 0
165 ? std::min<double>(server.getConfig().getRetryTimeout() * retryTimeoutScale,
166 server.getConfig().getRetryLimit())
167 : server.getConfig().getRetryTimeout() * retryTimeoutScale;
168 relativeRetryTimeout -= utils::Random::getInRange(-server.getConfig().getRetryJitter(),
169 server.getConfig().getRetryJitter()) *
170 relativeRetryTimeout / 100.;
171
172 LOG(INFO) << server.getConfig().getInstanceName() << ": OnStatus";
173 LOG(INFO) << " retrying in " << relativeRetryTimeout << " seconds";
174
176 [server, onStatus, tries, retryTimeoutScale]() mutable {
177 server.getConfig().Local::renew();
178 server.currentOk = 0;
179 server.currentError = 0;
180 server.currentFatal = 0;
181
182 server.realListen(onStatus, tries + 1, retryTimeoutScale * server.getConfig().getRetryBase());
183 },
184 relativeRetryTimeout);
185 }
186 }
187 }
188 },
189 Super::config);
190 }
191 }
192
193 public:
194 void listen(const std::function<void(const SocketAddress&, core::socket::State)>& onStatus) const {
195 realListen(onStatus, 0, 1);
196 }
197
198 void listen(const SocketAddress& localAddress,
199 const std::function<void(const SocketAddress&, core::socket::State)>& onStatus) const {
200 Super::config->Local::setSocketAddress(localAddress);
201
202 listen(onStatus);
203 }
204
205 void listen(const SocketAddress& localAddress,
206 int backlog,
207 const std::function<void(const SocketAddress&, core::socket::State)>& onStatus) const {
208 Super::config->Local::setBacklog(backlog);
209
210 listen(localAddress, onStatus);
211 }
212
213 std::function<void(SocketConnection*)> setOnConnect(const std::function<void(SocketConnection*)>& onConnect) {
214 std::function<void(SocketConnection*)> oldOnConnect = this->onConnect;
215
216 this->onConnect = onConnect;
217
218 return oldOnConnect;
219 }
220
221 std::function<void(SocketConnection*)> setOnConnected(const std::function<void(SocketConnection*)>& onConnected) {
222 std::function<void(SocketConnection*)> oldOnConnected = this->onConnected;
223
224 this->onConnected = onConnected;
225
226 return oldOnConnected;
227 }
228
229 std::function<void(SocketConnection*)> setOnDisconnect(const std::function<void(SocketConnection*)>& onDisconnect) {
230 std::function<void(SocketConnection*)> oldOnDisconnect = this->onDisconnect;
231
232 this->onDisconnect = onDisconnect;
233
234 return oldOnDisconnect;
235 }
236
237 std::shared_ptr<SocketContextFactory> getSocketContextFactory() {
239 }
240
241 private:
242 std::shared_ptr<SocketContextFactory> socketContextFactory;
243
244 std::function<void(SocketConnection*)> onConnect;
245 std::function<void(SocketConnection*)> onConnected;
246 std::function<void(SocketConnection*)> onDisconnect;
247
248 std::size_t totalOk = 0;
249 std::size_t currentOk = 0;
250 std::size_t currentError = 0;
251 std::size_t currentFatal = 0;
252 };
253
254 template <typename SocketServer, typename... Args>
255 SocketServer Server(const std::string& instanceName,
256 const std::function<void(typename SocketServer::Config&)>& configurator,
257 Args&&... socketContextFactoryArgs) {
258 const SocketServer socketServer(instanceName, std::forward<Args>(socketContextFactoryArgs)...);
259
260 configurator(socketServer.getConfig());
261
262 return socketServer;
263 }
264
265 template <typename SocketServer, typename... Args>
266 SocketServer Server(const std::string& instanceName, Args&&... socketContextFactoryArgs) {
267 return SocketServer(instanceName, std::forward<Args>(socketContextFactoryArgs)...);
268 }
269
270} // namespace core::socket::stream
271
272#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:60
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 *)> setOnConnected(const std::function< void(SocketConnection *)> &onConnected)
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 *)> 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
std::function< void(SocketConnection *)> setOnConnect(const std::function< void(SocketConnection *)> &onConnect)
void listen(const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const
std::function< void(SocketConnection *)> onConnected
std::shared_ptr< SocketContextFactory > getSocketContextFactory()
std::function< void(SocketConnection *)> setOnDisconnect(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:165
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