SNode.C
Loading...
Searching...
No Matches
core::socket::stream::SocketServer< SocketAcceptorT, SocketContextFactoryT, Args > Class Template Reference

#include <SocketServer.h>

Inheritance diagram for core::socket::stream::SocketServer< SocketAcceptorT, SocketContextFactoryT, Args >:
Collaboration diagram for core::socket::stream::SocketServer< SocketAcceptorT, SocketContextFactoryT, Args >:

Classes

struct  Context

Public Types

using SocketConnection = typename SocketAcceptor::SocketConnection
using SocketAddress = typename SocketAcceptor::SocketAddress
using Config = typename SocketAcceptor::Config
Public Types inherited from core::socket::Socket< SocketAcceptorT::Config >
using Config

Public Member Functions

 SocketServer (const std::string &name, const std::function< void(SocketConnection *)> &onConnect, const std::function< void(SocketConnection *)> &onConnected, const std::function< void(SocketConnection *)> &onDisconnect, Args &&... args)
 SocketServer (const std::function< void(SocketConnection *)> &onConnect, const std::function< void(SocketConnection *)> &onConnected, const std::function< void(SocketConnection *)> &onDisconnect, Args &&... args)
 SocketServer (const std::string &name, Args &&... args)
 SocketServer (Args &&... args)
const SocketServerlisten (const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const
const SocketServerlisten (const SocketAddress &localAddress, const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const
const SocketServerlisten (const SocketAddress &localAddress, int backlog, const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const
std::function< void(SocketConnection *)> & getOnConnect () const
const SocketServersetOnConnect (const std::function< void(SocketConnection *)> &onConnect, bool initialize=false) const
std::function< void(SocketConnection *)> & getOnConnected () const
const SocketServersetOnConnected (const std::function< void(SocketConnection *)> &onConnected, bool initialize=false) const
std::function< void(SocketConnection *)> & getOnDisconnect () const
SocketServersetOnDisconnect (const std::function< void(SocketConnection *)> &onDisconnect, bool initialize=false)
std::function< void(const std::shared_ptr< AutoConnectControl > &)> & getOnAutoConnectControl ()
const SocketServersetOnAutoConnectControl (const std::function< void(const std::shared_ptr< AutoConnectControl > &)> &onAutoConnectControl, bool initialize=false) const
std::function< void(core::eventreceiver::AcceptEventReceiver *)> & getOnInitState () const
const SocketServersetOnInitState (const std::function< void(core::eventreceiver::AcceptEventReceiver *)> &onInitState, bool initialize=false) const
std::shared_ptr< SocketContextFactorygetSocketContextFactory () const
Public Member Functions inherited from core::socket::Socket< SocketAcceptorT::Config >
 Socket (const std::string &name)
virtual ~Socket ()
ConfiggetConfig () const

Private Types

using SocketAcceptor = SocketAcceptorT
using SocketContextFactory = SocketContextFactoryT
using Super = core::socket::Socket<typename SocketAcceptor::Config>

Private Member Functions

 SocketServer (const std::shared_ptr< Config > &config, const std::shared_ptr< SocketContextFactory > &socketContextFactory, const std::function< void(SocketConnection *)> onConnect, const std::function< void(SocketConnection *)> onConnected, const std::function< void(SocketConnection *)> onDisconnect)
 SocketServer (const std::shared_ptr< Config > &config, const std::shared_ptr< Context > &sharedContext)
const SocketServerrealListen (const std::function< void(const SocketAddress &, core::socket::State)> &onStatus, unsigned int tries, double retryTimeoutScale) const

Private Attributes

std::shared_ptr< ContextsharedContext

Additional Inherited Members

Protected Member Functions inherited from core::socket::Socket< SocketAcceptorT::Config >
Socketoperator= (const Socket &)=default
Protected Attributes inherited from core::socket::Socket< SocketAcceptorT::Config >
const std::shared_ptr< Configconfig

Detailed Description

template<typename SocketAcceptorT, typename SocketContextFactoryT, typename... Args>
requires std::is_base_of_v<core::eventreceiver::AcceptEventReceiver, SocketAcceptorT> && std::is_base_of_v<core::socket::stream::SocketContextFactory, SocketContextFactoryT>
class core::socket::stream::SocketServer< SocketAcceptorT, SocketContextFactoryT, Args >

Definition at line 68 of file SocketServer.h.

Member Typedef Documentation

◆ Config

template<typename SocketAcceptorT, typename SocketContextFactoryT, typename... Args>
using core::socket::stream::SocketServer< SocketAcceptorT, SocketContextFactoryT, Args >::Config = typename SocketAcceptor::Config

Definition at line 78 of file SocketServer.h.

◆ SocketAcceptor

template<typename SocketAcceptorT, typename SocketContextFactoryT, typename... Args>
using core::socket::stream::SocketServer< SocketAcceptorT, SocketContextFactoryT, Args >::SocketAcceptor = SocketAcceptorT
private

Definition at line 70 of file SocketServer.h.

◆ SocketAddress

template<typename SocketAcceptorT, typename SocketContextFactoryT, typename... Args>
using core::socket::stream::SocketServer< SocketAcceptorT, SocketContextFactoryT, Args >::SocketAddress = typename SocketAcceptor::SocketAddress

Definition at line 77 of file SocketServer.h.

◆ SocketConnection

template<typename SocketAcceptorT, typename SocketContextFactoryT, typename... Args>
using core::socket::stream::SocketServer< SocketAcceptorT, SocketContextFactoryT, Args >::SocketConnection = typename SocketAcceptor::SocketConnection

Definition at line 76 of file SocketServer.h.

◆ SocketContextFactory

template<typename SocketAcceptorT, typename SocketContextFactoryT, typename... Args>
using core::socket::stream::SocketServer< SocketAcceptorT, SocketContextFactoryT, Args >::SocketContextFactory = SocketContextFactoryT
private

Definition at line 71 of file SocketServer.h.

◆ Super

template<typename SocketAcceptorT, typename SocketContextFactoryT, typename... Args>
using core::socket::stream::SocketServer< SocketAcceptorT, SocketContextFactoryT, Args >::Super = core::socket::Socket<typename SocketAcceptor::Config>
private

Definition at line 73 of file SocketServer.h.

Constructor & Destructor Documentation

◆ SocketServer() [1/6]

template<typename SocketAcceptorT, typename SocketContextFactoryT, typename... Args>
core::socket::stream::SocketServer< SocketAcceptorT, SocketContextFactoryT, Args >::SocketServer ( const std::shared_ptr< Config > & config,
const std::shared_ptr< SocketContextFactory > & socketContextFactory,
const std::function< void(SocketConnection *)> onConnect,
const std::function< void(SocketConnection *)> onConnected,
const std::function< void(SocketConnection *)> onDisconnect )
inlineprivate

Definition at line 107 of file SocketServer.h.

References sharedContext.

◆ SocketServer() [2/6]

template<typename SocketAcceptorT, typename SocketContextFactoryT, typename... Args>
core::socket::stream::SocketServer< SocketAcceptorT, SocketContextFactoryT, Args >::SocketServer ( const std::shared_ptr< Config > & config,
const std::shared_ptr< Context > & sharedContext )
inlineprivate

Definition at line 116 of file SocketServer.h.

117 : Super(config)
119 }

References sharedContext.

◆ SocketServer() [3/6]

template<typename SocketAcceptorT, typename SocketContextFactoryT, typename... Args>
core::socket::stream::SocketServer< SocketAcceptorT, SocketContextFactoryT, Args >::SocketServer ( const std::string & name,
const std::function< void(SocketConnection *)> & onConnect,
const std::function< void(SocketConnection *)> & onConnected,
const std::function< void(SocketConnection *)> & onDisconnect,
Args &&... args )
inline

Definition at line 122 of file SocketServer.h.

127 : Super(name)
131 LOG(DEBUG) << socketConnection->getConnectionName() << ": OnConnect";
132
133 LOG(DEBUG) << " Local: " << socketConnection->getLocalAddress().toString();
134 LOG(DEBUG) << " Peer: " << socketConnection->getRemoteAddress().toString();
135
136 if (onConnect) {
138 }
139 },
141 LOG(DEBUG) << socketConnection->getConnectionName() << ": OnConnected";
142
143 LOG(DEBUG) << " Local: " << socketConnection->getLocalAddress().toString();
144 LOG(DEBUG) << " Peer: " << socketConnection->getRemoteAddress().toString();
145
146 if (onConnected) {
148 }
149 },
151 LOG(DEBUG) << socketConnection->getConnectionName() << ": OnDisconnect";
152
153 LOG(DEBUG) << " Local: " << socketConnection->getLocalAddress().toString();
154 LOG(DEBUG) << " Peer: " << socketConnection->getRemoteAddress().toString();
155
156 LOG(DEBUG) << " Online Since: " << socketConnection->getOnlineSince();
157 LOG(DEBUG) << " Online Duration: " << socketConnection->getOnlineDuration();
158
159 LOG(DEBUG) << " Total Queued: " << socketConnection->getTotalQueued();
160 LOG(DEBUG) << " Total Sent: " << socketConnection->getTotalSent();
161 LOG(DEBUG) << " Write Delta: " << socketConnection->getTotalQueued() - socketConnection->getTotalSent();
162 LOG(DEBUG) << " Total Read: " << socketConnection->getTotalRead();
163 LOG(DEBUG) << " Total Processed: " << socketConnection->getTotalProcessed();
164 LOG(DEBUG) << " Read Delta: " << socketConnection->getTotalRead() - socketConnection->getTotalProcessed();
165
166 if (onDisconnect) {
168 }
169 })) {
170 }
typename SocketAcceptor::SocketConnection SocketConnection

References sharedContext.

◆ SocketServer() [4/6]

template<typename SocketAcceptorT, typename SocketContextFactoryT, typename... Args>
core::socket::stream::SocketServer< SocketAcceptorT, SocketContextFactoryT, Args >::SocketServer ( const std::function< void(SocketConnection *)> & onConnect,
const std::function< void(SocketConnection *)> & onConnected,
const std::function< void(SocketConnection *)> & onDisconnect,
Args &&... args )
inline

Definition at line 172 of file SocketServer.h.

177 }
SocketServer(const std::shared_ptr< Config > &config, const std::shared_ptr< SocketContextFactory > &socketContextFactory, const std::function< void(SocketConnection *)> onConnect, const std::function< void(SocketConnection *)> onConnected, const std::function< void(SocketConnection *)> onDisconnect)

◆ SocketServer() [5/6]

template<typename SocketAcceptorT, typename SocketContextFactoryT, typename... Args>
core::socket::stream::SocketServer< SocketAcceptorT, SocketContextFactoryT, Args >::SocketServer ( const std::string & name,
Args &&... args )
inline

Definition at line 179 of file SocketServer.h.

180 : SocketServer(name, {}, {}, {}, std::forward<Args>(args)...) {
181 }

◆ SocketServer() [6/6]

template<typename SocketAcceptorT, typename SocketContextFactoryT, typename... Args>
core::socket::stream::SocketServer< SocketAcceptorT, SocketContextFactoryT, Args >::SocketServer ( Args &&... args)
inlineexplicit

Definition at line 183 of file SocketServer.h.

185 }

Member Function Documentation

◆ getOnAutoConnectControl()

template<typename SocketAcceptorT, typename SocketContextFactoryT, typename... Args>
std::function< void(const std::shared_ptr< AutoConnectControl > &)> & core::socket::stream::SocketServer< SocketAcceptorT, SocketContextFactoryT, Args >::getOnAutoConnectControl ( )
inline

Definition at line 317 of file SocketServer.h.

317 {
318 return sharedContext->onAutoConnectControl;
319 }

References sharedContext.

◆ getOnConnect()

template<typename SocketAcceptorT, typename SocketContextFactoryT, typename... Args>
std::function< void(SocketConnection *)> & core::socket::stream::SocketServer< SocketAcceptorT, SocketContextFactoryT, Args >::getOnConnect ( ) const
inline

Definition at line 274 of file SocketServer.h.

274 {
275 return sharedContext->onConnect;
276 }

References sharedContext.

◆ getOnConnected()

template<typename SocketAcceptorT, typename SocketContextFactoryT, typename... Args>
std::function< void(SocketConnection *)> & core::socket::stream::SocketServer< SocketAcceptorT, SocketContextFactoryT, Args >::getOnConnected ( ) const
inline

Definition at line 288 of file SocketServer.h.

288 {
289 return sharedContext->onConnected;
290 }

References sharedContext.

◆ getOnDisconnect()

template<typename SocketAcceptorT, typename SocketContextFactoryT, typename... Args>
std::function< void(SocketConnection *)> & core::socket::stream::SocketServer< SocketAcceptorT, SocketContextFactoryT, Args >::getOnDisconnect ( ) const
inline

Definition at line 302 of file SocketServer.h.

302 {
303 return sharedContext->onDisconnect;
304 }

References sharedContext.

◆ getOnInitState()

template<typename SocketAcceptorT, typename SocketContextFactoryT, typename... Args>
std::function< void(core::eventreceiver::AcceptEventReceiver *)> & core::socket::stream::SocketServer< SocketAcceptorT, SocketContextFactoryT, Args >::getOnInitState ( ) const
inline

Definition at line 340 of file SocketServer.h.

340 {
341 return sharedContext->onInitState;
342 }

References sharedContext.

◆ getSocketContextFactory()

template<typename SocketAcceptorT, typename SocketContextFactoryT, typename... Args>
std::shared_ptr< SocketContextFactory > core::socket::stream::SocketServer< SocketAcceptorT, SocketContextFactoryT, Args >::getSocketContextFactory ( ) const
inline

Definition at line 356 of file SocketServer.h.

356 {
357 return sharedContext->socketContextFactory;
358 }

References sharedContext.

◆ listen() [1/3]

template<typename SocketAcceptorT, typename SocketContextFactoryT, typename... Args>
const SocketServer & core::socket::stream::SocketServer< SocketAcceptorT, SocketContextFactoryT, Args >::listen ( const SocketAddress & localAddress,
const std::function< void(const SocketAddress &, core::socket::State)> & onStatus ) const
inline

Definition at line 259 of file SocketServer.h.

260 {
261 Super::config->Local::setSocketAddress(localAddress);
262
263 return listen(onStatus);
264 }
const SocketServer & listen(const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const

◆ listen() [2/3]

template<typename SocketAcceptorT, typename SocketContextFactoryT, typename... Args>
const SocketServer & core::socket::stream::SocketServer< SocketAcceptorT, SocketContextFactoryT, Args >::listen ( const SocketAddress & localAddress,
int backlog,
const std::function< void(const SocketAddress &, core::socket::State)> & onStatus ) const
inline

Definition at line 266 of file SocketServer.h.

268 {
269 Super::config->Local::setBacklog(backlog);
270
272 }

◆ listen() [3/3]

template<typename SocketAcceptorT, typename SocketContextFactoryT, typename... Args>
const SocketServer & core::socket::stream::SocketServer< SocketAcceptorT, SocketContextFactoryT, Args >::listen ( const std::function< void(const SocketAddress &, core::socket::State)> & onStatus) const
inline

Definition at line 255 of file SocketServer.h.

255 {
256 return realListen(onStatus, 0, 1);
257 }
const SocketServer & realListen(const std::function< void(const SocketAddress &, core::socket::State)> &onStatus, unsigned int tries, double retryTimeoutScale) const

References realListen().

Referenced by main(), express::legacy::in6::Server(), express::legacy::in::Server(), express::legacy::rc::Server(), express::legacy::un::Server(), express::tls::in6::Server(), express::tls::in::Server(), express::tls::rc::Server(), and express::tls::un::Server().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ realListen()

template<typename SocketAcceptorT, typename SocketContextFactoryT, typename... Args>
const SocketServer & core::socket::stream::SocketServer< SocketAcceptorT, SocketContextFactoryT, Args >::realListen ( const std::function< void(const SocketAddress &, core::socket::State)> & onStatus,
unsigned int tries,
double retryTimeoutScale ) const
inlineprivate

Definition at line 188 of file SocketServer.h.

190 {
193 LOG(DEBUG) << config->getInstanceName() << ": Initiating listen";
194
196 auto autoConnectControl = sharedContext->autoConnectControl;
197 if (!autoConnectControl) {
199 sharedContext->autoConnectControl = autoConnectControl;
200 if (sharedContext->onAutoConnectControl) {
201 sharedContext->onAutoConnectControl(autoConnectControl);
202 }
203 }
204
205 new SocketAcceptor(
206 sharedContext->socketContextFactory,
207 sharedContext->onConnect,
208 sharedContext->onConnected,
209 sharedContext->onDisconnect,
210 sharedContext->onInitState,
213 const bool retryFlag = (state & core::socket::State::NO_RETRY) == 0;
214 state &= ~core::socket::State::NO_RETRY;
215 onStatus(socketAddress, state);
216
217 if (retryFlag && config->getRetry() // Shall we potentially retry? In case are the ...
218 && autoConnectControl->isRetryEnabled() &&
219 (config->getRetryTries() == 0 ||
220 tries < config->getRetryTries()) // ... limits not reached and has an ...
221 && (state == core::socket::State::ERROR ||
222 (state == core::socket::State::FATAL && config->getRetryOnFatal()))) { // error occurred?
223 double relativeRetryTimeout =
224 config->getRetryLimit() > 0
225 ? std::min<double>(config->getRetryTimeout() * retryTimeoutScale, config->getRetryLimit())
226 : config->getRetryTimeout() * retryTimeoutScale;
227 relativeRetryTimeout -= utils::Random::getInRange(-config->getRetryJitter(), config->getRetryJitter()) *
228 relativeRetryTimeout / 100.;
229
230 LOG(INFO) << config->getInstanceName() << ": Retry listen in " << relativeRetryTimeout << " seconds";
231
232 autoConnectControl->armRetryTimer(
233 relativeRetryTimeout,
234 [config, sharedContext, autoConnectControl, /*generation,*/ onStatus, tries, retryTimeoutScale]() {
235 if (!autoConnectControl->isRetryEnabled()) {
236 return;
237 }
238 if (config->getRetry()) {
239 SocketServer(config, sharedContext)
240 .realListen(onStatus, tries + 1, retryTimeoutScale * config->getRetryBase());
241 } else {
242 LOG(INFO) << config->getInstanceName() << ": Retry listen disabled during wait";
243 }
244 });
245 }
246 },
247 config);
248 }
249 });
250
251 return *this;
252 }
static void atNextTick(const std::function< void(void)> &callBack)
typename SocketAcceptor::SocketAddress SocketAddress

References core::EventReceiver::atNextTick(), core::socket::State::ERROR, core::socket::State::FATAL, utils::Random::getInRange(), core::INITIALIZED, core::socket::State::NO_RETRY, core::socket::State::operator&(), core::socket::State::operator&=(), core::socket::State::operator==(), realListen(), core::RUNNING, and core::SNodeC::state().

Referenced by listen(), and realListen().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setOnAutoConnectControl()

template<typename SocketAcceptorT, typename SocketContextFactoryT, typename... Args>
const SocketServer & core::socket::stream::SocketServer< SocketAcceptorT, SocketContextFactoryT, Args >::setOnAutoConnectControl ( const std::function< void(const std::shared_ptr< AutoConnectControl > &)> & onAutoConnectControl,
bool initialize = false ) const
inline

Definition at line 322 of file SocketServer.h.

323 {
324 sharedContext->onAutoConnectControl = initialize ? onAutoConnectControl
325 : [oldOnAutoConnectControl = sharedContext->onAutoConnectControl,
329 };
330
331 if (sharedContext->autoConnectControl && onAutoConnectControl) {
333 onAutoConnectControl(sharedContext->autoConnectControl);
334 });
335 }
336
337 return *this;
338 }

References core::EventReceiver::atNextTick(), and sharedContext.

Here is the call graph for this function:

◆ setOnConnect()

template<typename SocketAcceptorT, typename SocketContextFactoryT, typename... Args>
const SocketServer & core::socket::stream::SocketServer< SocketAcceptorT, SocketContextFactoryT, Args >::setOnConnect ( const std::function< void(SocketConnection *)> & onConnect,
bool initialize = false ) const
inline

Definition at line 278 of file SocketServer.h.

278 {
279 sharedContext->onConnect =
283 };
284
285 return *this;
286 }

References sharedContext.

◆ setOnConnected()

template<typename SocketAcceptorT, typename SocketContextFactoryT, typename... Args>
const SocketServer & core::socket::stream::SocketServer< SocketAcceptorT, SocketContextFactoryT, Args >::setOnConnected ( const std::function< void(SocketConnection *)> & onConnected,
bool initialize = false ) const
inline

Definition at line 292 of file SocketServer.h.

292 {
293 sharedContext->onConnected =
297 };
298
299 return *this;
300 }

References sharedContext.

◆ setOnDisconnect()

template<typename SocketAcceptorT, typename SocketContextFactoryT, typename... Args>
SocketServer & core::socket::stream::SocketServer< SocketAcceptorT, SocketContextFactoryT, Args >::setOnDisconnect ( const std::function< void(SocketConnection *)> & onDisconnect,
bool initialize = false )
inline

Definition at line 306 of file SocketServer.h.

306 {
307 sharedContext->onDisconnect =
312 };
313
314 return *this;
315 }

References sharedContext.

◆ setOnInitState()

template<typename SocketAcceptorT, typename SocketContextFactoryT, typename... Args>
const SocketServer & core::socket::stream::SocketServer< SocketAcceptorT, SocketContextFactoryT, Args >::setOnInitState ( const std::function< void(core::eventreceiver::AcceptEventReceiver *)> & onInitState,
bool initialize = false ) const
inline

Definition at line 344 of file SocketServer.h.

References sharedContext.

Referenced by main().

Here is the caller graph for this function:

Member Data Documentation

◆ sharedContext

template<typename SocketAcceptorT, typename SocketContextFactoryT, typename... Args>
std::shared_ptr<Context> core::socket::stream::SocketServer< SocketAcceptorT, SocketContextFactoryT, Args >::sharedContext
private

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