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::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)
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 >
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< SocketContextFactory > & socketContextFactory,
const std::function< void(SocketConnection *)> & onConnect,
const std::function< void(SocketConnection *)> & onConnected,
const std::function< void(SocketConnection *)> & onDisconnect )
inlineprivate

Definition at line 81 of file SocketServer.h.

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

96 : Super(name)
100 LOG(DEBUG) << socketConnection->getConnectionName() << ": OnConnect";
101
102 LOG(DEBUG) << " Local: " << socketConnection->getLocalAddress().toString();
103 LOG(DEBUG) << " Peer: " << socketConnection->getRemoteAddress().toString();
104
105 if (onConnect) {
107 }
108 },
110 LOG(DEBUG) << socketConnection->getConnectionName() << ": OnConnected";
111
112 LOG(DEBUG) << " Local: " << socketConnection->getLocalAddress().toString();
113 LOG(DEBUG) << " Peer: " << socketConnection->getRemoteAddress().toString();
114
115 if (onConnected) {
117 }
118 },
120 LOG(DEBUG) << socketConnection->getConnectionName() << ": OnDisconnect";
121
122 LOG(DEBUG) << " Local: " << socketConnection->getLocalAddress().toString();
123 LOG(DEBUG) << " Peer: " << socketConnection->getRemoteAddress().toString();
124
125 LOG(DEBUG) << " Online Since: " << socketConnection->getOnlineSince();
126 LOG(DEBUG) << " Online Duration: " << socketConnection->getOnlineDuration();
127
128 LOG(DEBUG) << " Total Queued: " << socketConnection->getTotalQueued();
129 LOG(DEBUG) << " Total Sent: " << socketConnection->getTotalSent();
130 LOG(DEBUG) << " Write Delta: " << socketConnection->getTotalQueued() - socketConnection->getTotalSent();
131 LOG(DEBUG) << " Total Read: " << socketConnection->getTotalRead();
132 LOG(DEBUG) << " Total Processed: " << socketConnection->getTotalProcessed();
133 LOG(DEBUG) << " Read Delta: " << socketConnection->getTotalRead() - socketConnection->getTotalProcessed();
134
135 if (onDisconnect) {
137 }
138 })) {
139 }
typename SocketAcceptor::SocketConnection SocketConnection

References sharedContext.

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

146 }
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() [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 148 of file SocketServer.h.

149 : SocketServer(name, {}, {}, {}, std::forward<Args>(args)...) {
150 }

◆ SocketServer() [5/5]

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

Definition at line 152 of file SocketServer.h.

154 }

Member Function Documentation

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

242 {
243 return sharedContext->onConnect;
244 }

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

256 {
257 return sharedContext->onConnected();
258 }

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

270 {
271 return sharedContext->onDisconnect;
272 }

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

285 {
286 return sharedContext->socketContextFactory;
287 }

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

228 {
229 Super::config->Local::setSocketAddress(localAddress);
230
231 return listen(onStatus);
232 }
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 234 of file SocketServer.h.

236 {
237 Super::config->Local::setBacklog(backlog);
238
240 }

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

223 {
224 return realListen(onStatus, 0, 1);
225 }
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 157 of file SocketServer.h.

159 {
160 LOG(DEBUG) << this->config->getInstanceName() << ": Initiating listen";
161
165 new SocketAcceptor(
166 sharedContext->socketContextFactory,
167 sharedContext->onConnect,
168 sharedContext->onConnected,
169 sharedContext->onDisconnect,
170 [config,
171 onConnect = sharedContext->onConnect,
172 onConnected = sharedContext->onConnected,
173 onDisconnect = sharedContext->onDisconnect,
174 socketContextFactory = sharedContext->socketContextFactory,
175 onStatus,
176 tries,
178 const bool retryFlag = (state & core::socket::State::NO_RETRY) == 0;
179 state &= ~core::socket::State::NO_RETRY;
180 onStatus(socketAddress, state);
181
182 if (retryFlag && config->getRetry() // Shall we potentially retry? In case are the ...
183 && (config->getRetryTries() == 0 ||
184 tries < config->getRetryTries()) // ... limits not reached and has an ...
185 && (state == core::socket::State::ERROR ||
186 (state == core::socket::State::FATAL && config->getRetryOnFatal()))) { // error occurred?
187 double relativeRetryTimeout =
188 config->getRetryLimit() > 0
189 ? std::min<double>(config->getRetryTimeout() * retryTimeoutScale, config->getRetryLimit())
190 : config->getRetryTimeout() * retryTimeoutScale;
191 relativeRetryTimeout -= utils::Random::getInRange(-config->getRetryJitter(), config->getRetryJitter()) *
192 relativeRetryTimeout / 100.;
193
194 LOG(INFO) << config->getInstanceName() << ": Retry listen in " << relativeRetryTimeout << " seconds";
195
196 core::timer::Timer::singleshotTimer(
197 [config,
198 onConnect,
199 onConnected,
200 onDisconnect,
201 onStatus,
202 tries,
203 retryTimeoutScale,
204 socketContextFactory]() {
205 if (config->getRetry()) {
206 SocketServer(config, socketContextFactory, onConnect, onConnected, onDisconnect)
207 .realListen(onStatus, tries + 1, retryTimeoutScale * config->getRetryBase());
208 } else {
209 LOG(INFO) << config->getInstanceName() << ": Retry listen disabled during wait";
210 }
211 },
212 relativeRetryTimeout);
213 }
214 },
215 config);
216 }
217 });
218
219 return *this;
220 }
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, core::timer::Timer::singleshotTimer(), 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 246 of file SocketServer.h.

246 {
247 sharedContext->onConnect =
251 };
252
253 return *this;
254 }

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

260 {
261 sharedContext->onConnected =
265 };
266
267 return *this;
268 }

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

274 {
275 sharedContext->onDisconnect =
280 };
281
282 return *this;
283 }

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: