SNode.C
Loading...
Searching...
No Matches
core::socket::stream::SocketConnector< PhysicalSocketClientT, ConfigT, SocketConnectionT > Class Template Referenceabstract

#include <SocketConnector.h>

Inheritance diagram for core::socket::stream::SocketConnector< PhysicalSocketClientT, ConfigT, SocketConnectionT >:
Collaboration diagram for core::socket::stream::SocketConnector< PhysicalSocketClientT, ConfigT, SocketConnectionT >:

Public Member Functions

 SocketConnector (const std::shared_ptr< core::socket::stream::SocketContextFactory > &socketContextFactory, const std::function< void(SocketConnection *)> &onConnect, const std::function< void(SocketConnection *)> &onConnected, const std::function< void(SocketConnection *)> &onDisconnect, const std::function< void(const SocketAddress &, core::socket::State)> &onStatus, const std::shared_ptr< Config > &config)
 
 SocketConnector (const SocketConnector &socketConnector)
 
 ~SocketConnector () override
 

Protected Types

using Config = ConfigT
 
using SocketAddress = typename PhysicalClientSocket::SocketAddress
 
using SocketConnection = SocketConnectionT< PhysicalClientSocket, Config >
 

Protected Member Functions

virtual void useNextSocketAddress ()=0
 
virtual void init ()
 
void destruct () final
 
- Protected Member Functions inherited from core::eventreceiver::ConnectEventReceiver
 ConnectEventReceiver (const std::string &name, const utils::Timeval &timeout)
 
- Protected Member Functions inherited from core::DescriptorEventReceiver
bool enable (int fd)
 
void disable ()
 
void suspend ()
 
void resume ()
 
 DescriptorEventReceiver (const std::string &name, DescriptorEventPublisher &descriptorEventPublisher, const utils::Timeval &timeout=TIMEOUT::DISABLE)
 
int getRegisteredFd () const
 
bool isEnabled () const
 
bool isSuspended () const
 
void setTimeout (const utils::Timeval &timeout)
 
utils::Timeval getTimeout (const utils::Timeval &currentTime) const
 
void checkTimeout (const utils::Timeval &currentTime)
 
- Protected Member Functions inherited from core::Observer
void observed ()
 
void unObserved ()
 
 Observer ()=default
 
 Observer (Observer &)=delete
 
 Observer (Observer &&)=delete
 
virtual ~Observer ()
 
- Protected Member Functions inherited from core::EventReceiver
virtual ~EventReceiver ()=default
 
 EventReceiver (const std::string &name)
 
 EventReceiver (EventReceiver &)=delete
 
 EventReceiver (EventReceiver &&)=delete
 
EventReceiveroperator= (EventReceiver &)=delete
 
EventReceiveroperator= (EventReceiver &&)=delete
 
void span ()
 
void relax ()
 
const std::string & getName () const
 

Protected Attributes

std::shared_ptr< core::socket::stream::SocketContextFactorysocketContextFactory = nullptr
 
std::function< void(SocketConnection *)> onConnect
 
std::function< void(SocketConnection *)> onConnected
 
std::function< void(SocketConnection *)> onDisconnect
 
std::function< void(const SocketAddress &, core::socket::State)> onStatus
 
std::shared_ptr< Configconfig = nullptr
 

Private Types

using PhysicalClientSocket = PhysicalSocketClientT
 

Private Member Functions

void connectEvent () final
 
void unobservedEvent () final
 
void connectTimeout () final
 

Private Attributes

PhysicalClientSocket physicalClientSocket
 
SocketAddress remoteAddress
 

Detailed Description

template<typename PhysicalSocketClientT, typename ConfigT, template< typename PhysicalSocketClient, typename Config > typename SocketConnectionT>
class core::socket::stream::SocketConnector< PhysicalSocketClientT, ConfigT, SocketConnectionT >

Definition at line 64 of file SocketConnector.h.

Member Typedef Documentation

◆ Config

template<typename PhysicalSocketClientT , typename ConfigT , template< typename PhysicalSocketClient, typename Config > typename SocketConnectionT>
using core::socket::stream::SocketConnector< PhysicalSocketClientT, ConfigT, SocketConnectionT >::Config = ConfigT
protected

Definition at line 69 of file SocketConnector.h.

◆ PhysicalClientSocket

template<typename PhysicalSocketClientT , typename ConfigT , template< typename PhysicalSocketClient, typename Config > typename SocketConnectionT>
using core::socket::stream::SocketConnector< PhysicalSocketClientT, ConfigT, SocketConnectionT >::PhysicalClientSocket = PhysicalSocketClientT
private

Definition at line 66 of file SocketConnector.h.

◆ SocketAddress

template<typename PhysicalSocketClientT , typename ConfigT , template< typename PhysicalSocketClient, typename Config > typename SocketConnectionT>
using core::socket::stream::SocketConnector< PhysicalSocketClientT, ConfigT, SocketConnectionT >::SocketAddress = typename PhysicalClientSocket::SocketAddress
protected

Definition at line 70 of file SocketConnector.h.

◆ SocketConnection

template<typename PhysicalSocketClientT , typename ConfigT , template< typename PhysicalSocketClient, typename Config > typename SocketConnectionT>
using core::socket::stream::SocketConnector< PhysicalSocketClientT, ConfigT, SocketConnectionT >::SocketConnection = SocketConnectionT<PhysicalClientSocket, Config>
protected

Definition at line 71 of file SocketConnector.h.

Constructor & Destructor Documentation

◆ SocketConnector() [1/2]

template<typename PhysicalSocketClient , typename Config , template< typename ConfigT, typename PhysicalSocketClientT > typename SocketConnection>
core::socket::stream::SocketConnector< PhysicalSocketClient, Config, SocketConnection >::SocketConnector ( const std::shared_ptr< core::socket::stream::SocketContextFactory > &  socketContextFactory,
const std::function< void(SocketConnection *)> &  onConnect,
const std::function< void(SocketConnection *)> &  onConnected,
const std::function< void(SocketConnection *)> &  onDisconnect,
const std::function< void(const SocketAddress &, core::socket::State)> &  onStatus,
const std::shared_ptr< Config > &  config 
)

Definition at line 61 of file SocketConnector.hpp.

68 : core::eventreceiver::ConnectEventReceiver(config->getInstanceName() + " SocketConnector", 0)
74 , config(config) {
75 atNextTick([this]() {
77 init();
78 } else {
79 destruct();
80 }
81 });
82 }
static void atNextTick(const std::function< void(void)> &callBack)
std::function< void(const SocketAddress &, core::socket::State)> onStatus
std::function< void(SocketConnection *)> onConnected
std::shared_ptr< core::socket::stream::SocketContextFactory > socketContextFactory
std::function< void(SocketConnection *)> onDisconnect
std::function< void(SocketConnection *)> onConnect
State eventLoopState()
Definition State.cpp:52

References core::socket::stream::SocketConnector< PhysicalSocketClientT, ConfigT, SocketConnectionT >::onConnect, core::socket::stream::SocketConnector< PhysicalSocketClientT, ConfigT, SocketConnectionT >::onConnected, core::socket::stream::SocketConnector< PhysicalSocketClientT, ConfigT, SocketConnectionT >::onDisconnect, core::socket::stream::SocketConnector< PhysicalSocketClientT, ConfigT, SocketConnectionT >::onStatus, and core::socket::stream::SocketConnector< PhysicalSocketClientT, ConfigT, SocketConnectionT >::socketContextFactory.

◆ SocketConnector() [2/2]

template<typename PhysicalSocketServer , typename Config , template< typename ConfigT, typename PhysicalSocketServerT > typename SocketConnection>
core::socket::stream::SocketConnector< PhysicalSocketServer, Config, SocketConnection >::SocketConnector ( const SocketConnector< PhysicalSocketClientT, ConfigT, SocketConnectionT > &  socketConnector)

Definition at line 87 of file SocketConnector.hpp.

88 : core::eventreceiver::ConnectEventReceiver(socketConnector.config->getInstanceName() + " SocketConnector", 0)
89 , socketContextFactory(socketConnector.socketContextFactory)
90 , onConnect(socketConnector.onConnect)
91 , onConnected(socketConnector.onConnected)
92 , onDisconnect(socketConnector.onDisconnect)
93 , onStatus(socketConnector.onStatus)
94 , config(socketConnector.config) {
95 atNextTick([this]() {
97 init();
98 } else {
99 destruct();
100 }
101 });
102 }

◆ ~SocketConnector()

template<typename PhysicalSocketClient , typename Config , template< typename ConfigT, typename PhysicalSocketClientT > typename SocketConnection>
core::socket::stream::SocketConnector< PhysicalSocketClient, Config, SocketConnection >::~SocketConnector ( )
override

Definition at line 107 of file SocketConnector.hpp.

107 {
108 }

Member Function Documentation

◆ connectEvent()

template<typename PhysicalSocketClient , typename Config , template< typename ConfigT, typename PhysicalSocketClientT > typename SocketConnection>
void core::socket::stream::SocketConnector< PhysicalSocketClient, Config, SocketConnection >::connectEvent ( )
finalprivatevirtual

Implements core::eventreceiver::ConnectEventReceiver.

Definition at line 249 of file SocketConnector.hpp.

249 {
250 int cErrno = 0;
251
252 if (physicalClientSocket.getSockError(cErrno) == 0) { // == 0->return valid : < 0->getsockopt failed
253 const utils::PreserveErrno pe(cErrno); // errno = cErrno
254
255 if (errno == 0) {
256 SocketConnection* socketConnection = new SocketConnection(std::move(physicalClientSocket), onDisconnect, config);
257
258 LOG(DEBUG) << config->getInstanceName() << " connect " << remoteAddress.toString() << ": success";
259 LOG(DEBUG) << " " << socketConnection->getLocalAddress().toString() << " -> "
260 << socketConnection->getRemoteAddress().toString();
261
262 onStatus(remoteAddress, core::socket::STATE_OK);
263
264 onConnect(socketConnection);
265 onConnected(socketConnection);
266
267 disable();
268 } else if (PhysicalClientSocket::connectInProgress(errno)) {
269 LOG(DEBUG) << config->getInstanceName() << " connect " << remoteAddress.toString() << ": in progress:";
270 } else {
271 SocketAddress currentRemoteAddress = remoteAddress;
272
273 core::socket::State state = core::socket::STATE_OK;
274
275 switch (errno) {
276 case EADDRINUSE:
277 case EADDRNOTAVAIL:
278 case ECONNREFUSED:
279 case ENETUNREACH:
280 case ENOENT:
281 case EHOSTDOWN:
282 state = core::socket::STATE_ERROR;
283 break;
284 default:
285 state = core::socket::STATE_FATAL;
286 break;
287 }
288
289 if (remoteAddress.useNext()) {
290 PLOG(DEBUG) << config->getInstanceName() << " connect '" << remoteAddress.toString();
291
292 onStatus(currentRemoteAddress, (state | core::socket::State::NO_RETRY));
293
294 LOG(DEBUG) << config->getInstanceName()
295 << " using next SocketAddress: " << config->Remote::getSocketAddress().toString();
296
298
299 disable();
300 } else {
301 PLOG(DEBUG) << config->getInstanceName() << " connect " << remoteAddress.toString();
302
303 onStatus(currentRemoteAddress, state);
304
305 disable();
306 }
307 }
308 } else {
309 PLOG(DEBUG) << config->getInstanceName() << " getsockopt syscall error: '" << remoteAddress.toString() << "'";
310
311 onStatus(remoteAddress, core::socket::STATE_FATAL);
312 disable();
313 }
314 }
static constexpr int NO_RETRY
Definition State.h:59
typename PhysicalClientSocket::SocketAddress SocketAddress
SocketConnectionT< PhysicalClientSocket, Config > SocketConnection

