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
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)
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 >
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< SocketContextFactory > & socketContextFactory,
const std::function< void(SocketConnection *)> & onConnect,
const std::function< void(SocketConnection *)> & onConnected,
const std::function< void(SocketConnection *)> & onDisconnect )
inlineprivate

Definition at line 86 of file SocketClient.h.

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

101 : Super(name)
105 LOG(DEBUG) << socketConnection->getConnectionName() << ": OnConnect";
106
107 LOG(DEBUG) << " Local: " << socketConnection->getLocalAddress().toString();
108 LOG(DEBUG) << " Peer: " << socketConnection->getRemoteAddress().toString();
109
110 if (onConnect) {
112 }
113 },
115 LOG(DEBUG) << socketConnection->getConnectionName() << ": OnConnected";
116
117 LOG(DEBUG) << " Local: " << socketConnection->getLocalAddress().toString();
118 LOG(DEBUG) << " Peer: " << socketConnection->getRemoteAddress().toString();
119
120 if (onConnected) {
122 }
123 },
125 LOG(DEBUG) << socketConnection->getConnectionName() << ": OnDisconnect";
126
127 LOG(DEBUG) << " Local: " << socketConnection->getLocalAddress().toString();
128 LOG(DEBUG) << " Peer: " << socketConnection->getRemoteAddress().toString();
129
130 LOG(DEBUG) << " Online Since: " << socketConnection->getOnlineSince();
131 LOG(DEBUG) << " Online Duration: " << socketConnection->getOnlineDuration();
132
133 LOG(DEBUG) << " Total Queued: " << socketConnection->getTotalQueued();
134 LOG(DEBUG) << " Total Sent: " << socketConnection->getTotalSent();
135 LOG(DEBUG) << " Write Delta: " << socketConnection->getTotalQueued() - socketConnection->getTotalSent();
136 LOG(DEBUG) << " Total Read: " << socketConnection->getTotalRead();
137 LOG(DEBUG) << " Total Processed: " << socketConnection->getTotalProcessed();
138 LOG(DEBUG) << " Read Delta: " << socketConnection->getTotalRead() - socketConnection->getTotalProcessed();
139
140 if (onDisconnect) {
142 }
143 })) {
144 }
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 146 of file SocketClient.h.

151 }
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() [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 153 of file SocketClient.h.

154 : SocketClient(name, {}, {}, {}, std::forward<Args>(args)...) {
155 }

◆ SocketClient() [5/5]

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

Definition at line 157 of file SocketClient.h.

159 }

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

265 {
266 Super::config->Local::setSocketAddress(localAddress);
267
269 }
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 256 of file SocketClient.h.

257 {
258 Super::config->Remote::setSocketAddress(remoteAddress);
259
260 return connect(onStatus);
261 }

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

252 {
253 return realConnect(onStatus, 0, 1);
254 }
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:

◆ getOnConnect()

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

Definition at line 271 of file SocketClient.h.

271 {
272 return sharedContext->onConnect;
273 }

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

285 {
286 return sharedContext->onConnected();
287 }

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

299 {
300 return sharedContext->onDisconnect;
301 }

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

314 {
315 return sharedContext->socketContextFactory;
316 }

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

164 {
165 LOG(DEBUG) << this->config->getInstanceName() << ": Initiating connect";
166
170 new SocketConnector(
171 sharedContext->socketContextFactory,
172 sharedContext->onConnect,
173 sharedContext->onConnected,
174 [config,
175 onConnect = sharedContext->onConnect,
176 onConnected = sharedContext->onConnected,
177 onDisconnect = sharedContext->onDisconnect,
178 socketContextFactory = sharedContext->socketContextFactory,
180 onDisconnect(socketConnection);
181
182 if (config->getReconnect() && core::eventLoopState() == core::State::RUNNING) {
183 double relativeReconnectTimeout = config->getReconnectTime();
184
185 LOG(INFO) << config->getInstanceName() << ": Reconnect in " << relativeReconnectTimeout << " seconds";
186
187 core::timer::Timer::singleshotTimer(
188 [config, onConnect, onConnected, onDisconnect, onStatus, socketContextFactory]() {
189 if (config->getReconnect()) {
190 SocketClient(config, socketContextFactory, onConnect, onConnected, onDisconnect)
191 .realConnect(onStatus, 0, config->getRetryBase());
192 } else {
193 LOG(INFO) << config->getInstanceName() << ": Reconnect disabled during wait";
194 }
195 },
196 relativeReconnectTimeout);
197 }
198 },
199 [config,
200 onConnect = sharedContext->onConnect,
201 onConnected = sharedContext->onConnected,
202 onDisconnect = sharedContext->onDisconnect,
203 socketContextFactory = sharedContext->socketContextFactory,
204 onStatus,
205 tries,
207 const bool retryFlag = (state & core::socket::State::NO_RETRY) == 0;
210
211 if (retryFlag && config->getRetry() // Shall we potentially retry? In case are the ...
212 && (config->getRetryTries() == 0 ||
213 tries < config->getRetryTries()) // ... limits not reached and has an ...
215 (state == core::socket::State::FATAL && config->getRetryOnFatal()))) { // error occurred?
216 double relativeRetryTimeout =
217 config->getRetryLimit() > 0
218 ? std::min<double>(config->getRetryTimeout() * retryTimeoutScale, config->getRetryLimit())
220 relativeRetryTimeout -= utils::Random::getInRange(-config->getRetryJitter(), config->getRetryJitter()) *
222
223 LOG(INFO) << config->getInstanceName() << ": Retry connect in " << relativeRetryTimeout << " seconds";
224
226 [config,
227 onConnect,
230 onStatus,
231 tries,
234 if (config->getRetry()) {
236 .realConnect(onStatus, tries + 1, retryTimeoutScale * config->getRetryBase());
237 } else {
238 LOG(INFO) << config->getInstanceName() << ": Retry connect disabled during wait";
239 }
240 },
242 }
243 },
244 config);
245 }
246 });
247
248 return *this;
249 }
static void atNextTick(const std::function< void(void)> &callBack)
typename SocketConnector::SocketAddress SocketAddress
static Timer singleshotTimer(const std::function< void()> &dispatcher, const utils::Timeval &timeout)
Definition Timer.cpp:57
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 275 of file SocketClient.h.

275 {
276 sharedContext->onConnect =
280 };
281
282 return *this;
283 }

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

289 {
290 sharedContext->onConnected =
294 };
295
296 return *this;
297 }

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

303 {
304 sharedContext->onDisconnect =
309 };
310
311 return *this;
312 }

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

Referenced by 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... >::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: