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
 

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::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 70 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 79 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 78 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 72 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 73 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 75 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

Definition at line 81 of file SocketClient.h.

86 : Super(name)
87 , socketContextFactory(std::make_shared<SocketContextFactory>(std::forward<Args>(args)...))
91 }
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/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 93 of file SocketClient.h.

97 : SocketClient("", onConnect, onConnected, onDisconnect, std::forward<Args>(args)...) {
98 }
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 101 of file SocketClient.h.

102 : SocketClient(
103 name,
104 [name](SocketConnection* socketConnection) { // onConnect
105 VLOG(2) << name << ": OnConnect";
106
107 VLOG(2) << " Local: " << socketConnection->getLocalAddress().toString();
108 VLOG(2) << " Peer: " << socketConnection->getRemoteAddress().toString();
109 },
110 [name](SocketConnection* socketConnection) { // onConnected
111 VLOG(2) << name << ": OnConnected ";
112
113 VLOG(2) << " Local: " << socketConnection->getLocalAddress().toString();
114 VLOG(2) << " Peer: " << socketConnection->getRemoteAddress().toString();
115 },
116 [name](SocketConnection* socketConnection) { // onDisconnect
117 VLOG(2) << name << ": OnDisconnect ";
118
119 VLOG(2) << " Local: " << socketConnection->getLocalAddress().toString();
120 VLOG(2) << " Peer: " << socketConnection->getRemoteAddress().toString();
121 },
122 std::forward<Args>(args)...) {
123 }
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 125 of file SocketClient.h.

126 : SocketClient("", std::forward<Args>(args)...) {
127 }

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

218 {
219 Super::config->Local::setSocketAddress(localAddress);
220
221 connect(remoteAddress, onStatus);
222 }
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 209 of file SocketClient.h.

210 {
211 Super::config->Remote::setSocketAddress(remoteAddress);
212
213 connect(onStatus);
214 }

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

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

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

248 {
250 }

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

132 {
134 new SocketConnector(
136 onConnect,
138 [client = *this, onStatus](SocketConnection* socketConnection) {
139 client.onDisconnect(socketConnection);
140
141 if (client.getConfig().getReconnect() && core::eventLoopState() == core::State::RUNNING) {
142 double relativeReconnectTimeout = client.getConfig().getReconnectTime();
143
144 LOG(INFO) << client.getConfig().getInstanceName() << ": OnStatus";
145 LOG(INFO) << " reconnecting in " << relativeReconnectTimeout << " seconds";
146
147 core::timer::Timer::singleshotTimer(
148 [client, onStatus]() {
149 client.getConfig().Local::renew();
150 client.getConfig().Remote::renew();
151
152 client.realConnect(onStatus, 0, 1);
153 },
154 relativeReconnectTimeout);
155 }
156 },
157 [client = *this, onStatus, tries, retryTimeoutScale](const SocketAddress& socketAddress, core::socket::State state) {
158 bool retry = (state & core::socket::State::NO_RETRY) == 0 &&
159 (client.getConfig().getRetryTries() == 0 || tries < client.getConfig().getRetryTries());
161
162 onStatus(socketAddress, state);
163
164 switch (state) {
166 [[fallthrough]];
168 retry = false;
169 break;
171 retry = retry && client.getConfig().getRetry();
172 break;
174 retry = retry && client.getConfig().getRetry() && client.getConfig().getRetryOnFatal();
175 break;
176 }
177
178 if (retry) {
179 double relativeRetryTimeout = client.getConfig().getRetryLimit() > 0
180 ? std::min<double>(client.getConfig().getRetryTimeout() * retryTimeoutScale,
181 client.getConfig().getRetryLimit())
182 : client.getConfig().getRetryTimeout() * retryTimeoutScale;
183 relativeRetryTimeout -=
184 utils::Random::getInRange(-client.getConfig().getRetryJitter(), client.getConfig().getRetryJitter()) *
185 relativeRetryTimeout / 100.;
186
187 LOG(INFO) << client.getConfig().getInstanceName() << ": OnStatus";
188 LOG(INFO) << " retrying in " << relativeRetryTimeout << " seconds";
189
191 [client, onStatus, tries, retryTimeoutScale]() mutable {
192 client.getConfig().Local::renew();
193 client.getConfig().Remote::renew();
194
195 client.realConnect(onStatus, tries + 1, retryTimeoutScale * client.getConfig().getRetryBase());
196 },
197 relativeRetryTimeout);
198 }
199 },
201 }
202 }
static State state()
Definition SNodeC.cpp:76
static constexpr int DISABLED
Definition State.h:56
static constexpr int ERROR
Definition State.h:57
static constexpr int FATAL
Definition State.h:58
static constexpr int OK
Definition State.h:55
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

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

224 {
225 std::function<void(SocketConnection*)> oldOnConnect = this->onConnect;
226
227 this->onConnect = onConnect;
228
229 return oldOnConnect;
230 }

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

232 {
233 std::function<void(SocketConnection*)> oldOnConnected = this->onConnected;
234
235 this->onConnected = onConnected;
236
237 return oldOnConnected;
238 }

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

240 {
241 std::function<void(SocketConnection*)> oldOnDisconnect = this->onDisconnect;
242
244
245 return oldOnDisconnect;
246 }

Member Data Documentation

◆ onConnect

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

Definition at line 255 of file SocketClient.h.

◆ onConnected

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

Definition at line 256 of file SocketClient.h.

◆ onDisconnect

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

Definition at line 257 of file SocketClient.h.

◆ socketContextFactory

template<typename SocketConnectorT , typename SocketContextFactoryT , typename... Args>
const std::shared_ptr<SocketContextFactory> core::socket::stream::SocketClient< SocketConnectorT, SocketContextFactoryT, Args >::socketContextFactory
private

Definition at line 253 of file SocketClient.h.


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