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 >:

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)
void connect (const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const
void connect (const SocketAddress &remoteAddress, const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const
void connect (const SocketAddress &remoteAddress, const SocketAddress &localAddress, const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const
std::function< void(SocketConnection *)> & getOnConnect ()
std::function< void(SocketConnection *)> setOnConnect (const std::function< void(SocketConnection *)> &onConnect, bool initialize=false)
std::function< void(SocketConnection *)> & getOnConnected ()
std::function< void(SocketConnection *)> setOnConnected (const std::function< void(SocketConnection *)> &onConnected, bool initialize=false)
std::function< void(SocketConnection *)> & getOnDisconnect ()
std::function< void(SocketConnection *)> setOnDisconnect (const std::function< void(SocketConnection *)> &onDisconnect, bool initialize=false)
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)
void realConnect (const std::function< void(const SocketAddress &, core::socket::State)> &onStatus, unsigned int tries, double retryTimeoutScale) const

Private Attributes

const std::shared_ptr< SocketContextFactorysocketContextFactory
std::function< void(SocketConnection *)> onConnect
std::function< void(SocketConnection *)> onConnected
std::function< void(SocketConnection *)> onDisconnect

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.

91 : Super(config)
96 }
const std::shared_ptr< SocketContextFactory > socketContextFactory
std::function< void(SocketConnection *)> onConnect
std::function< void(SocketConnection *)> onDisconnect
core::socket::Socket< typename SocketConnector::Config > Super
std::function< void(SocketConnection *)> onConnected

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

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

153 }
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 156 of file SocketClient.h.

157 : SocketClient(name, {}, {}, {}, std::forward<Args>(args)...) {
158 }

◆ SocketClient() [5/5]

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

Definition at line 160 of file SocketClient.h.

162 }

Member Function Documentation

◆ connect() [1/3]

template<typename SocketConnectorT, typename SocketContextFactoryT, typename... Args>
void 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 258 of file SocketClient.h.

260 {
261 Super::config->Local::setSocketAddress(localAddress);
262
264 }
void connect(const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const

◆ connect() [2/3]

template<typename SocketConnectorT, typename SocketContextFactoryT, typename... Args>
void 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 251 of file SocketClient.h.

252 {
253 Super::config->Remote::setSocketAddress(remoteAddress);
254
256 }

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>
void core::socket::stream::SocketClient< SocketConnectorT, SocketContextFactoryT, Args >::connect ( const std::function< void(const SocketAddress &, core::socket::State)> & onStatus) const
inline

Definition at line 247 of file SocketClient.h.

247 {
248 realConnect(onStatus, 0, 1);
249 }
void 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 266 of file SocketClient.h.

266 {
267 return onConnect;
268 }

◆ getOnConnected()

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

Definition at line 282 of file SocketClient.h.

282 {
283 return onConnected();
284 }

◆ getOnDisconnect()

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

Definition at line 298 of file SocketClient.h.

298 {
299 return onDisconnect;
300 }

◆ 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 {
316 }

◆ realConnect()

template<typename SocketConnectorT, typename SocketContextFactoryT, typename... Args>
void 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 165 of file SocketClient.h.

167 {
169 new SocketConnector(
171 onConnect,
173 [config = this->config,
174 onConnect = this->onConnect,
175 onConnected = this->onConnected,
180
181 if (config->getReconnect() && core::eventLoopState() == core::State::RUNNING) {
182 double relativeReconnectTimeout = config->getReconnectTime();
183
184 LOG(INFO) << config->getInstanceName() << ": Reconnect in " << relativeReconnectTimeout << " seconds";
185
188 if (config->getReconnect()) {
190 .realConnect(onStatus, 0, config->getRetryBase());
191 } else {
192 LOG(INFO) << config->getInstanceName() << ": Reconnect disabled during wait";
193 }
194 },
196 }
197 },
198 [config = this->config,
199 onConnect = this->onConnect,
200 onConnected = this->onConnected,
203 onStatus,
204 tries,
206 const bool retryFlag = (state & core::socket::State::NO_RETRY) == 0;
209
210 if (retryFlag && config->getRetry() // Shall we potentially retry? In case are the ...
211 && (config->getRetryTries() == 0 || tries < config->getRetryTries()) // ... limits not reached and has an ...
213 (state == core::socket::State::FATAL && config->getRetryOnFatal()))) { // error occurred?
214 double relativeRetryTimeout =
215 config->getRetryLimit() > 0
216 ? std::min<double>(config->getRetryTimeout() * retryTimeoutScale, config->getRetryLimit())
218 relativeRetryTimeout -= utils::Random::getInRange(-config->getRetryJitter(), config->getRetryJitter()) *
220
221 LOG(INFO) << config->getInstanceName() << ": Retry connect in " << relativeRetryTimeout << " seconds";
222
224 [config,
225 onConnect,
228 onStatus,
229 tries,
231 socketContextFactory]() mutable {
232 if (config->getRetry()) {
234 .realConnect(onStatus, tries + 1, retryTimeoutScale * config->getRetryBase());
235 } else {
236 LOG(INFO) << config->getInstanceName() << ": Retry connect disabled during wait";
237 }
238 },
240 }
241 },
243 }
244 }
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>
std::function< void(SocketConnection *)> core::socket::stream::SocketClient< SocketConnectorT, SocketContextFactoryT, Args >::setOnConnect ( const std::function< void(SocketConnection *)> & onConnect,
bool initialize = false )
inline

◆ setOnConnected()

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

◆ setOnDisconnect()

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

Member Data Documentation

◆ onConnect

◆ onConnected

◆ onDisconnect

◆ socketContextFactory


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