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>
 

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 SocketConnectionT>
class core::socket::stream::SocketConnector< PhysicalSocketClientT, ConfigT, SocketConnectionT >

Definition at line 62 of file SocketConnector.h.

Member Typedef Documentation

◆ Config

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

Definition at line 67 of file SocketConnector.h.

◆ PhysicalClientSocket

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

Definition at line 64 of file SocketConnector.h.

◆ SocketAddress

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

Definition at line 68 of file SocketConnector.h.

◆ SocketConnection

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

Definition at line 69 of file SocketConnector.h.

Constructor & Destructor Documentation

◆ SocketConnector() [1/2]

template<typename PhysicalSocketClient , typename Config , template< 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 105 of file SocketConnector.hpp.

112 : core::eventreceiver::ConnectEventReceiver(config->getInstanceName() + " SocketConnector", 0)
118 , config(config) {
119 atNextTick([this]() {
121 init();
122 } else {
123 destruct();
124 }
125 });
126 }
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

◆ SocketConnector() [2/2]

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

Definition at line 129 of file SocketConnector.hpp.

130 : core::eventreceiver::ConnectEventReceiver(socketConnector.config->getInstanceName() + " SocketConnector", 0)
131 , socketContextFactory(socketConnector.socketContextFactory)
132 , onConnect(socketConnector.onConnect)
133 , onConnected(socketConnector.onConnected)
134 , onDisconnect(socketConnector.onDisconnect)
135 , onStatus(socketConnector.onStatus)
136 , config(socketConnector.config) {
137 atNextTick([this]() {
139 init();
140 } else {
141 destruct();
142 }
143 });
144 }

◆ ~SocketConnector()

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

Definition at line 147 of file SocketConnector.hpp.

147 {
148 }

Member Function Documentation

◆ connectEvent()

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

Implements core::eventreceiver::ConnectEventReceiver.

Definition at line 281 of file SocketConnector.hpp.

281 {
282 int cErrno = 0;
283
284 if (physicalClientSocket.getSockError(cErrno) == 0) { // == 0->return valid : < 0->getsockopt failed
285 const utils::PreserveErrno pe(cErrno); // errno = cErrno
286
287 if (errno == 0) {
288 LOG(DEBUG) << config->getInstanceName() << " [" << physicalClientSocket.getFd() << "] connect success: '"
289 << remoteAddress.toString() << "'";
290
291 onStatus(remoteAddress, core::socket::STATE_OK);
292
293 SocketConnection* socketConnection =
294 new SocketConnection(config->getInstanceName(),
295 std::move(physicalClientSocket),
297 remoteAddress.toString(false),
300 config->getReadTimeout(),
301 config->getWriteTimeout(),
302 config->getReadBlockSize(),
303 config->getWriteBlockSize(),
304 config->getTerminateTimeout());
305
306 onConnect(socketConnection);
307 onConnected(socketConnection);
308
309 disable();
310 } else if (PhysicalClientSocket::connectInProgress(errno)) {
311 LOG(TRACE) << config->getInstanceName() << " connect still in progress: '" << remoteAddress.toString() << "'";
312 } else {
313 SocketAddress currentRemoteAddress = remoteAddress;
314 if (remoteAddress.useNext()) {
315 core::socket::State state = core::socket::STATE_OK;
316
317 switch (errno) {
318 case EADDRINUSE:
319 case EADDRNOTAVAIL:
320 case ECONNREFUSED:
321 case ENETUNREACH:
322 case ENOENT:
323 case EHOSTDOWN:
324 PLOG(DEBUG) << config->getInstanceName() << " connect: '" << remoteAddress.toString() << "'";
325
326 state = core::socket::STATE_ERROR;
327 break;
328 default:
329 PLOG(DEBUG) << config->getInstanceName() << ": connect: '" << remoteAddress.toString() << "'";
330
331 state = core::socket::STATE_FATAL;
332 break;
333 }
334
335 onStatus(currentRemoteAddress, (state | core::socket::State::NO_RETRY));
336
337 LOG(DEBUG) << config->getInstanceName() << " using next SocketAddress: '"
338 << config->Remote::getSocketAddress().toString() << "'";
339
341
342 disable();
343 } else {
344 core::socket::State state = core::socket::STATE_OK;
345
346 switch (errno) {
347 case EADDRINUSE:
348 case EADDRNOTAVAIL:
349 case ECONNREFUSED:
350 case ENETUNREACH:
351 case ENOENT:
352 case EHOSTDOWN:
353 PLOG(DEBUG) << config->getInstanceName() << " connect: '" << remoteAddress.toString() << "'";
354
355 state = core::socket::STATE_ERROR;
356 break;
357 default:
358 PLOG(DEBUG) << config->getInstanceName() << " connect: '" << remoteAddress.toString() << "'";
359
360 state = core::socket::STATE_FATAL;
361 break;
362 }
363
364 onStatus(currentRemoteAddress, state);
365
366 disable();
367 }
368 }
369 } else {
370 PLOG(DEBUG) << config->getInstanceName() << " getsockopt syscall error: '" << remoteAddress.toString() << "'";
371
372 onStatus(remoteAddress, core::socket::STATE_FATAL);
373 disable();
374 }
375 }
static constexpr int NO_RETRY
Definition State.h:59
typename PhysicalClientSocket::SocketAddress SocketAddress
SocketConnectionT< PhysicalClientSocket > SocketConnection
SocketAddress getRemoteSocketAddress(PhysicalSocket &physicalSocket, Config &config)
SocketAddress getLocalSocketAddress(PhysicalSocket &physicalSocket, Config &config)

◆ connectTimeout()

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

Reimplemented from core::eventreceiver::ConnectEventReceiver.

Definition at line 383 of file SocketConnector.hpp.

383 {
384 LOG(TRACE) << config->getInstanceName() << " connect timeout " << remoteAddress.toString();
385
386 SocketAddress currentRemoteAddress = remoteAddress;
387 if (remoteAddress.useNext()) {
388 LOG(DEBUG) << config->getInstanceName() << " using next SocketAddress: '" << config->Remote::getSocketAddress().toString()
389 << "'";
390
392 } else {
393 LOG(DEBUG) << config->getInstanceName() << " connect timeout '" << remoteAddress.toString() << "'";
394 errno = ETIMEDOUT;
395
396 onStatus(currentRemoteAddress, core::socket::STATE_ERROR);
397 }
398
400 }

◆ destruct()

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

Reimplemented from core::EventReceiver.

Definition at line 403 of file SocketConnector.hpp.

403 {
404 delete this;
405 }

◆ init()

template<typename PhysicalSocketClient , typename Config , template< 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 151 of file SocketConnector.hpp.

151 {
152 if (!config->getDisabled()) {
153 try {
154 LOG(TRACE) << config->getInstanceName() << " Starting";
155
156 remoteAddress = config->Remote::getSocketAddress();
157 SocketAddress localAddress = config->Local::getSocketAddress();
158
159 try {
160 core::socket::State state = core::socket::STATE_OK;
161
162 if (physicalClientSocket.open(config->getSocketOptions(), PhysicalClientSocket::Flags::NONBLOCK) < 0) {
163 switch (errno) {
164 case EMFILE:
165 case ENFILE:
166 case ENOBUFS:
167 case ENOMEM:
168 PLOG(DEBUG) << config->getInstanceName() << " open: '" << localAddress.toString() << "'";
169
170 state = core::socket::STATE_ERROR;
171 break;
172 default:
173 PLOG(DEBUG) << config->getInstanceName() << " open: '" << localAddress.toString() << "'";
174
175 state = core::socket::STATE_FATAL;
176 break;
177 }
178
179 onStatus(remoteAddress, state);
180 } else if (physicalClientSocket.bind(localAddress) < 0) {
181 switch (errno) {
182 case EADDRINUSE:
183 PLOG(DEBUG) << config->getInstanceName() << " bind: '" << localAddress.toString() << "'";
184
185 state = core::socket::STATE_ERROR;
186 break;
187 default:
188 PLOG(DEBUG) << config->getInstanceName() << " bind: '" << localAddress.toString() << "'";
189
190 state = core::socket::STATE_FATAL;
191 break;
192 }
193
194 onStatus(remoteAddress, state);
195 } else if (physicalClientSocket.connect(remoteAddress) < 0 && !PhysicalClientSocket::connectInProgress(errno)) {
196 switch (errno) {
197 case EADDRINUSE:
198 case EADDRNOTAVAIL:
199 case ECONNREFUSED:
200 case ENETUNREACH:
201 case ENOENT:
202 case EHOSTDOWN:
203 PLOG(DEBUG) << config->getInstanceName() << " connect: '" << remoteAddress.toString() << "'";
204
205 state = core::socket::STATE_ERROR;
206 break;
207 default:
208 PLOG(DEBUG) << config->getInstanceName() << " connect: '" << remoteAddress.toString() << "'";
209
210 state = core::socket::STATE_FATAL;
211 break;
212 }
213
214 SocketAddress currentRemoteAddress = remoteAddress;
215 if (remoteAddress.useNext()) {
216 onStatus(currentRemoteAddress, state | core::socket::State::NO_RETRY);
217
218 LOG(DEBUG) << config->getInstanceName() << " using next SocketAddress: '"
219 << config->Remote::getSocketAddress().toString() << "'";
220
222 } else {
223 onStatus(currentRemoteAddress, state);
224 }
225 } else if (PhysicalClientSocket::connectInProgress(errno)) {
226 if (enable(physicalClientSocket.getFd())) {
227 LOG(DEBUG) << config->getInstanceName() << " connect in progress: '" << remoteAddress.toString() << "'";
228 } else {
229 LOG(DEBUG) << config->getInstanceName() << " not enabled: '" << remoteAddress.toString() << "'";
230
231 state = core::socket::STATE(core::socket::STATE_FATAL, ECANCELED, "SocketConnector not enabled");
232
233 onStatus(remoteAddress, state);
234 }
235 } else {
236 LOG(DEBUG) << config->getInstanceName() << " [" << physicalClientSocket.getFd() << "] connect success: '"
237 << remoteAddress.toString() << "'";
238
239 onStatus(remoteAddress, core::socket::STATE_OK);
240
241 SocketConnection* socketConnection =
242 new SocketConnection(config->getInstanceName(),
243 std::move(physicalClientSocket),
245 remoteAddress.toString(false),
248 config->getReadTimeout(),
249 config->getWriteTimeout(),
250 config->getReadBlockSize(),
251 config->getWriteBlockSize(),
252 config->getTerminateTimeout());
253
254 onConnect(socketConnection);
255 onConnected(socketConnection);
256 }
257 } catch (const typename SocketAddress::BadSocketAddress& badSocketAddress) {
258 LOG(DEBUG) << config->getInstanceName() << " " << badSocketAddress.what();
259
260 onStatus({}, core::socket::STATE(badSocketAddress.getState(), badSocketAddress.getErrnum(), badSocketAddress.what()));
261 }
262 } catch (const typename SocketAddress::BadSocketAddress& badSocketAddress) {
263 LOG(DEBUG) << config->getInstanceName() << " " << badSocketAddress.what();
264
265 onStatus({}, core::socket::STATE(badSocketAddress.getState(), badSocketAddress.getErrnum(), badSocketAddress.what()));
266 }
267 } else {
268 LOG(DEBUG) << config->getInstanceName() << " disabled";
269
270 onStatus({}, core::socket::STATE_DISABLED);
271 }
272
273 if (isEnabled()) {
275 } else {
276 destruct();
277 }
278 }
void setTimeout(const utils::Timeval &timeout)

◆ unobservedEvent()

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

Implements core::Observer.

Definition at line 378 of file SocketConnector.hpp.

378 {
379 destruct();
380 }

◆ useNextSocketAddress()

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

Member Data Documentation

◆ config

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

Definition at line 110 of file SocketConnector.h.

◆ onConnect

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

Definition at line 104 of file SocketConnector.h.

◆ onConnected

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

Definition at line 105 of file SocketConnector.h.

◆ onDisconnect

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

Definition at line 106 of file SocketConnector.h.

◆ onStatus

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

Definition at line 108 of file SocketConnector.h.

◆ physicalClientSocket

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

Definition at line 98 of file SocketConnector.h.

◆ remoteAddress

template<typename PhysicalSocketClientT , typename ConfigT , template< typename PhysicalSocketClient > typename SocketConnectionT>
SocketAddress core::socket::stream::SocketConnector< PhysicalSocketClientT, ConfigT, SocketConnectionT >::remoteAddress
private

Definition at line 99 of file SocketConnector.h.

◆ socketContextFactory

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

Definition at line 102 of file SocketConnector.h.


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