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 = SocketConnectorT::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)
 
 Socket (const std::shared_ptr< Config > &config)
 
 Socket (Socket &&) noexcept=delete
 
Socketoperator= (Socket &&) noexcept=delete
 
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 >
 Socket (const Socket &)=default
 
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

◆ 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)
105 , socketContextFactory(std::make_shared<SocketContextFactory>(std::forward<Args>(args)...))
106 , onConnect([onConnect](SocketConnection* socketConnection) { // onConnect
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) {
113 onConnect(socketConnection);
114 }
115 })
116 , onConnected([onConnected](SocketConnection* socketConnection) { // onConnected
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) {
123 onConnected(socketConnection);
124 }
125 })
126 , onDisconnect([onDisconnect](SocketConnection* socketConnection) { // onDisconnect
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) {
143 onDisconnect(socketConnection);
144 }
145 }) {
146 }
typename SocketConnector::SocketConnection SocketConnection

References core::socket::stream::SocketClient< SocketConnectorT, SocketContextFactoryT, Args >::onConnect, and core::socket::stream::SocketClient< SocketConnectorT, SocketContextFactoryT, Args >::socketContextFactory.

◆ 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.

152 : SocketClient("", onConnect, onConnected, onDisconnect, std::forward<Args>(args)...) {
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.

161 : SocketClient("", std::forward<Args>(args)...) {
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 260 of file SocketClient.h.

262 {
263 Super::config->Local::setSocketAddress(localAddress);
264
265 connect(remoteAddress, onStatus);
266 }
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 253 of file SocketClient.h.

254 {
255 Super::config->Remote::setSocketAddress(remoteAddress);
256
257 connect(onStatus);
258 }

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

249 {
250 realConnect(onStatus, 0, 1);
251 }
void realConnect(const std::function< void(const SocketAddress &, core::socket::State)> &onStatus, unsigned int tries, double retryTimeoutScale) const

References core::socket::stream::SocketClient< SocketConnectorT, SocketContextFactoryT, Args >::realConnect().

Referenced by main().

Here is the call graph for this function:
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

◆ getOnConnected()

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

◆ getOnDisconnect()

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

◆ getSocketContextFactory()

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

◆ 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,
178 onStatus](SocketConnection* socketConnection) {
179 onDisconnect(socketConnection);
180
181 if (config->getReconnect() && core::eventLoopState() == core::State::RUNNING) {
182 double relativeReconnectTimeout = config->getReconnectTime();
183
184 LOG(INFO) << config->getInstanceName() << ": OnStatus";
185 LOG(INFO) << " reconnecting in " << relativeReconnectTimeout << " seconds";
186
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 = this->config,
200 onConnect = this->onConnect,
201 onConnected = this->onConnected,
204 onStatus,
205 tries,
206 retryTimeoutScale](const SocketAddress& socketAddress, core::socket::State state) mutable {
207 const bool retryFlag = (state & core::socket::State::NO_RETRY) == 0;
209 onStatus(socketAddress, state);
210
211 if (retryFlag && config->getRetry() // Shall we potentially retry? In case are the ...
212 && (config->getRetryTries() == 0 || tries < config->getRetryTries()) // ... limits not reached and has an ...
213 && (state == core::socket::State::ERROR ||
214 (state == core::socket::State::FATAL && config->getRetryOnFatal()))) { // error occurred?
215 double relativeRetryTimeout =
216 config->getRetryLimit() > 0
217 ? std::min<double>(config->getRetryTimeout() * retryTimeoutScale, config->getRetryLimit())
218 : config->getRetryTimeout() * retryTimeoutScale;
219 relativeRetryTimeout -= utils::Random::getInRange(-config->getRetryJitter(), config->getRetryJitter()) *
220 relativeRetryTimeout / 100.;
221
222 LOG(INFO) << config->getInstanceName() << ": OnStatus";
223 LOG(INFO) << " retrying in " << relativeRetryTimeout << " seconds";
224
226 [config,
227 onConnect,
230 onStatus,
231 tries,
232 retryTimeoutScale,
233 socketContextFactory]() mutable {
234 if (config->getRetry()) {
235 SocketClient(config, socketContextFactory, onConnect, onConnected, onDisconnect)
236 .realConnect(onStatus, tries + 1, retryTimeoutScale * config->getRetryBase());
237 } else {
238 LOG(INFO) << config->getInstanceName() << ": Retry disabled during wait";
239 }
240 },
241 relativeRetryTimeout);
242 }
243 },
245 }
246 }
static State state()
Definition SNodeC.cpp:76
static constexpr int ERROR
Definition State.h:57
static constexpr int FATAL
Definition State.h:58
static constexpr int NO_RETRY
Definition State.h:59
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
Definition Timer.h:59
State eventLoopState()
Definition State.cpp:52

References core::socket::State::ERROR, core::eventLoopState(), core::socket::State::FATAL, utils::Random::getInRange(), core::INITIALIZED, core::socket::State::NO_RETRY, core::socket::stream::SocketClient< SocketConnectorT, SocketContextFactoryT, Args >::onConnect, core::socket::stream::SocketClient< SocketConnectorT, SocketContextFactoryT, Args >::onConnected, core::socket::State::operator&(), core::socket::State::operator&=(), core::socket::State::operator==(), core::socket::stream::SocketClient< SocketConnectorT, SocketContextFactoryT, Args >::realConnect(), core::RUNNING, core::timer::Timer::singleshotTimer(), core::socket::stream::SocketClient< SocketConnectorT, SocketContextFactoryT, Args >::socketContextFactory, and core::SNodeC::state().

Referenced by core::socket::stream::SocketClient< SocketConnectorT, SocketContextFactoryT, Args >::connect(), and core::socket::stream::SocketClient< SocketConnectorT, SocketContextFactoryT, Args >::realConnect().

Here is the call graph for this function:
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

Definition at line 272 of file SocketClient.h.

273 {
274 std::function<void(SocketConnection*)> oldOnConnect = this->onConnect;
275
276 this->onConnect = initialize ? onConnect : [oldOnConnect, onConnect](SocketConnection* socketConnection) {
277 oldOnConnect(socketConnection);
278 onConnect(socketConnection);
279 };
280
281 return oldOnConnect;
282 }

References core::socket::stream::SocketClient< SocketConnectorT, SocketContextFactoryT, Args >::onConnect.

◆ 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

Definition at line 288 of file SocketClient.h.

289 {
290 std::function<void(SocketConnection*)> oldOnConnected = this->onConnected;
291
292 this->onConnected = initialize ? onConnected : [oldOnConnected, onConnected](SocketConnection* socketConnection) {
293 oldOnConnected(socketConnection);
294 onConnected(socketConnection);
295 };
296
297 return oldOnConnected;
298 }

References core::socket::stream::SocketClient< SocketConnectorT, SocketContextFactoryT, Args >::onConnected.

◆ 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

Definition at line 304 of file SocketClient.h.

305 {
306 std::function<void(SocketConnection*)> oldOnDisconnect = this->onDisconnect;
307
308 this->onDisconnect = initialize ? onDisconnect : [oldOnDisconnect, onDisconnect](SocketConnection* socketConnection) {
309 oldOnDisconnect(socketConnection);
310 onDisconnect(socketConnection);
311 };
312
313 return oldOnDisconnect;
314 }

References core::socket::stream::SocketClient< SocketConnectorT, SocketContextFactoryT, Args >::onDisconnect.

Member Data Documentation

◆ onConnect

◆ onConnected

◆ onDisconnect

◆ socketContextFactory


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