References core::socket::stream::SocketConnector< PhysicalSocketClientT, ConfigT, SocketConnectionT >::config, core::DescriptorEventReceiver::disable(), core::socket::State::NO_RETRY, core::socket::stream::SocketConnector< PhysicalSocketClientT, ConfigT, SocketConnectionT >::onConnect, core::socket::stream::SocketConnector< PhysicalSocketClientT, ConfigT, SocketConnectionT >::onConnected, core::socket::stream::SocketConnector< PhysicalSocketClientT, ConfigT, SocketConnectionT >::onDisconnect, core::socket::stream::SocketConnector< PhysicalSocketClientT, ConfigT, SocketConnectionT >::onStatus, core::socket::State::operator|(), core::socket::stream::SocketConnector< PhysicalSocketClientT, ConfigT, SocketConnectionT >::physicalClientSocket, utils::PreserveErrno::PreserveErrno(), core::socket::stream::SocketConnector< PhysicalSocketClientT, ConfigT, SocketConnectionT >::remoteAddress, and core::socket::stream::SocketConnector< PhysicalSocketClientT, ConfigT, SocketConnectionT >::useNextSocketAddress().

Here is the call graph for this function:

◆ connectTimeout()

template<typename PhysicalSocketClient , typename Config , template< typename ConfigT, typename PhysicalSocketClientT > typename SocketConnection>
void core::socket::stream::SocketConnector< PhysicalSocketClient, Config, SocketConnection >::connectTimeout ( )
finalprivatevirtual

Reimplemented from core::eventreceiver::ConnectEventReceiver.

Definition at line 326 of file SocketConnector.hpp.

326 {
327 LOG(TRACE) << config->getInstanceName() << " connect timeout " << remoteAddress.toString();
328
329 SocketAddress currentRemoteAddress = remoteAddress;
330 if (remoteAddress.useNext()) {
331 LOG(DEBUG) << config->getInstanceName() << " using next SocketAddress: '" << config->Remote::getSocketAddress().toString()
332 << "'";
333
335 } else {
336 LOG(DEBUG) << config->getInstanceName() << " connect timeout '" << remoteAddress.toString() << "'";
337 errno = ETIMEDOUT;
338
339 onStatus(currentRemoteAddress, core::socket::STATE_ERROR);
340 }
341
343 }

References core::socket::stream::SocketConnector< PhysicalSocketClientT, ConfigT, SocketConnectionT >::config, core::eventreceiver::ConnectEventReceiver::connectTimeout(), core::socket::stream::SocketConnector< PhysicalSocketClientT, ConfigT, SocketConnectionT >::remoteAddress, and core::socket::stream::SocketConnector< PhysicalSocketClientT, ConfigT, SocketConnectionT >::useNextSocketAddress().

Here is the call graph for this function:

◆ destruct()

template<typename PhysicalSocketClient , typename Config , template< typename ConfigT, typename PhysicalSocketClientT > typename SocketConnection>
void core::socket::stream::SocketConnector< PhysicalSocketClient, Config, SocketConnection >::destruct ( )
finalprotectedvirtual

◆ init()

template<typename PhysicalSocketClient , typename Config , template< typename ConfigT, typename PhysicalSocketClientT > typename SocketConnection>
void core::socket::stream::SocketConnector< PhysicalSocketClient, Config, SocketConnection >::init ( )
protectedvirtual

Reimplemented in core::socket::stream::tls::SocketConnector< PhysicalSocketClientT, ConfigT >.

Definition at line 113 of file SocketConnector.hpp.

113 {
114 if (!config->getDisabled()) {
115 try {
116 core::socket::State state = core::socket::STATE_OK;
117
118 LOG(TRACE) << config->getInstanceName() << " Starting";
119
120 SocketAddress bindAddress = config->Local::getSocketAddress();
121
122 try {
123 remoteAddress = config->Remote::getSocketAddress();
124
125 if (physicalClientSocket.open(config->getSocketOptions(), PhysicalClientSocket::Flags::NONBLOCK) < 0) {
126 PLOG(DEBUG) << config->getInstanceName() << " open " << bindAddress.toString();
127
128 switch (errno) {
129 case EMFILE:
130 case ENFILE:
131 case ENOBUFS:
132 case ENOMEM:
133 state = core::socket::STATE_ERROR;
134 break;
135 default:
136 state = core::socket::STATE_FATAL;
137 break;
138 }
139
140 onStatus(bindAddress, state);
141 } else {
142 LOG(TRACE) << config->getInstanceName() << " open " << bindAddress.toString() << ": success";
143
144 if (physicalClientSocket.bind(bindAddress) < 0) {
145 PLOG(DEBUG) << config->getInstanceName() << " bind " << bindAddress.toString();
146
147 switch (errno) {
148 case EADDRINUSE:
149 state = core::socket::STATE_ERROR;
150 break;
151 default:
152 state = core::socket::STATE_FATAL;
153 break;
154 }
155
156 onStatus(bindAddress, state);
157 } else {
158 LOG(TRACE) << config->getInstanceName() << " bind " << bindAddress.toString() << ": success";
159
160 if (physicalClientSocket.connect(remoteAddress) < 0 && !PhysicalClientSocket::connectInProgress(errno)) {
161 PLOG(DEBUG) << config->getInstanceName() << " connect " << remoteAddress.toString();
162 switch (errno) {
163 case EADDRINUSE:
164 case EADDRNOTAVAIL:
165 case ECONNREFUSED:
166 case ENETUNREACH:
167 case ENOENT:
168 case EHOSTDOWN:
169 state = core::socket::STATE_ERROR;
170 break;
171 default:
172 state = core::socket::STATE_FATAL;
173 break;
174 }
175
176 SocketAddress currentRemoteAddress = remoteAddress;
177 if (remoteAddress.useNext()) {
178 onStatus(currentRemoteAddress, state | core::socket::State::NO_RETRY);
179
180 LOG(DEBUG) << config->getInstanceName() << " using next SocketAddress: " << remoteAddress.toString();
181
183 } else {
184 onStatus(currentRemoteAddress, state);
185 }
186 } else {
187 LOG(TRACE) << config->getInstanceName() << " connect " << remoteAddress.toString() << ": success";
188
189 if (PhysicalClientSocket::connectInProgress(errno)) {
190 if (enable(physicalClientSocket.getFd())) {
191 LOG(DEBUG)
192 << config->getInstanceName() << " enable " << remoteAddress.toString(false) << ": success";
193 } else {
194 LOG(ERROR) << config->getInstanceName() << " enable " << remoteAddress.toString()
195 << ": failed. No valid descriptor created";
196
197 state = core::socket::STATE(core::socket::STATE_FATAL, ECANCELED, "SocketConnector not enabled");
198
199 onStatus(remoteAddress, state);
200 }
201 } else {
202 SocketConnection* socketConnection =
204
205 LOG(DEBUG) << config->getInstanceName() << " connect " << remoteAddress.toString() << ": success";
206 LOG(DEBUG) << " " << socketConnection->getLocalAddress().toString() << " -> "
207 << socketConnection->getRemoteAddress().toString();
208
209 onStatus(remoteAddress, state);
210
211 onConnect(socketConnection);
212 onConnected(socketConnection);
213 }
214 }
215 }
216 }
217 } catch (const typename SocketAddress::BadSocketAddress& badSocketAddress) {
218 core::socket::State state =
219 core::socket::STATE(badSocketAddress.getState(), badSocketAddress.getErrnum(), badSocketAddress.what());
220
221 LOG(ERROR) << state.what();
222
223 onStatus({}, state);
224 }
225 } catch (const typename SocketAddress::BadSocketAddress& badSocketAddress) {
226 core::socket::State state =
227 core::socket::STATE(badSocketAddress.getState(), badSocketAddress.getErrnum(), badSocketAddress.what());
228
229 LOG(ERROR) << state.what();
230
231 onStatus({}, state);
232 }
233 } else {
234 LOG(DEBUG) << config->getInstanceName() << ": disabled";
235
236 onStatus({}, core::socket::STATE_DISABLED);
237 }
238
239 if (isEnabled()) {
241 } else {
242 destruct();
243 }
244 }
void setTimeout(const utils::Timeval &timeout)
std::string what() const
Definition State.cpp:114

