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
 
- 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 *)> setOnConnect (const std::function< void(SocketConnection *)> &onConnect)
 
std::function< void(SocketConnection *)> setOnConnected (const std::function< void(SocketConnection *)> &onConnected)
 
std::function< void(SocketConnection *)> setOnDisconnect (const std::function< void(SocketConnection *)> &onDisconnect)
 
std::shared_ptr< SocketContextFactorygetSocketContextFactory () const
 
- Public Member Functions inherited from core::socket::Socket< SocketConnectorT::Config >
 Socket (const std::string &name)
 
 Socket (const Socket &)=default
 
 Socket (Socket &&) noexcept=delete
 
Socketoperator= (const Socket &)=default
 
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

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

◆ 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/4]

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

◆ SocketClient() [2/4]

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

100 : SocketClient("", onConnect, onConnected, onDisconnect, std::forward<Args>(args)...) {
101 }
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() [3/4]

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

105 : SocketClient(
106 name,
107 [](SocketConnection* socketConnection) { // onConnect
108 VLOG(2) << socketConnection->getConnectionName() << ": OnConnect";
109
110 VLOG(2) << " Local: " << socketConnection->getLocalAddress().toString();
111 VLOG(2) << " Peer: " << socketConnection->getRemoteAddress().toString();
112 },
113 [](SocketConnection* socketConnection) { // onConnected
114 VLOG(2) << socketConnection->getConnectionName() << ": OnConnected";
115
116 VLOG(2) << " Local: " << socketConnection->getLocalAddress().toString();
117 VLOG(2) << " Peer: " << socketConnection->getRemoteAddress().toString();
118 },
119 [](SocketConnection* socketConnection) { // onDisconnect
120 VLOG(2) << socketConnection->getConnectionName() << ": OnDisconnect";
121
122 VLOG(2) << " Local: " << socketConnection->getLocalAddress().toString();
123 VLOG(2) << " Peer: " << socketConnection->getRemoteAddress().toString();
124
125 VLOG(2) << " Online Since: " << socketConnection->getOnlineSince();
126 VLOG(2) << " Online Duration: " << socketConnection->getOnlineDuration();
127
128 VLOG(2) << " Total Queued: " << socketConnection->getTotalQueued();
129 VLOG(2) << " Total Sent: " << socketConnection->getTotalSent();
130 VLOG(2) << " Write Delta: " << socketConnection->getTotalQueued() - socketConnection->getTotalSent();
131 VLOG(2) << " Total Read: " << socketConnection->getTotalRead();
132 VLOG(2) << " Total Processed: " << socketConnection->getTotalProcessed();
133 VLOG(2) << " Read Delta: " << socketConnection->getTotalRead() - socketConnection->getTotalProcessed();
134
135 },
136 std::forward<Args>(args)...) {
137 }
typename SocketConnector::SocketConnection SocketConnection

◆ SocketClient() [4/4]

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

Definition at line 139 of file SocketClient.h.

140 : SocketClient("", std::forward<Args>(args)...) {
141 }

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

221 {
222 Super::config->Local::setSocketAddress(localAddress);
223
224 connect(remoteAddress, onStatus);
225 }
std::shared_ptr< Config > config
Definition Socket.h:72
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 212 of file SocketClient.h.

213 {
214 Super::config->Remote::setSocketAddress(remoteAddress);
215
216 connect(onStatus);
217 }

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

208 {
209 realConnect(onStatus, 0, 1);
210 }
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:

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

146 {
148 new SocketConnector(
150 onConnect,
152 [client = *this, onStatus](SocketConnection* socketConnection) {
153 client.onDisconnect(socketConnection);
154
155 if (client.getConfig().getReconnect() && core::eventLoopState() == core::State::RUNNING) {
156 double relativeReconnectTimeout = client.getConfig().getReconnectTime();
157
158 LOG(INFO) << client.getConfig().getInstanceName() << ": OnStatus";
159 LOG(INFO) << " reconnecting in " << relativeReconnectTimeout << " seconds";
160
161 core::timer::Timer::singleshotTimer(
162 [client, onStatus]() {
163 client.getConfig().Local::renew();
164 client.getConfig().Remote::renew();
165
166 client.realConnect(onStatus, 0, 1);
167 },
168 relativeReconnectTimeout);
169 }
170 },
171 [client = *this, onStatus, tries, retryTimeoutScale](const SocketAddress& socketAddress,
172 core::socket::State state) mutable {
173 const bool retryFlag = (state & core::socket::State::NO_RETRY) == 0;
175 onStatus(socketAddress, state);
176
177 if (retryFlag && client.getConfig().getRetry() // Shall we potentially retry? In case are the ...
178 && (client.getConfig().getRetryTries() == 0 ||
179 tries < client.getConfig().getRetryTries()) // ... limits not reached and has an ...
180 && (state == core::socket::State::ERROR ||
181 (state == core::socket::State::FATAL && client.getConfig().getRetryOnFatal()))) { // error occurred?
182 double relativeRetryTimeout = client.getConfig().getRetryLimit() > 0
183 ? std::min<double>(client.getConfig().getRetryTimeout() * retryTimeoutScale,
184 client.getConfig().getRetryLimit())
185 : client.getConfig().getRetryTimeout() * retryTimeoutScale;
186 relativeRetryTimeout -=
187 utils::Random::getInRange(-client.getConfig().getRetryJitter(), client.getConfig().getRetryJitter()) *
188 relativeRetryTimeout / 100.;
189
190 LOG(INFO) << client.getConfig().getInstanceName() << ": OnStatus";
191 LOG(INFO) << " retrying in " << relativeRetryTimeout << " seconds";
192
194 [client, onStatus, tries, retryTimeoutScale]() mutable {
195 client.getConfig().Local::renew();
196 client.getConfig().Remote::renew();
197
198 client.realConnect(onStatus, tries + 1, retryTimeoutScale * client.getConfig().getRetryBase());
199 },
200 relativeRetryTimeout);
201 }
202 },
204 }
205 }
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::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().

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)
inline

Definition at line 227 of file SocketClient.h.

227 {
228 std::function<void(SocketConnection*)> oldOnConnect = this->onConnect;
229
230 this->onConnect = onConnect;
231
232 return oldOnConnect;
233 }

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)
inline

Definition at line 235 of file SocketClient.h.

235 {
236 std::function<void(SocketConnection*)> oldOnConnected = this->onConnected;
237
238 this->onConnected = onConnected;
239
240 return oldOnConnected;
241 }

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)
inline

Definition at line 243 of file SocketClient.h.

243 {
244 std::function<void(SocketConnection*)> oldOnDisconnect = this->onDisconnect;
245
247
248 return oldOnDisconnect;
249 }

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

Member Data Documentation

◆ onConnect

◆ onConnected

◆ onDisconnect

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

◆ socketContextFactory


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