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 48 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 57 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 56 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 50 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 51 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 53 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 59 of file SocketClient.h.

64 : Super(name)
65 , socketContextFactory(std::make_shared<SocketContextFactory>(std::forward<Args>(args)...))
69 }
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 71 of file SocketClient.h.

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

81 name,
82 [name](SocketConnection* socketConnection) { // onConnect
83 VLOG(2) << name << ": OnConnect";
84
85 VLOG(2) << " Local: " << socketConnection->getLocalAddress().toString();
86 VLOG(2) << " Peer: " << socketConnection->getRemoteAddress().toString();
87 },
88 [name](SocketConnection* socketConnection) { // onConnected
89 VLOG(2) << name << ": OnConnected ";
90
91 VLOG(2) << " Local: " << socketConnection->getLocalAddress().toString();
92 VLOG(2) << " Peer: " << socketConnection->getRemoteAddress().toString();
93 },
94 [name](SocketConnection* socketConnection) { // onDisconnect
95 VLOG(2) << name << ": OnDisconnect ";
96
97 VLOG(2) << " Local: " << socketConnection->getLocalAddress().toString();
98 VLOG(2) << " Peer: " << socketConnection->getRemoteAddress().toString();
99 },
100 std::forward<Args>(args)...) {
101 }
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 103 of file SocketClient.h.

104 : SocketClient("", std::forward<Args>(args)...) {
105 }

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

196 {
197 Super::config->Local::setSocketAddress(localAddress);
198
199 connect(remoteAddress, onStatus);
200 }
std::shared_ptr< Config > config
Definition Socket.h:50
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 187 of file SocketClient.h.

188 {
189 Super::config->Remote::setSocketAddress(remoteAddress);
190
191 connect(onStatus);
192 }

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

183 {
184 realConnect(onStatus, 0, 1);
185 }
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 226 of file SocketClient.h.

226 {
228 }

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

110 {
112 new SocketConnector(
114 onConnect,
116 [client = *this, onStatus](SocketConnection* socketConnection) {
117 client.onDisconnect(socketConnection);
118
119 if (client.getConfig().getReconnect() && core::eventLoopState() == core::State::RUNNING) {
120 double relativeReconnectTimeout = client.getConfig().getReconnectTime();
121
122 LOG(INFO) << client.getConfig().getInstanceName() << ": OnStatus";
123 LOG(INFO) << " reconnecting in " << relativeReconnectTimeout << " seconds";
124
125 core::timer::Timer::singleshotTimer(
126 [client, onStatus]() {
127 client.getConfig().Local::renew();
128 client.getConfig().Remote::renew();
129
130 client.realConnect(onStatus, 0, 1);
131 },
132 relativeReconnectTimeout);
133 }
134 },
135 [client = *this, onStatus, tries, retryTimeoutScale](const SocketAddress& socketAddress, core::socket::State state) {
136 bool retry = (state & core::socket::State::NO_RETRY) == 0 &&
137 (client.getConfig().getRetryTries() == 0 || tries < client.getConfig().getRetryTries());
139
140 onStatus(socketAddress, state);
141
142 switch (state) {
144 [[fallthrough]];
146 retry = false;
147 break;
149 retry = retry && client.getConfig().getRetry();
150 break;
152 retry = retry && client.getConfig().getRetry() && client.getConfig().getRetryOnFatal();
153 break;
154 }
155
156 if (retry) {
157 double relativeRetryTimeout = client.getConfig().getRetryLimit() > 0
158 ? std::min<double>(client.getConfig().getRetryTimeout() * retryTimeoutScale,
159 client.getConfig().getRetryLimit())
160 : client.getConfig().getRetryTimeout() * retryTimeoutScale;
161 relativeRetryTimeout -=
162 utils::Random::getInRange(-client.getConfig().getRetryJitter(), client.getConfig().getRetryJitter()) *
163 relativeRetryTimeout / 100.;
164
165 LOG(INFO) << client.getConfig().getInstanceName() << ": OnStatus";
166 LOG(INFO) << " retrying in " << relativeRetryTimeout << " seconds";
167
169 [client, onStatus, tries, retryTimeoutScale]() mutable {
170 client.getConfig().Local::renew();
171 client.getConfig().Remote::renew();
172
173 client.realConnect(onStatus, tries + 1, retryTimeoutScale * client.getConfig().getRetryBase());
174 },
175 relativeRetryTimeout);
176 }
177 },
179 }
180 }
static State state()
Definition SNodeC.cpp:54
static constexpr int DISABLED
Definition State.h:34
static constexpr int ERROR
Definition State.h:35
static constexpr int FATAL
Definition State.h:36
static constexpr int OK
Definition State.h:33
static constexpr int NO_RETRY
Definition State.h:37
typename SocketConnector::SocketAddress SocketAddress
static Timer singleshotTimer(const std::function< void()> &dispatcher, const utils::Timeval &timeout)
Definition Timer.cpp:35
static double getInRange(double ll, double ul)
Definition Random.cpp:32
Definition Timer.h:37
State eventLoopState()
Definition State.cpp:30

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

202 {
203 std::function<void(SocketConnection*)> oldOnConnect = this->onConnect;
204
205 this->onConnect = onConnect;
206
207 return oldOnConnect;
208 }

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

210 {
211 std::function<void(SocketConnection*)> oldOnConnected = this->onConnected;
212
213 this->onConnected = onConnected;
214
215 return oldOnConnected;
216 }

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

218 {
219 std::function<void(SocketConnection*)> oldOnDisconnect = this->onDisconnect;
220
222
223 return oldOnDisconnect;
224 }

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 233 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 234 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 235 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 231 of file SocketClient.h.


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