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 40 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 45 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 42 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 46 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 47 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 83 of file SocketConnector.hpp.

90 : core::eventreceiver::ConnectEventReceiver(config->getInstanceName() + " SocketConnector", 0)
96 , config(config) {
97 atNextTick([this]() {
98 init();
99 });
100 }
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

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

104 : core::eventreceiver::ConnectEventReceiver(socketConnector.config->getInstanceName() + " SocketConnector", 0)
105 , socketContextFactory(socketConnector.socketContextFactory)
106 , onConnect(socketConnector.onConnect)
107 , onConnected(socketConnector.onConnected)
108 , onDisconnect(socketConnector.onDisconnect)
109 , onStatus(socketConnector.onStatus)
110 , config(socketConnector.config) {
111 atNextTick([this]() {
112 init();
113 });
114 }

◆ ~SocketConnector()

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

Definition at line 117 of file SocketConnector.hpp.

117 {
118 }

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

250 {
251 int cErrno = 0;
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 LOG(DEBUG) << config->getInstanceName() << " [" << physicalClientSocket.getFd() << "] connect success: '"
257 << remoteAddress.toString() << "'";
258
259 onStatus(remoteAddress, core::socket::STATE_OK);
260
261 SocketConnection* socketConnection =
262 new SocketConnection(config->getInstanceName(),
263 std::move(physicalClientSocket),
265 remoteAddress.toString(false),
268 config->getReadTimeout(),
269 config->getWriteTimeout(),
270 config->getReadBlockSize(),
271 config->getWriteBlockSize(),
272 config->getTerminateTimeout());
273
274 onConnect(socketConnection);
275 onConnected(socketConnection);
276
277 disable();
278 } else if (PhysicalClientSocket::connectInProgress(errno)) {
279 LOG(TRACE) << config->getInstanceName() << " connect still in progress: '" << remoteAddress.toString() << "'";
280 } else if (remoteAddress.useNext()) {
281 core::socket::State state = core::socket::STATE_OK;
282
283 switch (errno) {
284 case EADDRINUSE:
285 case EADDRNOTAVAIL:
286 case ECONNREFUSED:
287 case ENETUNREACH:
288 case ENOENT:
289 case EHOSTDOWN:
290 PLOG(DEBUG) << config->getInstanceName() << " connect: '" << remoteAddress.toString() << "'";
291
292 state = core::socket::STATE_ERROR;
293 break;
294 default:
295 PLOG(DEBUG) << config->getInstanceName() << ": connect: '" << remoteAddress.toString() << "'";
296
297 state = core::socket::STATE_FATAL;
298 break;
299 }
300
302
303 LOG(DEBUG) << config->getInstanceName() << " using next SocketAddress: '" << config->Remote::getSocketAddress().toString()
304 << "'";
305
307
308 disable();
309 } else {
310 core::socket::State state = core::socket::STATE_OK;
311
312 switch (errno) {
313 case EADDRINUSE:
314 case EADDRNOTAVAIL:
315 case ECONNREFUSED:
316 case ENETUNREACH:
317 case ENOENT:
318 case EHOSTDOWN:
319 PLOG(DEBUG) << config->getInstanceName() << " connect: '" << remoteAddress.toString() << "'";
320
321 state = core::socket::STATE_ERROR;
322 break;
323 default:
324 PLOG(DEBUG) << config->getInstanceName() << " connect: '" << remoteAddress.toString() << "'";
325
326 state = core::socket::STATE_FATAL;
327 break;
328 }
329
330 onStatus(remoteAddress, state);
331
332 disable();
333 }
334 } else {
335 PLOG(DEBUG) << config->getInstanceName() << " getsockopt syscall error: '" << remoteAddress.toString() << "'";
336
337 onStatus(remoteAddress, core::socket::STATE_FATAL);
338 disable();
339 }
340 }
static constexpr int NO_RETRY
Definition State.h:37
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 348 of file SocketConnector.hpp.

348 {
349 LOG(TRACE) << config->getInstanceName() << " connect timeout " << remoteAddress.toString();
350
351 if (remoteAddress.useNext()) {
352 LOG(DEBUG) << config->getInstanceName() << " using next SocketAddress: '" << config->Remote::getSocketAddress().toString()
353 << "'";
354
356 } else {
357 LOG(DEBUG) << config->getInstanceName() << " connect timeout '" << remoteAddress.toString() << "'";
358 errno = ETIMEDOUT;
359
360 onStatus(remoteAddress, core::socket::STATE_ERROR);
361 }
362
364 }

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

367 {
368 delete this;
369 }

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

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

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

343 {
344 destruct();
345 }

◆ 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 88 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 82 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 83 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 84 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 86 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 76 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 77 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 80 of file SocketConnector.h.


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