References core::socket::stream::SocketConnector< PhysicalSocketClientT, ConfigT, SocketConnectionT >::config, core::socket::stream::SocketConnector< PhysicalSocketClientT, ConfigT, SocketConnectionT >::destruct(), core::DescriptorEventReceiver::enable(), core::DescriptorEventReceiver::isEnabled(), core::socket::State::NO_RETRY, core::socket::stream::SocketConnector< PhysicalSocketClientT, ConfigT, SocketConnectionT >::onConnect, core::socket::stream::SocketConnector< PhysicalSocketClientT, ConfigT, SocketConnectionT >::onConnected, core::socket::stream::SocketConnector< PhysicalSocketClientT, ConfigT, SocketConnectionT >::onDisconnect, core::socket::stream::SocketConnector< PhysicalSocketClientT, ConfigT, SocketConnectionT >::onStatus, core::socket::State::operator|(), core::socket::stream::SocketConnector< PhysicalSocketClientT, ConfigT, SocketConnectionT >::physicalClientSocket, core::socket::stream::SocketConnector< PhysicalSocketClientT, ConfigT, SocketConnectionT >::remoteAddress, core::DescriptorEventReceiver::setTimeout(), core::socket::stream::SocketConnector< PhysicalSocketClientT, ConfigT, SocketConnectionT >::useNextSocketAddress(), and core::socket::State::what().

Referenced by core::socket::stream::SocketConnector< PhysicalClientSocketT, ConfigT, core::socket::stream::legacy::SocketConnection >::SocketConnector().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ unobservedEvent()

template<typename PhysicalSocketClient , typename Config , template< typename ConfigT, typename PhysicalSocketClientT > typename SocketConnection>
void core::socket::stream::SocketConnector< PhysicalSocketClient, Config, SocketConnection >::unobservedEvent ( )
finalprivatevirtual

Implements core::Observer.

Definition at line 319 of file SocketConnector.hpp.

319 {
320 destruct();
321 }

References core::socket::stream::SocketConnector< PhysicalSocketClientT, ConfigT, SocketConnectionT >::destruct().

Here is the call graph for this function:

◆ useNextSocketAddress()

template<typename PhysicalSocketClientT , typename ConfigT , template< typename PhysicalSocketClient, typename Config > typename SocketConnectionT>
virtual void core::socket::stream::SocketConnector< PhysicalSocketClientT, ConfigT, SocketConnectionT >::useNextSocketAddress ( )
protectedpure virtual

Member Data Documentation

◆ config

◆ onConnect

◆ onConnected

template<typename PhysicalSocketClientT , typename ConfigT , template< typename PhysicalSocketClient, typename Config > typename SocketConnectionT>
std::function<void(SocketConnection*)> core::socket::stream::SocketConnector< PhysicalSocketClientT, ConfigT, SocketConnectionT >::onConnected
protected

◆ onDisconnect

template<typename PhysicalSocketClientT , typename ConfigT , template< typename PhysicalSocketClient, typename Config > typename SocketConnectionT>
std::function<void(SocketConnection*)> core::socket::stream::SocketConnector< PhysicalSocketClientT, ConfigT, SocketConnectionT >::onDisconnect
protected

◆ onStatus

template<typename PhysicalSocketClientT , typename ConfigT , template< typename PhysicalSocketClient, typename Config > typename SocketConnectionT>
std::function<void(const SocketAddress&, core::socket::State)> core::socket::stream::SocketConnector< PhysicalSocketClientT, ConfigT, SocketConnectionT >::onStatus
protected

◆ physicalClientSocket

template<typename PhysicalSocketClientT , typename ConfigT , template< typename PhysicalSocketClient, typename Config > typename SocketConnectionT>
PhysicalClientSocket core::socket::stream::SocketConnector< PhysicalSocketClientT, ConfigT, SocketConnectionT >::physicalClientSocket
private

◆ remoteAddress

◆ socketContextFactory

template<typename PhysicalSocketClientT , typename ConfigT , template< typename PhysicalSocketClient, typename Config > typename SocketConnectionT>
std::shared_ptr<core::socket::stream::SocketContextFactory> core::socket::stream::SocketConnector< PhysicalSocketClientT, ConfigT, SocketConnectionT >::socketContextFactory = nullptr
protected

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