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
ClientFlowControllergetFlowController () 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< 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/5]

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 108 of file SocketClient.h.

109 : Super(config)
111 }
const std::shared_ptr< Config > config
Definition Socket.h:75
core::socket::Socket< typename SocketConnector::Config > Super
std::shared_ptr< Context > sharedContext

◆ SocketClient() [2/5]

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 114 of file SocketClient.h.

119 : Super(name)
121 this->config.get(),
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 },
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) {
141 }
142 },
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) {
161 }
162 })) {
163 }
#define LOG(level)
Definition Logger.h:148
typename SocketConnector::SocketConnection SocketConnection

◆ SocketClient() [3/5]

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 165 of file SocketClient.h.

170 }
SocketClient(const std::shared_ptr< Config > &config, const std::shared_ptr< Context > &sharedContext)

◆ SocketClient() [4/5]

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 172 of file SocketClient.h.

173 : SocketClient(name, {}, {}, {}, std::forward<Args>(args)...) {
174 }

◆ SocketClient() [5/5]

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

Definition at line 176 of file SocketClient.h.

178 }

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 286 of file SocketClient.h.

288 {
289 Super::config->Local::setSocketAddress(localAddress);
290
292 }
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 279 of file SocketClient.h.

280 {
281 Super::config->Remote::setSocketAddress(remoteAddress);
282
283 return connect(onStatus);
284 }

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 275 of file SocketClient.h.

275 {
276 return realConnect(onStatus, 0, 1);
277 }
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:

◆ getFlowController()

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

Definition at line 337 of file SocketClient.h.

337 {
338 return &sharedContext->flowController;
339 }

◆ getOnConnect()

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

Definition at line 294 of file SocketClient.h.

294 {
295 return sharedContext->onConnect;
296 }

◆ 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 308 of file SocketClient.h.

308 {
309 return sharedContext->onConnected;
310 }

◆ 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 322 of file SocketClient.h.

322 {
323 return sharedContext->onDisconnect;
324 }

◆ 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 341 of file SocketClient.h.

341 {
342 return sharedContext->socketContextFactory;
343 }

◆ 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 181 of file SocketClient.h.

183 {
184 sharedContext->flowController.startFlow(
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,
195 sharedContext->onDisconnect(socketConnection);
196
197 if (config->getReconnect() && sharedContext->flowController.isReconnectEnabled() &&
198 core::eventLoopState() == core::State::RUNNING) {
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 },
219 sharedContext->flowController.observeConnectEventReceiver(connectEventReceiver);
220 },
223 const bool retryFlag = (state & core::socket::State::NO_RETRY) == 0;
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 ...
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())
238 utils::Random::getInRange(-config->getRetryJitter(), config->getRetryJitter()) *
240
241 LOG(INFO)
242 << config->getInstanceName() << ": Retry connect in " << relativeRetryTimeout << " seconds";
243
244 sharedContext->flowController.armRetryTimer(
246 [config,
248 /*generation,*/ onStatus,
249 tries,
251 if (!sharedContext->flowController.isRetryEnabled()) {
252 return;
253 }
254 if (config->getRetry()) {
255 sharedContext->flowController.reportFlowRetry();
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 }
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 298 of file SocketClient.h.

298 {
299 sharedContext->onConnect =
303 };
304
305 return *this;
306 }

◆ 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 312 of file SocketClient.h.

312 {
313 sharedContext->onConnected =
317 };
318
319 return *this;
320 }

◆ 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 326 of file SocketClient.h.

326 {
327 sharedContext->onDisconnect =
332 };
333
334 return *this;
335 }

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 346 of file SocketClient.h.

Referenced by core::socket::stream::SocketClient< SocketConnectorT< net::in6::phy::stream::PhysicalSocketClient, ConfigSocketClientT >, SocketContextFactoryT, Args... >::getFlowController(), 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... >::getSocketContextFactory(), core::socket::stream::SocketClient< SocketConnectorT< net::in6::phy::stream::PhysicalSocketClient, ConfigSocketClientT >, SocketContextFactoryT, Args... >::realConnect(), 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... >::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: