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)
AutoConnectControlgetAutoConnectController () 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 104 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 113 of file SocketServer.h.

114 : Super(config)
116 }

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 119 of file SocketServer.h.

124 : Super(name)
128 LOG(DEBUG) << socketConnection->getConnectionName() << ": OnConnect";
129
130 LOG(DEBUG) << " Local: " << socketConnection->getLocalAddress().toString();
131 LOG(DEBUG) << " Peer: " << socketConnection->getRemoteAddress().toString();
132
133 if (onConnect) {
135 }
136 },
138 LOG(DEBUG) << socketConnection->getConnectionName() << ": OnConnected";
139
140 LOG(DEBUG) << " Local: " << socketConnection->getLocalAddress().toString();
141 LOG(DEBUG) << " Peer: " << socketConnection->getRemoteAddress().toString();
142
143 if (onConnected) {
145 }
146 },
148 LOG(DEBUG) << socketConnection->getConnectionName() << ": OnDisconnect";
149
150 LOG(DEBUG) << " Local: " << socketConnection->getLocalAddress().toString();
151 LOG(DEBUG) << " Peer: " << socketConnection->getRemoteAddress().toString();
152
153 LOG(DEBUG) << " Online Since: " << socketConnection->getOnlineSince();
154 LOG(DEBUG) << " Online Duration: " << socketConnection->getOnlineDuration();
155
156 LOG(DEBUG) << " Total Queued: " << socketConnection->getTotalQueued();
157 LOG(DEBUG) << " Total Sent: " << socketConnection->getTotalSent();
158 LOG(DEBUG) << " Write Delta: " << socketConnection->getTotalQueued() - socketConnection->getTotalSent();
159 LOG(DEBUG) << " Total Read: " << socketConnection->getTotalRead();
160 LOG(DEBUG) << " Total Processed: " << socketConnection->getTotalProcessed();
161 LOG(DEBUG) << " Read Delta: " << socketConnection->getTotalRead() - socketConnection->getTotalProcessed();
162
163 if (onDisconnect) {
165 }
166 })) {
167 }
#define LOG(level)
Definition Logger.h:148
typename SocketAcceptor::SocketConnection SocketConnection

References logger::DEBUG, logger::LogMessage::LogMessage(), and sharedContext.

Here is the call graph for this function:

◆ 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 169 of file SocketServer.h.

174 }
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 176 of file SocketServer.h.

177 : SocketServer(name, {}, {}, {}, std::forward<Args>(args)...) {
178 }

◆ SocketServer() [6/6]

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

Definition at line 180 of file SocketServer.h.

182 }

Member Function Documentation

◆ getAutoConnectController()

template<typename SocketAcceptorT, typename SocketContextFactoryT, typename... Args>
AutoConnectControl * core::socket::stream::SocketServer< SocketAcceptorT, SocketContextFactoryT, Args >::getAutoConnectController ( ) const
inline

Definition at line 311 of file SocketServer.h.

311 {
312 return sharedContext->autoConnectControl.get();
313 }

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 268 of file SocketServer.h.

268 {
269 return sharedContext->onConnect;
270 }

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 282 of file SocketServer.h.

282 {
283 return sharedContext->onConnected;
284 }

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 296 of file SocketServer.h.

296 {
297 return sharedContext->onDisconnect;
298 }

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 315 of file SocketServer.h.

315 {
316 return sharedContext->onInitState;
317 }

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 331 of file SocketServer.h.

331 {
332 return sharedContext->socketContextFactory;
333 }

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 253 of file SocketServer.h.

254 {
255 Super::config->Local::setSocketAddress(localAddress);
256
257 return listen(onStatus);
258 }
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 260 of file SocketServer.h.

262 {
263 Super::config->Local::setBacklog(backlog);
264
266 }

◆ 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 249 of file SocketServer.h.

249 {
250 return realListen(onStatus, 0, 1);
251 }
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 185 of file SocketServer.h.

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

References core::EventReceiver::atNextTick(), logger::DEBUG, core::socket::State::ERROR, core::socket::State::FATAL, logger::FATAL, utils::Random::getInRange(), logger::INFO, core::INITIALIZED, logger::LogMessage::LogMessage(), 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:

◆ 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 272 of file SocketServer.h.

272 {
273 sharedContext->onConnect =
277 };
278
279 return *this;
280 }

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 286 of file SocketServer.h.

286 {
287 sharedContext->onConnected =
291 };
292
293 return *this;
294 }

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 300 of file SocketServer.h.

300 {
301 sharedContext->onDisconnect =
306 };
307
308 return *this;
309 }

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 319 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: