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(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)
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::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::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.

67 : core::eventreceiver::ConnectEventReceiver(config->getInstanceName() + " SocketConnector", 0)
72 , config(config) {
73 }
std::function< void(const SocketAddress &, core::socket::State)> onStatus
std::function< void(SocketConnection *)> onConnected
std::function< void(SocketConnection *)> onDisconnect
std::function< void(SocketConnection *)> onConnect

References config, onConnect, onConnected, onDisconnect, 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 90 of file SocketConnector.hpp.

90 {
91 }

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

232 {
233 int cErrno = 0;
234
235 if (physicalClientSocket.getSockError(cErrno) == 0) { // == 0->return valid : < 0->getsockopt failed
236 const utils::PreserveErrno pe(cErrno); // errno = cErrno
237
238 if (errno == 0) {
240
241 LOG(DEBUG) << config->getInstanceName() << " connect " << remoteAddress.toString() << ": success";
242 LOG(DEBUG) << " " << socketConnection->getLocalAddress().toString() << " -> "
243 << socketConnection->getRemoteAddress().toString();
244
246
249
250 disable();
252 LOG(DEBUG) << config->getInstanceName() << " connect " << remoteAddress.toString() << ": in progress:";
253 } else {
255
257
258 switch (errno) {
259 case EADDRINUSE:
260 case EADDRNOTAVAIL:
261 case ECONNREFUSED:
262 case ENETUNREACH:
263 case ENOENT:
264 case EHOSTDOWN:
266 break;
267 default:
269 break;
270 }
271
272 if (remoteAddress.useNext()) {
273 PLOG(DEBUG) << config->getInstanceName() << " connect '" << remoteAddress.toString();
274
276
277 LOG(DEBUG) << config->getInstanceName()
278 << " using next SocketAddress: " << config->Remote::getSocketAddress().toString();
279
281
282 disable();
283 } else {
284 PLOG(DEBUG) << config->getInstanceName() << " connect " << remoteAddress.toString();
285
287
288 disable();
289 }
290 }
291 } else {
292 PLOG(DEBUG) << config->getInstanceName() << " getsockopt syscall error: '" << remoteAddress.toString() << "'";
293
295 disable();
296 }
297 }
typename PhysicalClientSocket::SocketAddress SocketAddress
SocketConnectionT< PhysicalClientSocket, Config > SocketConnection

References config, core::DescriptorEventReceiver::disable(), 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 309 of file SocketConnector.hpp.

309 {
310 LOG(TRACE) << config->getInstanceName() << " connect timeout " << remoteAddress.toString();
311
313 if (remoteAddress.useNext()) {
314 LOG(DEBUG) << config->getInstanceName() << " using next SocketAddress: '" << config->Remote::getSocketAddress().toString()
315 << "'";
316
318 } else {
319 LOG(DEBUG) << config->getInstanceName() << " connect timeout '" << remoteAddress.toString() << "'";
321
323 }
324
326 }

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

331 {
332 delete this;
333 }

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

96 {
97 if (!config->getDisabled()) {
98 try {
100
101 LOG(DEBUG) << config->getInstanceName() << " Connect: starting";
102
103 SocketAddress bindAddress = config->Local::getSocketAddress();
104
105 try {
106 remoteAddress = config->Remote::getSocketAddress();
107
108 if (physicalClientSocket.open(config->getSocketOptions(), PhysicalClientSocket::Flags::NONBLOCK) < 0) {
109 PLOG(DEBUG) << config->getInstanceName() << " open " << bindAddress.toString();
110
111 switch (errno) {
112 case EMFILE:
113 case ENFILE:
114 case ENOBUFS:
115 case ENOMEM:
117 break;
118 default:
120 break;
121 }
122
124 } else {
125 LOG(TRACE) << config->getInstanceName() << " open " << bindAddress.toString() << ": success";
126
127 if (physicalClientSocket.bind(bindAddress) < 0) {
128 PLOG(DEBUG) << config->getInstanceName() << " bind " << bindAddress.toString();
129
130 switch (errno) {
131 case EADDRINUSE:
133 break;
134 default:
136 break;
137 }
138
140 } else {
141 LOG(TRACE) << config->getInstanceName() << " bind " << bindAddress.toString() << ": success";
142
144 PLOG(DEBUG) << config->getInstanceName() << " connect " << remoteAddress.toString();
145 switch (errno) {
146 case EADDRINUSE:
147 case EADDRNOTAVAIL:
148 case ECONNREFUSED:
149 case ENETUNREACH:
150 case ENOENT:
151 case EHOSTDOWN:
153 break;
154 default:
156 break;
157 }
158
160 if (remoteAddress.useNext()) {
162
163 LOG(INFO) << config->getInstanceName() << ": Using next SocketAddress: " << remoteAddress.toString();
164
166 } else {
168 }
169 } else {
170 LOG(TRACE) << config->getInstanceName() << " connect " << remoteAddress.toString() << ": success";
171
173 if (enable(physicalClientSocket.getFd())) {
174 LOG(DEBUG)
175 << config->getInstanceName() << " enable " << remoteAddress.toString(false) << ": success";
176 } else {
177 LOG(ERROR) << config->getInstanceName() << " enable " << remoteAddress.toString()
178 << ": failed. No valid descriptor created";
179
180 state = core::socket::STATE(core::socket::STATE_FATAL, ECANCELED, "SocketConnector not enabled");
181
183 }
184 } else {
187
188 LOG(DEBUG) << config->getInstanceName() << " connect " << remoteAddress.toString() << ": success";
189 LOG(DEBUG) << " " << socketConnection->getLocalAddress().toString() << " -> "
190 << socketConnection->getRemoteAddress().toString();
191
193
196 }
197 }
198 }
199 }
200 } catch (const typename SocketAddress::BadSocketAddress& badSocketAddress) {
203
204 LOG(ERROR) << state.what();
205
206 onStatus({}, state);
207 }
208 } catch (const typename SocketAddress::BadSocketAddress& badSocketAddress) {
211
212 LOG(ERROR) << state.what();
213
214 onStatus({}, state);
215 }
216 } else {
217 LOG(DEBUG) << config->getInstanceName() << ": disabled";
218
220 }
221
222 if (isEnabled()) {
224 } else {
225 destruct();
226 }
227 }
void setTimeout(const utils::Timeval &timeout)

References config, destruct(), core::DescriptorEventReceiver::enable(), core::DescriptorEventReceiver::isEnabled(), core::socket::State::NO_RETRY, onConnect, onConnected, onDisconnect, 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 302 of file SocketConnector.hpp.

302 {
303 destruct();
304 }

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 103 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 104 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 105 of file SocketConnector.h.

Referenced by connectEvent(), 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 99 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 100 of file SocketConnector.h.

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


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