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

234 {
235 int cErrno = 0;
236
237 if (physicalClientSocket.getSockError(cErrno) == 0) { // == 0->return valid : < 0->getsockopt failed
238 const utils::PreserveErrno pe(cErrno); // errno = cErrno
239
240 if (errno == 0) {
241 LOG(DEBUG) << config->getInstanceName() << " [" << physicalClientSocket.getFd() << "] connect success: '"
242 << remoteAddress.toString() << "'";
243
244 onStatus(remoteAddress, core::socket::STATE_OK);
245
246 SocketConnection* socketConnection = new SocketConnection(std::move(physicalClientSocket), onDisconnect, config);
247
248 onConnect(socketConnection);
249 onConnected(socketConnection);
250
251 disable();
252 } else if (PhysicalClientSocket::connectInProgress(errno)) {
253 LOG(TRACE) << config->getInstanceName() << " connect still in progress: '" << remoteAddress.toString() << "'";
254 } else {
255 SocketAddress currentRemoteAddress = remoteAddress;
256 if (remoteAddress.useNext()) {
257 core::socket::State state = core::socket::STATE_OK;
258
259 switch (errno) {
260 case EADDRINUSE:
261 case EADDRNOTAVAIL:
262 case ECONNREFUSED:
263 case ENETUNREACH:
264 case ENOENT:
265 case EHOSTDOWN:
266 PLOG(DEBUG) << config->getInstanceName() << " connect: '" << remoteAddress.toString() << "'";
267
268 state = core::socket::STATE_ERROR;
269 break;
270 default:
271 PLOG(DEBUG) << config->getInstanceName() << ": connect: '" << remoteAddress.toString() << "'";
272
273 state = core::socket::STATE_FATAL;
274 break;
275 }
276
277 onStatus(currentRemoteAddress, (state | core::socket::State::NO_RETRY));
278
279 LOG(DEBUG) << config->getInstanceName() << " using next SocketAddress: '"
280 << config->Remote::getSocketAddress().toString() << "'";
281
283
284 disable();
285 } else {
286 core::socket::State state = core::socket::STATE_OK;
287
288 switch (errno) {
289 case EADDRINUSE:
290 case EADDRNOTAVAIL:
291 case ECONNREFUSED:
292 case ENETUNREACH:
293 case ENOENT:
294 case EHOSTDOWN:
295 PLOG(DEBUG) << config->getInstanceName() << " connect: '" << remoteAddress.toString() << "'";
296
297 state = core::socket::STATE_ERROR;
298 break;
299 default:
300 PLOG(DEBUG) << config->getInstanceName() << " connect: '" << remoteAddress.toString() << "'";
301
302 state = core::socket::STATE_FATAL;
303 break;
304 }
305
306 onStatus(currentRemoteAddress, state);
307
308 disable();
309 }
310 }
311 } else {
312 PLOG(DEBUG) << config->getInstanceName() << " getsockopt syscall error: '" << remoteAddress.toString() << "'";
313
314 onStatus(remoteAddress, core::socket::STATE_FATAL);
315 disable();
316 }
317 }
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 329 of file SocketConnector.hpp.

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

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 LOG(TRACE) << config->getInstanceName() << " Starting";
117
118 remoteAddress = config->Remote::getSocketAddress();
119 SocketAddress localAddress = config->Local::getSocketAddress();
120
121 try {
122 core::socket::State state = core::socket::STATE_OK;
123
124 if (physicalClientSocket.open(config->getSocketOptions(), PhysicalClientSocket::Flags::NONBLOCK) < 0) {
125 switch (errno) {
126 case EMFILE:
127 case ENFILE:
128 case ENOBUFS:
129 case ENOMEM:
130 PLOG(DEBUG) << config->getInstanceName() << " open: '" << localAddress.toString() << "'";
131
132 state = core::socket::STATE_ERROR;
133 break;
134 default:
135 PLOG(DEBUG) << config->getInstanceName() << " open: '" << localAddress.toString() << "'";
136
137 state = core::socket::STATE_FATAL;
138 break;
139 }
140
141 onStatus(remoteAddress, state);
142 } else if (physicalClientSocket.bind(localAddress) < 0) {
143 switch (errno) {
144 case EADDRINUSE:
145 PLOG(DEBUG) << config->getInstanceName() << " bind: '" << localAddress.toString() << "'";
146
147 state = core::socket::STATE_ERROR;
148 break;
149 default:
150 PLOG(DEBUG) << config->getInstanceName() << " bind: '" << localAddress.toString() << "'";
151
152 state = core::socket::STATE_FATAL;
153 break;
154 }
155
156 onStatus(remoteAddress, state);
157 } else if (physicalClientSocket.connect(remoteAddress) < 0 && !PhysicalClientSocket::connectInProgress(errno)) {
158 switch (errno) {
159 case EADDRINUSE:
160 case EADDRNOTAVAIL:
161 case ECONNREFUSED:
162 case ENETUNREACH:
163 case ENOENT:
164 case EHOSTDOWN:
165 PLOG(DEBUG) << config->getInstanceName() << " connect: '" << remoteAddress.toString() << "'";
166
167 state = core::socket::STATE_ERROR;
168 break;
169 default:
170 PLOG(DEBUG) << config->getInstanceName() << " connect: '" << remoteAddress.toString() << "'";
171
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: '"
181 << config->Remote::getSocketAddress().toString() << "'";
182
184 } else {
185 onStatus(currentRemoteAddress, state);
186 }
187 } else if (PhysicalClientSocket::connectInProgress(errno)) {
188 if (enable(physicalClientSocket.getFd())) {
189 LOG(DEBUG) << config->getInstanceName() << " connect in progress: '" << remoteAddress.toString() << "'";
190 } else {
191 LOG(DEBUG) << config->getInstanceName() << " not enabled: '" << remoteAddress.toString() << "'";
192
193 state = core::socket::STATE(core::socket::STATE_FATAL, ECANCELED, "SocketConnector not enabled");
194
195 onStatus(remoteAddress, state);
196 }
197 } else {
198 LOG(DEBUG) << config->getInstanceName() << " [" << physicalClientSocket.getFd() << "] connect success: '"
199 << remoteAddress.toString() << "'";
200
201 onStatus(remoteAddress, core::socket::STATE_OK);
202
203 SocketConnection* socketConnection = new SocketConnection(std::move(physicalClientSocket), onDisconnect, config);
204
205 onConnect(socketConnection);
206 onConnected(socketConnection);
207 }
208 } catch (const typename SocketAddress::BadSocketAddress& badSocketAddress) {
209 LOG(DEBUG) << config->getInstanceName() << " " << badSocketAddress.what();
210
211 onStatus({}, core::socket::STATE(badSocketAddress.getState(), badSocketAddress.getErrnum(), badSocketAddress.what()));
212 }
213 } catch (const typename SocketAddress::BadSocketAddress& badSocketAddress) {
214 LOG(DEBUG) << config->getInstanceName() << " " << badSocketAddress.what();
215
216 onStatus({}, core::socket::STATE(badSocketAddress.getState(), badSocketAddress.getErrnum(), badSocketAddress.what()));
217 }
218 } else {
219 LOG(DEBUG) << config->getInstanceName() << " disabled";
220
221 onStatus({}, core::socket::STATE_DISABLED);
222 }
223
224 if (isEnabled()) {
226 } else {
227 destruct();
228 }
229 }
void setTimeout(const utils::Timeval &timeout)

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(), and core::socket::stream::SocketConnector< PhysicalSocketClientT, ConfigT, SocketConnectionT >::useNextSocketAddress().

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

322 {
323 destruct();
324 }

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: