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)
ServerFlowControllergetFlowController () 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< 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/5]

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

103 : Super(config)
105 }
const std::shared_ptr< Config > config
Definition Socket.h:75
core::socket::Socket< typename SocketAcceptor::Config > Super
std::shared_ptr< Context > sharedContext

References sharedContext.

◆ SocketServer() [2/5]

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

113 : Super(name)
115 this->config.get(),
118 LOG(DEBUG) << socketConnection->getConnectionName() << ": OnConnect";
119
120 LOG(DEBUG) << " Local: " << socketConnection->getLocalAddress().toString();
121 LOG(DEBUG) << " Peer: " << socketConnection->getRemoteAddress().toString();
122
123 if (onConnect) {
124 onConnect(socketConnection);
125 }
126 },
128 LOG(DEBUG) << socketConnection->getConnectionName() << ": OnConnected";
129
130 LOG(DEBUG) << " Local: " << socketConnection->getLocalAddress().toString();
131 LOG(DEBUG) << " Peer: " << socketConnection->getRemoteAddress().toString();
132
133 if (onConnected) {
135 }
136 },
138 LOG(DEBUG) << socketConnection->getConnectionName() << ": OnDisconnect";
139
140 LOG(DEBUG) << " Local: " << socketConnection->getLocalAddress().toString();
141 LOG(DEBUG) << " Peer: " << socketConnection->getRemoteAddress().toString();
142
143 LOG(DEBUG) << " Online Since: " << socketConnection->getOnlineSince();
144 LOG(DEBUG) << " Online Duration: " << socketConnection->getOnlineDuration();
145
146 LOG(DEBUG) << " Total Queued: " << socketConnection->getTotalQueued();
147 LOG(DEBUG) << " Total Sent: " << socketConnection->getTotalSent();
148 LOG(DEBUG) << " Write Delta: " << socketConnection->getTotalQueued() - socketConnection->getTotalSent();
149 LOG(DEBUG) << " Total Read: " << socketConnection->getTotalRead();
150 LOG(DEBUG) << " Total Processed: " << socketConnection->getTotalProcessed();
151 LOG(DEBUG) << " Read Delta: " << socketConnection->getTotalRead() - socketConnection->getTotalProcessed();
152
153 if (onDisconnect) {
155 }
156 })) {
157 }
#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() [3/5]

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

164 }
SocketServer(const std::shared_ptr< Config > &config, const std::shared_ptr< Context > &sharedContext)

◆ SocketServer() [4/5]

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

167 : SocketServer(name, {}, {}, {}, std::forward<Args>(args)...) {
168 }

◆ SocketServer() [5/5]

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

Definition at line 170 of file SocketServer.h.

172 }

Member Function Documentation

◆ getFlowController()

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

Definition at line 304 of file SocketServer.h.

304 {
305 return &sharedContext->flowController;
306 }

References sharedContext.

Referenced by main().

Here is the caller graph for this function:

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

261 {
262 return sharedContext->onConnect;
263 }

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

275 {
276 return sharedContext->onConnected;
277 }

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

289 {
290 return sharedContext->onDisconnect;
291 }

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

308 {
309 return sharedContext->socketContextFactory;
310 }

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

247 {
248 Super::config->Local::setSocketAddress(localAddress);
249
250 return listen(onStatus);
251 }
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 253 of file SocketServer.h.

255 {
256 Super::config->Local::setBacklog(backlog);
257
259 }

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

242 {
243 return realListen(onStatus, 0, 1);
244 }
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 175 of file SocketServer.h.

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

References 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, sharedContext, 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 265 of file SocketServer.h.

265 {
266 sharedContext->onConnect =
270 };
271
272 return *this;
273 }

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

279 {
280 sharedContext->onConnected =
284 };
285
286 return *this;
287 }

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

293 {
294 sharedContext->onDisconnect =
299 };
300
301 return *this;
302 }

References sharedContext.

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: