SNode.C
Loading...
Searching...
No Matches
core::socket::stream::SocketClient< SocketConnectorT, SocketContextFactoryT, Args > Class Template Reference

#include <SocketClient.h>

Inheritance diagram for core::socket::stream::SocketClient< SocketConnectorT, SocketContextFactoryT, Args >:
Collaboration diagram for core::socket::stream::SocketClient< SocketConnectorT, SocketContextFactoryT, Args >:

Classes

struct  Context

Public Types

using SocketConnection = typename SocketConnector::SocketConnection
using SocketAddress = typename SocketConnector::SocketAddress
using Config = typename SocketConnector::Config
Public Types inherited from core::socket::Socket< SocketConnectorT::Config >
using Config

Public Member Functions

 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)
 SocketClient (const std::function< void(SocketConnection *)> &onConnect, const std::function< void(SocketConnection *)> &onConnected, const std::function< void(SocketConnection *)> &onDisconnect, Args &&... args)
 SocketClient (const std::string &name, Args &&... args)
 SocketClient (Args &&... args)
const SocketClientconnect (const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const
const SocketClientconnect (const SocketAddress &remoteAddress, const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const
const SocketClientconnect (const SocketAddress &remoteAddress, const SocketAddress &localAddress, const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const
std::function< void(SocketConnection *)> & getOnConnect ()
const SocketClientsetOnConnect (const std::function< void(SocketConnection *)> &onConnect, bool initialize=false) const
std::function< void(SocketConnection *)> & getOnConnected () const
const SocketClientsetOnConnected (const std::function< void(SocketConnection *)> &onConnected, bool initialize=false) const
std::function< void(SocketConnection *)> & getOnDisconnect () const
const SocketClientsetOnDisconnect (const std::function< void(SocketConnection *)> &onDisconnect, bool initialize=false) const
AutoConnectControlgetAutoConnectController () const
std::function< void(core::eventreceiver::ConnectEventReceiver *)> & getOnInitState () const
const SocketClientsetOnInitState (const std::function< void(core::eventreceiver::ConnectEventReceiver *)> &onInitState, bool initialize=false) const
std::shared_ptr< SocketContextFactorygetSocketContextFactory () const
Public Member Functions inherited from core::socket::Socket< SocketConnectorT::Config >
 Socket (const std::string &name)
virtual ~Socket ()
ConfiggetConfig () const

Private Types

using SocketConnector = SocketConnectorT
using SocketContextFactory = SocketContextFactoryT
using Super = core::socket::Socket<typename SocketConnector::Config>

Private Member Functions

 SocketClient (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)
 SocketClient (const std::shared_ptr< Config > &config, const std::shared_ptr< Context > &sharedContext)
const SocketClientrealConnect (const std::function< void(const SocketAddress &, core::socket::State)> &onStatus, unsigned int tries, double retryTimeoutScale) const

Private Attributes

std::shared_ptr< ContextsharedContext

Additional Inherited Members

Protected Member Functions inherited from core::socket::Socket< SocketConnectorT::Config >
Socketoperator= (const Socket &)=default
Protected Attributes inherited from core::socket::Socket< SocketConnectorT::Config >
const std::shared_ptr< Configconfig

Detailed Description

template<typename SocketConnectorT, typename SocketContextFactoryT, typename... Args>
requires std::is_base_of_v<core::eventreceiver::ConnectEventReceiver, SocketConnectorT> && std::is_base_of_v<core::socket::stream::SocketContextFactory, SocketContextFactoryT>
class core::socket::stream::SocketClient< SocketConnectorT, SocketContextFactoryT, Args >

Sequence diagram showing how a connect to a peer is performed.

Definition at line 73 of file SocketClient.h.

Member Typedef Documentation

◆ Config

template<typename SocketConnectorT, typename SocketContextFactoryT, typename... Args>
using core::socket::stream::SocketClient< SocketConnectorT, SocketContextFactoryT, Args >::Config = typename SocketConnector::Config

Definition at line 83 of file SocketClient.h.

◆ SocketAddress

template<typename SocketConnectorT, typename SocketContextFactoryT, typename... Args>
using core::socket::stream::SocketClient< SocketConnectorT, SocketContextFactoryT, Args >::SocketAddress = typename SocketConnector::SocketAddress

Definition at line 82 of file SocketClient.h.

◆ SocketConnection

template<typename SocketConnectorT, typename SocketContextFactoryT, typename... Args>
using core::socket::stream::SocketClient< SocketConnectorT, SocketContextFactoryT, Args >::SocketConnection = typename SocketConnector::SocketConnection

Definition at line 81 of file SocketClient.h.

◆ SocketConnector

template<typename SocketConnectorT, typename SocketContextFactoryT, typename... Args>
using core::socket::stream::SocketClient< SocketConnectorT, SocketContextFactoryT, Args >::SocketConnector = SocketConnectorT
private

Definition at line 75 of file SocketClient.h.

◆ SocketContextFactory

template<typename SocketConnectorT, typename SocketContextFactoryT, typename... Args>
using core::socket::stream::SocketClient< SocketConnectorT, SocketContextFactoryT, Args >::SocketContextFactory = SocketContextFactoryT
private

Definition at line 76 of file SocketClient.h.

◆ Super

template<typename SocketConnectorT, typename SocketContextFactoryT, typename... Args>
using core::socket::stream::SocketClient< SocketConnectorT, SocketContextFactoryT, Args >::Super = core::socket::Socket<typename SocketConnector::Config>
private

Definition at line 78 of file SocketClient.h.

Constructor & Destructor Documentation

◆ SocketClient() [1/6]

template<typename SocketConnectorT, typename SocketContextFactoryT, typename... Args>
core::socket::stream::SocketClient< SocketConnectorT, SocketContextFactoryT, Args >::SocketClient ( 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 )
inlineprivate

Definition at line 109 of file SocketClient.h.

◆ SocketClient() [2/6]

template<typename SocketConnectorT, typename SocketContextFactoryT, typename... Args>
core::socket::stream::SocketClient< SocketConnectorT, SocketContextFactoryT, Args >::SocketClient ( const std::shared_ptr< Config > & config,
const std::shared_ptr< Context > & sharedContext )
inlineprivate

Definition at line 118 of file SocketClient.h.

119 : Super(config)
121 }

◆ SocketClient() [3/6]

template<typename SocketConnectorT, typename SocketContextFactoryT, typename... Args>
core::socket::stream::SocketClient< SocketConnectorT, SocketContextFactoryT, Args >::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 )
inline

Definition at line 124 of file SocketClient.h.

129 : Super(name)
133 LOG(DEBUG) << socketConnection->getConnectionName() << ": OnConnect";
134
135 LOG(DEBUG) << " Local: " << socketConnection->getLocalAddress().toString();
136 LOG(DEBUG) << " Peer: " << socketConnection->getRemoteAddress().toString();
137
138 if (onConnect) {
140 }
141 },
143 LOG(DEBUG) << socketConnection->getConnectionName() << ": OnConnected";
144
145 LOG(DEBUG) << " Local: " << socketConnection->getLocalAddress().toString();
146 LOG(DEBUG) << " Peer: " << socketConnection->getRemoteAddress().toString();
147
148 if (onConnected) {
150 }
151 },
153 LOG(DEBUG) << socketConnection->getConnectionName() << ": OnDisconnect";
154
155 LOG(DEBUG) << " Local: " << socketConnection->getLocalAddress().toString();
156 LOG(DEBUG) << " Peer: " << socketConnection->getRemoteAddress().toString();
157
158 LOG(DEBUG) << " Online Since: " << socketConnection->getOnlineSince();
159 LOG(DEBUG) << " Online Duration: " << socketConnection->getOnlineDuration();
160
161 LOG(DEBUG) << " Total Queued: " << socketConnection->getTotalQueued();
162 LOG(DEBUG) << " Total Sent: " << socketConnection->getTotalSent();
163 LOG(DEBUG) << " Write Delta: " << socketConnection->getTotalQueued() - socketConnection->getTotalSent();
164 LOG(DEBUG) << " Total Read: " << socketConnection->getTotalRead();
165 LOG(DEBUG) << " Total Processed: " << socketConnection->getTotalProcessed();
166 LOG(DEBUG) << " Read Delta: " << socketConnection->getTotalRead() - socketConnection->getTotalProcessed();
167
168 if (onDisconnect) {
170 }
171 })) {
172 }
#define LOG(level)
Definition Logger.h:148
typename SocketConnector::SocketConnection SocketConnection

◆ SocketClient() [4/6]

template<typename SocketConnectorT, typename SocketContextFactoryT, typename... Args>
core::socket::stream::SocketClient< SocketConnectorT, SocketContextFactoryT, Args >::SocketClient ( const std::function< void(SocketConnection *)> & onConnect,
const std::function< void(SocketConnection *)> & onConnected,
const std::function< void(SocketConnection *)> & onDisconnect,
Args &&... args )
inline

Definition at line 174 of file SocketClient.h.

179 }
SocketClient(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)

◆ SocketClient() [5/6]

template<typename SocketConnectorT, typename SocketContextFactoryT, typename... Args>
core::socket::stream::SocketClient< SocketConnectorT, SocketContextFactoryT, Args >::SocketClient ( const std::string & name,
Args &&... args )
inline

Definition at line 181 of file SocketClient.h.

182 : SocketClient(name, {}, {}, {}, std::forward<Args>(args)...) {
183 }

◆ SocketClient() [6/6]

template<typename SocketConnectorT, typename SocketContextFactoryT, typename... Args>
core::socket::stream::SocketClient< SocketConnectorT, SocketContextFactoryT, Args >::SocketClient ( Args &&... args)
inlineexplicit

Definition at line 185 of file SocketClient.h.

187 }

Member Function Documentation

◆ connect() [1/3]

template<typename SocketConnectorT, typename SocketContextFactoryT, typename... Args>
const SocketClient & core::socket::stream::SocketClient< SocketConnectorT, SocketContextFactoryT, Args >::connect ( const SocketAddress & remoteAddress,
const SocketAddress & localAddress,
const std::function< void(const SocketAddress &, core::socket::State)> & onStatus ) const
inline

Definition at line 292 of file SocketClient.h.

294 {
295 Super::config->Local::setSocketAddress(localAddress);
296
298 }
const SocketClient & connect(const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const

◆ connect() [2/3]

template<typename SocketConnectorT, typename SocketContextFactoryT, typename... Args>
const SocketClient & core::socket::stream::SocketClient< SocketConnectorT, SocketContextFactoryT, Args >::connect ( const SocketAddress & remoteAddress,
const std::function< void(const SocketAddress &, core::socket::State)> & onStatus ) const
inline

Definition at line 285 of file SocketClient.h.

286 {
287 Super::config->Remote::setSocketAddress(remoteAddress);
288
289 return connect(onStatus);
290 }

Referenced by tls::getClient(), legacy::getLegacyClient(), and main().

Here is the caller graph for this function:

◆ connect() [3/3]

template<typename SocketConnectorT, typename SocketContextFactoryT, typename... Args>
const SocketClient & core::socket::stream::SocketClient< SocketConnectorT, SocketContextFactoryT, Args >::connect ( const std::function< void(const SocketAddress &, core::socket::State)> & onStatus) const
inline

Definition at line 281 of file SocketClient.h.

281 {
282 return realConnect(onStatus, 0, 1);
283 }
const SocketClient & realConnect(const std::function< void(const SocketAddress &, core::socket::State)> &onStatus, unsigned int tries, double retryTimeoutScale) const

Referenced by main().

Here is the caller graph for this function:

◆ getAutoConnectController()

template<typename SocketConnectorT, typename SocketContextFactoryT, typename... Args>
AutoConnectControl * core::socket::stream::SocketClient< SocketConnectorT, SocketContextFactoryT, Args >::getAutoConnectController ( ) const
inline

Definition at line 343 of file SocketClient.h.

343 {
344 return sharedContext->autoConnectControl.get();
345 }

◆ getOnConnect()

template<typename SocketConnectorT, typename SocketContextFactoryT, typename... Args>
std::function< void(SocketConnection *)> & core::socket::stream::SocketClient< SocketConnectorT, SocketContextFactoryT, Args >::getOnConnect ( )
inline

Definition at line 300 of file SocketClient.h.

300 {
301 return sharedContext->onConnect;
302 }

◆ getOnConnected()

template<typename SocketConnectorT, typename SocketContextFactoryT, typename... Args>
std::function< void(SocketConnection *)> & core::socket::stream::SocketClient< SocketConnectorT, SocketContextFactoryT, Args >::getOnConnected ( ) const
inline

Definition at line 314 of file SocketClient.h.

314 {
315 return sharedContext->onConnected;
316 }

◆ getOnDisconnect()

template<typename SocketConnectorT, typename SocketContextFactoryT, typename... Args>
std::function< void(SocketConnection *)> & core::socket::stream::SocketClient< SocketConnectorT, SocketContextFactoryT, Args >::getOnDisconnect ( ) const
inline

Definition at line 328 of file SocketClient.h.

328 {
329 return sharedContext->onDisconnect;
330 }

◆ getOnInitState()

template<typename SocketConnectorT, typename SocketContextFactoryT, typename... Args>
std::function< void(core::eventreceiver::ConnectEventReceiver *)> & core::socket::stream::SocketClient< SocketConnectorT, SocketContextFactoryT, Args >::getOnInitState ( ) const
inline

Definition at line 347 of file SocketClient.h.

347 {
348 return sharedContext->onInitState;
349 }

◆ getSocketContextFactory()

template<typename SocketConnectorT, typename SocketContextFactoryT, typename... Args>
std::shared_ptr< SocketContextFactory > core::socket::stream::SocketClient< SocketConnectorT, SocketContextFactoryT, Args >::getSocketContextFactory ( ) const
inline

Definition at line 363 of file SocketClient.h.

363 {
364 return sharedContext->socketContextFactory;
365 }

◆ realConnect()

template<typename SocketConnectorT, typename SocketContextFactoryT, typename... Args>
const SocketClient & core::socket::stream::SocketClient< SocketConnectorT, SocketContextFactoryT, Args >::realConnect ( const std::function< void(const SocketAddress &, core::socket::State)> & onStatus,
unsigned int tries,
double retryTimeoutScale ) const
inlineprivate

Definition at line 190 of file SocketClient.h.

192 {
194
196 if (config->Instance::getParent() != nullptr || !config->Instance::getRequired()) {
197 LOG(DEBUG) << config->getInstanceName() << ": Initiating connect";
198
200 new SocketConnector(
201 sharedContext->socketContextFactory,
202 sharedContext->onConnect,
203 sharedContext->onConnected,
205 sharedContext->onDisconnect(socketConnection);
206
207 if (config->getReconnect() && sharedContext->autoConnectControl->isReconnectEnabled() &&
208 core::eventLoopState() == core::State::RUNNING) {
209 double relativeReconnectTimeout = config->getReconnectTime();
210
211 LOG(INFO)
212 << config->getInstanceName() << ": Reconnect in " << relativeReconnectTimeout << " seconds";
213
214 sharedContext->autoConnectControl->armReconnectTimer(
215 relativeReconnectTimeout, [config, sharedContext, /*generation,*/ onStatus]() {
216 if (!sharedContext->autoConnectControl->isReconnectEnabled()) {
217 return;
218 }
219 if (config->getReconnect()) {
220 SocketClient(config, sharedContext).realConnect(onStatus, 0, config->getRetryBase());
221 } else {
222 LOG(INFO) << config->getInstanceName() << ": Reconnect disabled during wait";
223 }
224 });
225 }
226 },
227 sharedContext->onInitState,
230 const bool retryFlag = (state & core::socket::State::NO_RETRY) == 0;
233
234 if (retryFlag && config->getRetry() // Shall we potentially retry? In case are the ...
235 && sharedContext->autoConnectControl->isRetryEnabled() &&
236 (config->getRetryTries() == 0 ||
237 tries < config->getRetryTries()) // ... limits not reached and has an ...
239 (state == core::socket::State::FATAL && config->getRetryOnFatal()))) { // error occurred?
240 double relativeRetryTimeout =
241 config->getRetryLimit() > 0
242 ? std::min<double>(config->getRetryTimeout() * retryTimeoutScale, config->getRetryLimit())
245 utils::Random::getInRange(-config->getRetryJitter(), config->getRetryJitter()) *
247
248 LOG(INFO)
249 << config->getInstanceName() << ": Retry connect in " << relativeRetryTimeout << " seconds";
250
251 sharedContext->autoConnectControl->armRetryTimer(
253 [config,
255 /*generation,*/ onStatus,
256 tries,
258 if (!sharedContext->autoConnectControl->isRetryEnabled()) {
259 return;
260 }
261 if (config->getRetry()) {
263 .realConnect(onStatus, tries + 1, retryTimeoutScale * config->getRetryBase());
264 } else {
265 LOG(INFO) << config->getInstanceName() << ": Retry connect disabled during wait";
266 }
267 });
268 }
269 },
270 config);
271 }
272 } else {
273 LOG(FATAL) << config->getInstanceName() << " required";
274 }
275 });
276
277 return *this;
278 }
static void atNextTick(const std::function< void(void)> &callBack)
typename SocketConnector::SocketAddress SocketAddress
static double getInRange(double ll, double ul)
Definition Random.cpp:52

Referenced by core::socket::stream::SocketClient< SocketConnectorT< net::in6::phy::stream::PhysicalSocketClient, ConfigSocketClientT >, SocketContextFactoryT, Args... >::connect(), and core::socket::stream::SocketClient< SocketConnectorT< net::in6::phy::stream::PhysicalSocketClient, ConfigSocketClientT >, SocketContextFactoryT, Args... >::realConnect().

Here is the caller graph for this function:

◆ setOnConnect()

template<typename SocketConnectorT, typename SocketContextFactoryT, typename... Args>
const SocketClient & core::socket::stream::SocketClient< SocketConnectorT, SocketContextFactoryT, Args >::setOnConnect ( const std::function< void(SocketConnection *)> & onConnect,
bool initialize = false ) const
inline

Definition at line 304 of file SocketClient.h.

304 {
305 sharedContext->onConnect =
309 };
310
311 return *this;
312 }

◆ setOnConnected()

template<typename SocketConnectorT, typename SocketContextFactoryT, typename... Args>
const SocketClient & core::socket::stream::SocketClient< SocketConnectorT, SocketContextFactoryT, Args >::setOnConnected ( const std::function< void(SocketConnection *)> & onConnected,
bool initialize = false ) const
inline

Definition at line 318 of file SocketClient.h.

318 {
319 sharedContext->onConnected =
323 };
324
325 return *this;
326 }

◆ setOnDisconnect()

template<typename SocketConnectorT, typename SocketContextFactoryT, typename... Args>
const SocketClient & core::socket::stream::SocketClient< SocketConnectorT, SocketContextFactoryT, Args >::setOnDisconnect ( const std::function< void(SocketConnection *)> & onDisconnect,
bool initialize = false ) const
inline

Definition at line 332 of file SocketClient.h.

332 {
333 sharedContext->onDisconnect =
338 };
339
340 return *this;
341 }

◆ setOnInitState()

template<typename SocketConnectorT, typename SocketContextFactoryT, typename... Args>
const SocketClient & core::socket::stream::SocketClient< SocketConnectorT, SocketContextFactoryT, Args >::setOnInitState ( const std::function< void(core::eventreceiver::ConnectEventReceiver *)> & onInitState,
bool initialize = false ) const
inline

Definition at line 351 of file SocketClient.h.

Referenced by main().

Here is the caller graph for this function:

Member Data Documentation

◆ sharedContext

template<typename SocketConnectorT, typename SocketContextFactoryT, typename... Args>
std::shared_ptr<Context> core::socket::stream::SocketClient< SocketConnectorT, SocketContextFactoryT, Args >::sharedContext
private

Definition at line 368 of file SocketClient.h.

Referenced by core::socket::stream::SocketClient< SocketConnectorT< net::in6::phy::stream::PhysicalSocketClient, ConfigSocketClientT >, SocketContextFactoryT, Args... >::getAutoConnectController(), core::socket::stream::SocketClient< SocketConnectorT< net::in6::phy::stream::PhysicalSocketClient, ConfigSocketClientT >, SocketContextFactoryT, Args... >::getOnConnect(), core::socket::stream::SocketClient< SocketConnectorT< net::in6::phy::stream::PhysicalSocketClient, ConfigSocketClientT >, SocketContextFactoryT, Args... >::getOnConnected(), core::socket::stream::SocketClient< SocketConnectorT< net::in6::phy::stream::PhysicalSocketClient, ConfigSocketClientT >, SocketContextFactoryT, Args... >::getOnDisconnect(), core::socket::stream::SocketClient< SocketConnectorT< net::in6::phy::stream::PhysicalSocketClient, ConfigSocketClientT >, SocketContextFactoryT, Args... >::getOnInitState(), core::socket::stream::SocketClient< SocketConnectorT< net::in6::phy::stream::PhysicalSocketClient, ConfigSocketClientT >, SocketContextFactoryT, Args... >::getSocketContextFactory(), core::socket::stream::SocketClient< SocketConnectorT< net::in6::phy::stream::PhysicalSocketClient, ConfigSocketClientT >, SocketContextFactoryT, Args... >::setOnConnect(), core::socket::stream::SocketClient< SocketConnectorT< net::in6::phy::stream::PhysicalSocketClient, ConfigSocketClientT >, SocketContextFactoryT, Args... >::setOnConnected(), core::socket::stream::SocketClient< SocketConnectorT< net::in6::phy::stream::PhysicalSocketClient, ConfigSocketClientT >, SocketContextFactoryT, Args... >::setOnDisconnect(), core::socket::stream::SocketClient< SocketConnectorT< net::in6::phy::stream::PhysicalSocketClient, ConfigSocketClientT >, SocketContextFactoryT, Args... >::setOnInitState(), core::socket::stream::SocketClient< SocketConnectorT< net::in6::phy::stream::PhysicalSocketClient, ConfigSocketClientT >, SocketContextFactoryT, Args... >::SocketClient(), core::socket::stream::SocketClient< SocketConnectorT< net::in6::phy::stream::PhysicalSocketClient, ConfigSocketClientT >, SocketContextFactoryT, Args... >::SocketClient(), and core::socket::stream::SocketClient< SocketConnectorT< net::in6::phy::stream::PhysicalSocketClient, ConfigSocketClientT >, SocketContextFactoryT, Args... >::SocketClient().


The documentation for this class was generated from the following file: