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::function< void(SocketConnection *)> &onConnect, const std::function< void(SocketConnection *)> &onConnected, const std::function< void(SocketConnection *)> &onDisconnect, const std::function< void(core::eventreceiver::ConnectEventReceiver *)> &onInitState, const std::function< void(const SocketAddress &, core::socket::State)> &onStatus, const std::shared_ptr< Config > &config)
 SocketConnector (const SocketConnector &socketConnector)
 ~SocketConnector () override
virtual void init ()

Protected Types

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

Protected Member Functions

virtual void useNextSocketAddress ()=0
void destruct () final
Protected Member Functions inherited from core::eventreceiver::ConnectEventReceiver
 ConnectEventReceiver (const std::string &name, const utils::Timeval &timeout)
void stopConnect ()
Protected Member Functions inherited from core::DescriptorEventReceiver
 DescriptorEventReceiver (const std::string &name, DescriptorEventPublisher &descriptorEventPublisher, const utils::Timeval &timeout=TIMEOUT::DISABLE)
bool enable (int fd)
void disable ()
void suspend ()
void resume ()
void setTimeout (const utils::Timeval &timeout)
utils::Timeval getTimeout (const utils::Timeval &currentTime) const
void checkTimeout (const utils::Timeval &currentTime)
int getRegisteredFd () const
bool isEnabled () const
bool isSuspended () const
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::function< void(SocketConnection *)> onConnect
std::function< void(SocketConnection *)> onConnected
std::function< void(SocketConnection *)> onDisconnect
std::function< void(core::eventreceiver::ConnectEventReceiver *)> onInitState
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::function< void(SocketConnection *)> & onConnect,
const std::function< void(SocketConnection *)> & onConnected,
const std::function< void(SocketConnection *)> & onDisconnect,
const std::function< void(core::eventreceiver::ConnectEventReceiver *)> & onInitState,
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 }
std::function< void(const SocketAddress &, core::socket::State)> onStatus
std::function< void(SocketConnection *)> onConnected
std::function< void(SocketConnection *)> onDisconnect
std::function< void(core::eventreceiver::ConnectEventReceiver *)> onInitState
std::function< void(SocketConnection *)> onConnect

References config, onConnect, onConnected, onDisconnect, onInitState, and onStatus.

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

◆ ~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 93 of file SocketConnector.hpp.

93 {
94 }

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 236 of file SocketConnector.hpp.

236 {
237 int cErrno = 0;
238
239 if (isEnabled() && physicalClientSocket.getSockError(cErrno) == 0) { // == 0->return valid : < 0->getsockopt failed
240 const utils::PreserveErrno pe(cErrno); // errno = cErrno
241
242 if (errno == 0) {
244
245 LOG(DEBUG) << config->getInstanceName() << " connect " << remoteAddress.toString() << ": success";
246 LOG(DEBUG) << " " << socketConnection->getLocalAddress().toString() << " -> "
247 << socketConnection->getRemoteAddress().toString();
248
250
253
254 disable();
256 LOG(DEBUG) << config->getInstanceName() << " connect " << remoteAddress.toString() << ": in progress:";
257 } else {
259
261
262 switch (errno) {
263 case EADDRINUSE:
264 case EADDRNOTAVAIL:
265 case ECONNREFUSED:
266 case ENETUNREACH:
267 case ENOENT:
268 case EHOSTDOWN:
270 break;
271 default:
273 break;
274 }
275
276 if (remoteAddress.useNext()) {
277 PLOG(DEBUG) << config->getInstanceName() << " connect '" << remoteAddress.toString();
278
280
281 LOG(DEBUG) << config->getInstanceName()
282 << " using next SocketAddress: " << config->Remote::getSocketAddress().toString();
283
285
286 disable();
287 } else {
288 PLOG(DEBUG) << config->getInstanceName() << " connect " << remoteAddress.toString();
289
291
292 disable();
293 }
294 }
295 } else {
296 PLOG(DEBUG) << config->getInstanceName() << " getsockopt syscall error: '" << remoteAddress.toString() << "'";
297
299 disable();
300 }
301 }
typename PhysicalClientSocket::SocketAddress SocketAddress
SocketConnectionT< PhysicalClientSocket, Config > SocketConnection

References config, core::DescriptorEventReceiver::disable(), core::DescriptorEventReceiver::isEnabled(), core::socket::State::NO_RETRY, onConnect, onConnected, onDisconnect, onStatus, core::socket::State::operator|(), physicalClientSocket, utils::PreserveErrno::PreserveErrno(), remoteAddress, and 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 313 of file SocketConnector.hpp.

313 {
314 LOG(TRACE) << config->getInstanceName() << " connect timeout " << remoteAddress.toString();
315
317 if (remoteAddress.useNext()) {
318 LOG(DEBUG) << config->getInstanceName() << " using next SocketAddress: '" << config->Remote::getSocketAddress().toString()
319 << "'";
320
322 } else {
323 LOG(DEBUG) << config->getInstanceName() << " connect timeout '" << remoteAddress.toString() << "'";
325
327 }
328
330 }

References config, core::eventreceiver::ConnectEventReceiver::connectTimeout(), onStatus, remoteAddress, and 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

Reimplemented from core::EventReceiver.

Definition at line 335 of file SocketConnector.hpp.

335 {
336 if (!config->getDisabled()) {
337 onInitState(this);
338 }
339
340 delete this;
341 }

References config, and onInitState.

Referenced by init(), and unobservedEvent().

Here is the caller graph for this function:

◆ init()

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

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

Definition at line 99 of file SocketConnector.hpp.

99 {
100 if (!config->getDisabled()) {
101 try {
103
104 LOG(DEBUG) << config->getInstanceName() << " Connect: starting";
105
106 SocketAddress bindAddress = config->Local::getSocketAddress();
107
108 try {
109 remoteAddress = config->Remote::getSocketAddress();
110
111 if (physicalClientSocket.open(config->getSocketOptions(), PhysicalClientSocket::Flags::NONBLOCK) < 0) {
112 PLOG(DEBUG) << config->getInstanceName() << " open " << bindAddress.toString();
113
114 switch (errno) {
115 case EMFILE:
116 case ENFILE:
117 case ENOBUFS:
118 case ENOMEM:
120 break;
121 default:
123 break;
124 }
125
127 } else {
128 LOG(TRACE) << config->getInstanceName() << " open " << bindAddress.toString() << ": success";
129
130 if (physicalClientSocket.bind(bindAddress) < 0) {
131 PLOG(DEBUG) << config->getInstanceName() << " bind " << bindAddress.toString();
132
133 switch (errno) {
134 case EADDRINUSE:
136 break;
137 default:
139 break;
140 }
141
143 } else {
144 LOG(TRACE) << config->getInstanceName() << " bind " << bindAddress.toString() << ": success";
145
147 PLOG(DEBUG) << config->getInstanceName() << " connect " << remoteAddress.toString();
148 switch (errno) {
149 case EADDRINUSE:
150 case EADDRNOTAVAIL:
151 case ECONNREFUSED:
152 case ENETUNREACH:
153 case ENOENT:
154 case EHOSTDOWN:
156 break;
157 default:
159 break;
160 }
161
163 if (remoteAddress.useNext()) {
165
166 LOG(INFO) << config->getInstanceName() << ": Using next SocketAddress: " << remoteAddress.toString();
167
169 } else {
171 }
172 } else {
173 LOG(TRACE) << config->getInstanceName() << " connect " << remoteAddress.toString() << ": success";
174
176 if (enable(physicalClientSocket.getFd())) {
177 LOG(DEBUG)
178 << config->getInstanceName() << " enable " << remoteAddress.toString(false) << ": success";
179 } else {
180 LOG(ERROR) << config->getInstanceName() << " enable " << remoteAddress.toString()
181 << ": failed. No valid descriptor created";
182
183 state = core::socket::STATE(core::socket::STATE_FATAL, ECANCELED, "SocketConnector not enabled");
184
186 }
187 } else {
190
191 LOG(DEBUG) << config->getInstanceName() << " connect " << remoteAddress.toString() << ": success";
192 LOG(DEBUG) << " " << socketConnection->getLocalAddress().toString() << " -> "
193 << socketConnection->getRemoteAddress().toString();
194
196
199 }
200 }
201 }
202 }
203 } catch (const typename SocketAddress::BadSocketAddress& badSocketAddress) {
206
207 LOG(ERROR) << state.what();
208
209 onStatus({}, state);
210 }
211 } catch (const typename SocketAddress::BadSocketAddress& badSocketAddress) {
214
215 LOG(ERROR) << state.what();
216
217 onStatus({}, state);
218 }
219 } else {
220 LOG(DEBUG) << config->getInstanceName() << ": disabled";
221
223 }
224
225 if (isEnabled()) {
226 onInitState(this);
228 } else {
229 destruct();
230 }
231 }
void setTimeout(const utils::Timeval &timeout)

References config, destruct(), core::DescriptorEventReceiver::enable(), core::DescriptorEventReceiver::isEnabled(), core::socket::State::NO_RETRY, onConnect, onConnected, onDisconnect, onInitState, onStatus, core::socket::State::operator|(), physicalClientSocket, remoteAddress, core::DescriptorEventReceiver::setTimeout(), useNextSocketAddress(), and core::socket::State::what().

Here is the call 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 306 of file SocketConnector.hpp.

306 {
307 destruct();
308 }

References 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

Implemented in core::socket::stream::legacy::SocketConnector< PhysicalClientSocketT, ConfigT >, and core::socket::stream::tls::SocketConnector< PhysicalSocketClientT, ConfigT >.

Referenced by connectEvent(), connectTimeout(), and init().

Here is the caller graph for this function:

Member Data Documentation

◆ config

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

◆ onConnect

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

Definition at line 104 of file SocketConnector.h.

Referenced by connectEvent(), init(), SocketConnector(), and SocketConnector().

◆ 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

Definition at line 105 of file SocketConnector.h.

Referenced by connectEvent(), init(), SocketConnector(), and SocketConnector().

◆ 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

Definition at line 106 of file SocketConnector.h.

Referenced by connectEvent(), init(), SocketConnector(), and SocketConnector().

◆ onInitState

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

Definition at line 107 of file SocketConnector.h.

Referenced by destruct(), init(), SocketConnector(), and SocketConnector().

◆ 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

Definition at line 100 of file SocketConnector.h.

Referenced by connectEvent(), and init().

◆ remoteAddress

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

Definition at line 101 of file SocketConnector.h.

Referenced by connectEvent(), connectTimeout(), and init().


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