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