SNode.C
Loading...
Searching...
No Matches
SocketClient.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, 2026
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_SOCKETCLIENT_H
43#define CORE_SOCKET_STREAM_SOCKETCLIENT_H
44
45#include "core/EventReceiver.h"
46#include "core/SNodeC.h"
47#include "core/socket/Socket.h" // IWYU pragma: export
48#include "core/socket/State.h" // IWYU pragma: export
49#include "core/socket/stream/ClientFlowController.h" // IWYU pragma: export
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 /** Sequence diagram showing how a connect to a peer is performed.
66 @startuml
67 !include core/socket/stream/pu/SocketClient.pu
68 @enduml
69 */
70 template <typename SocketConnectorT, typename SocketContextFactoryT, typename... Args>
71 requires std::is_base_of_v<core::eventreceiver::ConnectEventReceiver, SocketConnectorT> &&
72 std::is_base_of_v<core::socket::stream::SocketContextFactory, SocketContextFactoryT>
73 class SocketClient : public core::socket::Socket<typename SocketConnectorT::Config> {
74 private:
75 using SocketConnector = SocketConnectorT;
76 using SocketContextFactory = SocketContextFactoryT;
77
78 using Super = core::socket::Socket<typename SocketConnector::Config>;
79
80 public:
83 using Config = typename SocketConnector::Config;
84
85 private:
86 struct Context {
87 Context(Config* config,
88 const std::shared_ptr<SocketContextFactory>& socketContextFactory,
89 const std::function<void(SocketConnection*)>& onConnect,
90 const std::function<void(SocketConnection*)>& onConnected,
91 const std::function<void(SocketConnection*)>& onDisconnect)
92 : flowController(config)
93 , socketContextFactory(socketContextFactory)
94 , onConnect(onConnect)
95 , onConnected(onConnected)
96 , onDisconnect(onDisconnect) {
97 }
98
100
102
103 std::function<void(SocketConnection*)> onConnect;
104 std::function<void(SocketConnection*)> onConnected;
105 std::function<void(SocketConnection*)> onDisconnect;
106 };
107
108 SocketClient(const std::shared_ptr<Config>& config, const std::shared_ptr<Context>& sharedContext)
109 : Super(config)
110 , sharedContext(sharedContext) {
111 }
112
113 public:
114 SocketClient(const std::string& name,
115 const std::function<void(SocketConnection*)>& onConnect,
116 const std::function<void(SocketConnection*)>& onConnected,
117 const std::function<void(SocketConnection*)>& onDisconnect,
118 Args&&... args)
119 : Super(name)
120 , sharedContext(std::make_shared<Context>(
121 this->config.get(),
122 std::make_shared<SocketContextFactory>(std::forward<Args>(args)...),
123 [onConnect](SocketConnection* socketConnection) { // onConnect
124 LOG(DEBUG) << socketConnection->getConnectionName() << ": OnConnect";
125
126 LOG(DEBUG) << " Local: " << socketConnection->getLocalAddress().toString();
127 LOG(DEBUG) << " Peer: " << socketConnection->getRemoteAddress().toString();
128
129 if (onConnect) {
130 onConnect(socketConnection);
131 }
132 },
133 [onConnected](SocketConnection* socketConnection) { // onConnected
134 LOG(DEBUG) << socketConnection->getConnectionName() << ": OnConnected";
135
136 LOG(DEBUG) << " Local: " << socketConnection->getLocalAddress().toString();
137 LOG(DEBUG) << " Peer: " << socketConnection->getRemoteAddress().toString();
138
139 if (onConnected) {
140 onConnected(socketConnection);
141 }
142 },
143 [onDisconnect](SocketConnection* socketConnection) { // onDisconnect
144 LOG(DEBUG) << socketConnection->getConnectionName() << ": OnDisconnect";
145
146 LOG(DEBUG) << " Local: " << socketConnection->getLocalAddress().toString();
147 LOG(DEBUG) << " Peer: " << socketConnection->getRemoteAddress().toString();
148
149 LOG(DEBUG) << " Online Since: " << socketConnection->getOnlineSince();
150 LOG(DEBUG) << " Online Duration: " << socketConnection->getOnlineDuration();
151
152 LOG(DEBUG) << " Total Queued: " << socketConnection->getTotalQueued();
153 LOG(DEBUG) << " Total Sent: " << socketConnection->getTotalSent();
154 LOG(DEBUG) << " Write Delta: " << socketConnection->getTotalQueued() - socketConnection->getTotalSent();
155 LOG(DEBUG) << " Total Read: " << socketConnection->getTotalRead();
156 LOG(DEBUG) << " Total Processed: " << socketConnection->getTotalProcessed();
157 LOG(DEBUG) << " Read Delta: " << socketConnection->getTotalRead() - socketConnection->getTotalProcessed();
158
159 if (onDisconnect) {
160 onDisconnect(socketConnection);
161 }
162 })) {
163 }
164
165 SocketClient(const std::function<void(SocketConnection*)>& onConnect,
166 const std::function<void(SocketConnection*)>& onConnected,
167 const std::function<void(SocketConnection*)>& onDisconnect,
168 Args&&... args)
169 : SocketClient("", onConnect, onConnected, onDisconnect, std::forward<Args>(args)...) {
170 }
171
172 SocketClient(const std::string& name, Args&&... args)
173 : SocketClient(name, {}, {}, {}, std::forward<Args>(args)...) {
174 }
175
176 explicit SocketClient(Args&&... args)
177 : SocketClient("", std::forward<Args>(args)...) {
178 }
179
180 private:
181 const SocketClient& realConnect(const std::function<void(const SocketAddress&, core::socket::State)>& onStatus,
182 unsigned int tries,
183 double retryTimeoutScale) const {
184 sharedContext->flowController.startFlow(
185 [config = this->config, sharedContext = this->sharedContext, onStatus, tries, retryTimeoutScale] {
186 if (config->Instance::getParent() != nullptr || !config->Instance::getRequired()) {
187 LOG(DEBUG) << config->getInstanceName() << ": Initiating connect";
188
190 new SocketConnector(
191 sharedContext->socketContextFactory,
192 sharedContext->onConnect,
193 sharedContext->onConnected,
194 [config, sharedContext, onStatus](SocketConnection* socketConnection) {
195 sharedContext->onDisconnect(socketConnection);
196
197 if (config->getReconnect() && sharedContext->flowController.isReconnectEnabled() &&
199 double relativeReconnectTimeout = config->getReconnectTime();
200
201 LOG(INFO)
202 << config->getInstanceName() << ": Reconnect in " << relativeReconnectTimeout << " seconds";
203
204 sharedContext->flowController.armReconnectTimer(
205 relativeReconnectTimeout, [config, sharedContext, /*generation,*/ onStatus]() {
206 if (!sharedContext->flowController.isReconnectEnabled()) {
207 return;
208 }
209 if (config->getReconnect()) {
210 sharedContext->flowController.reportFlowReconnect();
211 SocketClient(config, sharedContext).realConnect(onStatus, 0, config->getRetryBase());
212 } else {
213 LOG(INFO) << config->getInstanceName() << ": Reconnect disabled during wait";
214 }
215 });
216 }
217 },
218 [sharedContext](core::eventreceiver::ConnectEventReceiver* connectEventReceiver) {
219 sharedContext->flowController.observeConnectEventReceiver(connectEventReceiver);
220 },
221 [config, sharedContext, onStatus, tries, retryTimeoutScale](const SocketAddress& socketAddress,
222 core::socket::State state) {
223 const bool retryFlag = (state & core::socket::State::NO_RETRY) == 0;
224 state &= ~core::socket::State::NO_RETRY;
225 onStatus(socketAddress, state);
226
227 if (retryFlag && config->getRetry() // Shall we potentially retry? In case are the ...
228 && sharedContext->flowController.isRetryEnabled() &&
229 (config->getRetryTries() == 0 ||
230 tries < config->getRetryTries()) // ... limits not reached and has an ...
231 && (state == core::socket::State::ERROR ||
232 (state == core::socket::State::FATAL && config->getRetryOnFatal()))) { // error occurred?
233 double relativeRetryTimeout =
234 config->getRetryLimit() > 0
235 ? std::min<double>(config->getRetryTimeout() * retryTimeoutScale, config->getRetryLimit())
236 : config->getRetryTimeout() * retryTimeoutScale;
237 relativeRetryTimeout -=
238 utils::Random::getInRange(-config->getRetryJitter(), config->getRetryJitter()) *
239 relativeRetryTimeout / 100.;
240
241 LOG(INFO)
242 << config->getInstanceName() << ": Retry connect in " << relativeRetryTimeout << " seconds";
243
244 sharedContext->flowController.armRetryTimer(
245 relativeRetryTimeout,
246 [config,
247 sharedContext,
248 /*generation,*/ onStatus,
249 tries,
250 retryTimeoutScale]() {
251 if (!sharedContext->flowController.isRetryEnabled()) {
252 return;
253 }
254 if (config->getRetry()) {
255 sharedContext->flowController.reportFlowRetry();
256 SocketClient(config, sharedContext)
257 .realConnect(onStatus, tries + 1, retryTimeoutScale * config->getRetryBase());
258 } else {
259 LOG(INFO) << config->getInstanceName() << ": Retry connect disabled during wait";
260 }
261 });
262 }
263 },
264 config);
265 }
266 } else {
267 LOG(FATAL) << config->getInstanceName() << " required";
268 }
269 });
270
271 return *this;
272 }
273
274 public:
275 const SocketClient& connect(const std::function<void(const SocketAddress&, core::socket::State)>& onStatus) const {
276 return realConnect(onStatus, 0, 1);
277 }
278
279 const SocketClient& connect(const SocketAddress& remoteAddress,
280 const std::function<void(const SocketAddress&, core::socket::State)>& onStatus) const {
281 Super::config->Remote::setSocketAddress(remoteAddress);
282
283 return connect(onStatus);
284 }
285
286 const SocketClient& connect(const SocketAddress& remoteAddress,
287 const SocketAddress& localAddress,
288 const std::function<void(const SocketAddress&, core::socket::State)>& onStatus) const {
289 Super::config->Local::setSocketAddress(localAddress);
290
291 return connect(remoteAddress, onStatus);
292 }
293
294 std::function<void(SocketConnection*)>& getOnConnect() {
295 return sharedContext->onConnect;
296 }
297
298 const SocketClient& setOnConnect(const std::function<void(SocketConnection*)>& onConnect, bool initialize = false) const {
299 sharedContext->onConnect =
300 initialize ? onConnect : [oldOnConnect = sharedContext->onConnect, onConnect](SocketConnection* socketConnection) {
301 oldOnConnect(socketConnection);
302 onConnect(socketConnection);
303 };
304
305 return *this;
306 }
307
308 std::function<void(SocketConnection*)>& getOnConnected() const {
309 return sharedContext->onConnected;
310 }
311
312 const SocketClient& setOnConnected(const std::function<void(SocketConnection*)>& onConnected, bool initialize = false) const {
313 sharedContext->onConnected =
314 initialize ? onConnected : [oldOnConnected = sharedContext->onConnected, onConnected](SocketConnection* socketConnection) {
315 oldOnConnected(socketConnection);
316 onConnected(socketConnection);
317 };
318
319 return *this;
320 }
321
322 std::function<void(SocketConnection*)>& getOnDisconnect() const {
323 return sharedContext->onDisconnect;
324 }
325
326 const SocketClient& setOnDisconnect(const std::function<void(SocketConnection*)>& onDisconnect, bool initialize = false) const {
327 sharedContext->onDisconnect =
328 initialize ? onDisconnect
329 : [oldOnDisconnect = sharedContext->onDisconnect, onDisconnect](SocketConnection* socketConnection) {
330 oldOnDisconnect(socketConnection);
331 onDisconnect(socketConnection);
332 };
333
334 return *this;
335 }
336
338 return &sharedContext->flowController;
339 }
340
342 return sharedContext->socketContextFactory;
343 }
344
345 private:
346 std::shared_ptr<Context> sharedContext;
347 };
348
349 template <typename SocketClient, typename... Args>
350 SocketClient Client(const std::string& instanceName,
351 const std::function<void(typename SocketClient::Config*)>& configurator,
352 Args&&... socketContextFactoryArgs) {
353 const SocketClient socketClient(instanceName, std::forward<Args>(socketContextFactoryArgs)...);
354
355 configurator(socketClient.getConfig());
356
357 return socketClient;
358 }
359
360 template <typename SocketClient, typename... Args>
361 SocketClient Client(const std::string& instanceName, Args&&... socketContextFactoryArgs) {
362 return SocketClient(instanceName, std::forward<Args>(socketContextFactoryArgs)...);
363 }
364
365} // namespace core::socket::stream
366
367#endif // CORE_SOCKET_STREAM_SOCKETCLIENT_H
#define LOG(level)
Definition Logger.h:148
#define VLOG(level)
Definition Logger.h:164
core::socket::stream::SocketContext * create(core::socket::stream::SocketConnection *socketConnection) override
web::http::client::ResponseParser * responseParser
SimpleSocketProtocol(core::socket::stream::SocketConnection *socketConnection)
void onReadError(int errnum) override
void onWriteError(int errnum) override
std::size_t onReceivedFromPeer() override
static void init(int argc, char *argv[])
Definition SNodeC.cpp:54
static State state()
Definition SNodeC.cpp:76
static int start(const utils::Timeval &timeOut={LONG_MAX, 0})
Definition SNodeC.cpp:60
ConnectEventReceiver(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
SocketClient(const std::string &name, const std::function< void(SocketConnection *)> &onConnect, const std::function< void(SocketConnection *)> &onConnected, const std::function< void(SocketConnection *)> &onDisconnect, Args &&... args)
const SocketClient & connect(const SocketAddress &remoteAddress, const SocketAddress &localAddress, const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const
std::function< void(SocketConnection *)> & getOnDisconnect() const
SocketClient(const std::string &name, Args &&... args)
SocketContextFactoryT SocketContextFactory
const SocketClient & connect(const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const
typename SocketConnector::SocketAddress SocketAddress
const SocketClient & setOnConnect(const std::function< void(SocketConnection *)> &onConnect, bool initialize=false) const
std::function< void(SocketConnection *)> & getOnConnected() const
const SocketClient & realConnect(const std::function< void(const SocketAddress &, core::socket::State)> &onStatus, unsigned int tries, double retryTimeoutScale) const
typename SocketConnector::SocketConnection SocketConnection
std::shared_ptr< SocketContextFactory > getSocketContextFactory() const
SocketClient(const std::function< void(SocketConnection *)> &onConnect, const std::function< void(SocketConnection *)> &onConnected, const std::function< void(SocketConnection *)> &onDisconnect, Args &&... args)
typename SocketConnector::Config Config
const SocketClient & setOnDisconnect(const std::function< void(SocketConnection *)> &onDisconnect, bool initialize=false) const
core::socket::Socket< typename SocketConnector::Config > Super
ClientFlowController * getFlowController() const
SocketClient(const std::shared_ptr< Config > &config, const std::shared_ptr< Context > &sharedContext)
std::function< void(SocketConnection *)> & getOnConnect()
const SocketClient & connect(const SocketAddress &remoteAddress, const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const
std::shared_ptr< Context > sharedContext
const SocketClient & setOnConnected(const std::function< void(SocketConnection *)> &onConnected, bool initialize=false) const
const SocketAddress & getRemoteAddress() const final
const SocketAddress & getLocalAddress() const final
void sendToPeer(const std::string &data)
SocketConnector(const std::function< void(SocketConnection *)> &onConnect, const std::function< void(SocketConnection *)> &onConnected, const std::function< void(SocketConnection *)> &onDisconnect, const std::function< void(core::eventreceiver::ConnectEventReceiver *)> &onInitState, const std::function< void(const SocketAddress &, core::socket::State)> &onStatus, const std::shared_ptr< Config > &config)
typename PhysicalClientSocket::SocketAddress SocketAddress
SocketConnector(const SocketConnector &socketConnector)
std::function< void(const SocketAddress &, core::socket::State)> onStatus
SocketConnectionT< PhysicalClientSocket, Config > SocketConnection
std::function< void(SocketConnection *)> onConnected
std::function< void(SocketConnection *)> onDisconnect
PhysicalSocketClientT PhysicalClientSocket
std::function< void(core::eventreceiver::ConnectEventReceiver *)> onInitState
std::function< void(SocketConnection *)> onConnect
SocketContext(core::socket::stream::SocketConnection *socketConnection)
void onReadError(int errnum) override
void onWriteError(int errnum) override
typename Super::SocketAddress SocketAddress
SocketConnector(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(core::eventreceiver::ConnectEventReceiver *)> &onInitState, const std::function< void(const SocketAddress &, core::socket::State)> &onStatus, const std::shared_ptr< Config > &config)
core::socket::stream::SocketConnector< PhysicalClientSocketT, ConfigT, core::socket::stream::legacy::SocketConnection > Super
SocketConnector(const SocketConnector &socketConnector)
typename Super::SocketConnection SocketConnection
LogMessage(Level level, int verboseLevel=-1, bool withErrno=false)
Definition Logger.cpp:280
const std::string & getInstanceName() const
SocketAddress(const std::string &ipOrHostname, uint16_t port)
std::string getCanonName() const
std::string toString(bool expanded=true) const override
void init(const Hints &hints={.aiFlags=0,.aiSockType=0,.aiProtocol=0})
const Super & connect(const std::string &ipOrHostname, uint16_t port, const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const
core::socket::stream:: SocketClient< SocketConnectorT< net::in::phy::stream::PhysicalSocketClient, ConfigSocketClientT >, SocketContextFactoryT, Args... > Super
const Super & connect(const std::string &ipOrHostname, uint16_t port, const std::string &bindHost, uint16_t bindPort, const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const
const Super & connect(const std::string &ipOrHostname, uint16_t port, const std::string &bindHost, const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const
const Super & connect(const std::string &ipOrHostname, uint16_t port, uint16_t bindPort, const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const
static double getInRange(double ll, double ul)
Definition Random.cpp:52
std::size_t parse()
Definition Parser.cpp:125
ResponseParser(core::socket::stream::SocketContext *socketContext, const std::function< void()> &onResponseStart, const std::function< void(Response &)> &onResponseParsed, const std::function< void(int, const std::string &)> &onResponseParseError)
int main(int argc, char *argv[])
static web::http::client::ResponseParser * getResponseParser(core::socket::stream::SocketContext *socketContext)
SocketClient Client(const std::string &instanceName, const std::function< void(typename SocketClient::Config *)> &configurator, Args &&... socketContextFactoryArgs)
SocketClient Client(const std::string &instanceName, Args &&... socketContextFactoryArgs)
State
Definition State.h:51
@ RUNNING
Definition State.h:51
@ INITIALIZED
Definition State.h:51
State eventLoopState()
Definition State.cpp:52
net::in::stream::legacy::SocketClient< apps::http::SimpleSocketProtocolFactory > SocketClient
SocketClient::SocketAddress SocketAddress
SocketClient::SocketConnection SocketConnection
SocketClient getLegacyClient()
SocketClient< SocketContextFactory, SocketContextFactoryArgs... > Client(const std::string &instanceName, SocketContextFactoryArgs &&... socketContextFactoryArgs)
net::in::stream::SocketClient< core::socket::stream::legacy::SocketConnector, net::in::stream::legacy::config::ConfigSocketClient, SocketContextFactoryT, Args... > SocketClient
SocketClient< SocketContextFactory, SocketContextFactoryArgs... > Client(const std::string &instanceName, const std::function< void(net::in::stream::legacy::config::ConfigSocketClient &)> &configurator, SocketContextFactoryArgs &&... socketContextFactoryArgs)
net::in::stream::SocketClient< core::socket::stream::tls::SocketConnector, net::in::stream::tls::config::ConfigSocketClient, SocketContextFactoryT, Args... > SocketClient
net::in::stream::tls::SocketClient< apps::http::SimpleSocketProtocolFactory > SocketClient
SocketClient getClient()
SocketClient::SocketConnection SocketConnection
SocketClient::SocketAddress SocketAddress
std::function< void(SocketConnection *)> onConnect
std::shared_ptr< SocketContextFactory > socketContextFactory
std::function< void(SocketConnection *)> onConnected
std::function< void(SocketConnection *)> onDisconnect
Context(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)