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

Public Types

using SocketConnection = typename SocketAcceptor::SocketConnection
 
using SocketAddress = typename SocketAcceptor::SocketAddress
 
- Public Types inherited from core::socket::Socket< SocketAcceptorT::Config >
using Config = SocketAcceptorT::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)
 
void listen (const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const
 
void listen (const SocketAddress &localAddress, const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const
 
void listen (const SocketAddress &localAddress, int backlog, const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const
 
std::function< void(SocketConnection *)> setOnConnect (const std::function< void(SocketConnection *)> &onConnect)
 
std::function< void(SocketConnection *)> setOnConnected (const std::function< void(SocketConnection *)> &onConnected)
 
std::function< void(SocketConnection *)> setOnDisconnect (const std::function< void(SocketConnection *)> &onDisconnect)
 
std::shared_ptr< SocketContextFactorygetSocketContextFactory ()
 
- Public Member Functions inherited from core::socket::Socket< SocketAcceptorT::Config >
 Socket (const std::string &name)
 
 Socket (const Socket &)=default
 
 Socket (Socket &&) noexcept=delete
 
Socketoperator= (const Socket &)=default
 
Socketoperator= (Socket &&) noexcept=delete
 
virtual ~Socket ()
 
ConfiggetConfig () const
 

Private Types

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

Private Member Functions

void realListen (const std::function< void(const SocketAddress &, core::socket::State)> &onStatus, unsigned int tries, double retryTimeoutScale) const
 

Private Attributes

std::shared_ptr< SocketContextFactorysocketContextFactory
 
std::function< void(SocketConnection *)> onConnect
 
std::function< void(SocketConnection *)> onConnected
 
std::function< void(SocketConnection *)> onDisconnect
 

Additional Inherited Members

- 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

◆ 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/4]

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

◆ SocketServer() [2/4]

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

95 : SocketServer("", onConnect, onConnected, onDisconnect, std::forward<Args>(args)...) {
96 }
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() [3/4]

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

100 : SocketServer(
101 name,
102 [](SocketConnection* socketConnection) { // onConnect
103 VLOG(2) << socketConnection->getConnectionName() << ": OnConnect";
104
105 VLOG(2) << " Local: " << socketConnection->getLocalAddress().toString();
106 VLOG(2) << " Peer: " << socketConnection->getRemoteAddress().toString();
107 },
108 [](SocketConnection* socketConnection) { // onConnected
109 VLOG(2) << socketConnection->getConnectionName() << ": OnConnected";
110
111 VLOG(2) << " Local: " << socketConnection->getLocalAddress().toString();
112 VLOG(2) << " Peer: " << socketConnection->getRemoteAddress().toString();
113 },
114 [](SocketConnection* socketConnection) { // onDisconnect
115 VLOG(2) << socketConnection->getConnectionName() << ": OnDisconnect";
116
117 VLOG(2) << " Local: " << socketConnection->getLocalAddress().toString();
118 VLOG(2) << " Peer: " << socketConnection->getRemoteAddress().toString();
119
120 VLOG(2) << " Online Since: " << socketConnection->getOnlineSince();
121 VLOG(2) << " Online Duration: " << socketConnection->getOnlineDuration();
122
123 VLOG(2) << " Total Queued: " << socketConnection->getTotalQueued();
124 VLOG(2) << " Total Sent: " << socketConnection->getTotalSent();
125 VLOG(2) << " Write Delta: " << socketConnection->getTotalQueued() - socketConnection->getTotalSent();
126 VLOG(2) << " Total Read: " << socketConnection->getTotalRead();
127 VLOG(2) << " Total Processed: " << socketConnection->getTotalProcessed();
128 VLOG(2) << " Read Delta: " << socketConnection->getTotalRead() - socketConnection->getTotalProcessed();
129 },
130 std::forward<Args>(args)...) {
131 }
typename SocketAcceptor::SocketConnection SocketConnection

◆ SocketServer() [4/4]

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

Definition at line 133 of file SocketServer.h.

134 : SocketServer("", std::forward<Args>(args)...) {
135 }

Member Function Documentation

◆ getSocketContextFactory()

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

◆ listen() [1/3]

template<typename SocketAcceptorT , typename SocketContextFactoryT , typename... Args>
void 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 187 of file SocketServer.h.

188 {
189 Super::config->Local::setSocketAddress(localAddress);
190
191 listen(onStatus);
192 }
std::shared_ptr< Config > config
Definition Socket.h:72
void listen(const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const

◆ listen() [2/3]

template<typename SocketAcceptorT , typename SocketContextFactoryT , typename... Args>
void 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 194 of file SocketServer.h.

196 {
197 Super::config->Local::setBacklog(backlog);
198
199 listen(localAddress, onStatus);
200 }

◆ listen() [3/3]

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

Definition at line 183 of file SocketServer.h.

183 {
184 realListen(onStatus, 0, 1);
185 }
void realListen(const std::function< void(const SocketAddress &, core::socket::State)> &onStatus, unsigned int tries, double retryTimeoutScale) const

References core::socket::stream::SocketServer< SocketAcceptorT, SocketContextFactoryT, Args >::realListen().

Referenced by main().

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

◆ realListen()

template<typename SocketAcceptorT , typename SocketContextFactoryT , typename... Args>
void 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 138 of file SocketServer.h.

140 {
142 new SocketAcceptor(
144 onConnect,
147 [server = *this, onStatus, tries, retryTimeoutScale](const SocketAddress& socketAddress,
148 core::socket::State state) mutable {
149 const bool retryFlag = (state & core::socket::State::NO_RETRY) == 0;
151 onStatus(socketAddress, state);
152
153 if (retryFlag && server.getConfig().getRetry() // Shall we potentially retry? In case are the ...
154 && (server.getConfig().getRetryTries() == 0 ||
155 tries < server.getConfig().getRetryTries()) // ... limits not reached and has an ...
156 && (state == core::socket::State::ERROR ||
157 (state == core::socket::State::FATAL && server.getConfig().getRetryOnFatal()))) { // error occurred?
158 double relativeRetryTimeout = server.getConfig().getRetryLimit() > 0
159 ? std::min<double>(server.getConfig().getRetryTimeout() * retryTimeoutScale,
160 server.getConfig().getRetryLimit())
161 : server.getConfig().getRetryTimeout() * retryTimeoutScale;
162 relativeRetryTimeout -=
163 utils::Random::getInRange(-server.getConfig().getRetryJitter(), server.getConfig().getRetryJitter()) *
164 relativeRetryTimeout / 100.;
165
166 LOG(INFO) << server.getConfig().getInstanceName() << ": OnStatus";
167 LOG(INFO) << " retrying in " << relativeRetryTimeout << " seconds";
168
170 [server, onStatus, tries, retryTimeoutScale]() mutable {
171 server.getConfig().Local::renew();
172
173 server.realListen(onStatus, tries + 1, retryTimeoutScale * server.getConfig().getRetryBase());
174 },
175 relativeRetryTimeout);
176 }
177 },
179 }
180 }
static State state()
Definition SNodeC.cpp:76
static constexpr int ERROR
Definition State.h:57
static constexpr int FATAL
Definition State.h:58
static constexpr int NO_RETRY
Definition State.h:59
typename SocketAcceptor::SocketAddress SocketAddress
static Timer singleshotTimer(const std::function< void()> &dispatcher, const utils::Timeval &timeout)
Definition Timer.cpp:57
static double getInRange(double ll, double ul)
Definition Random.cpp:52
Definition Timer.h:59

References core::socket::State::ERROR, core::socket::State::FATAL, utils::Random::getInRange(), core::INITIALIZED, core::socket::State::NO_RETRY, core::socket::stream::SocketServer< SocketAcceptorT, SocketContextFactoryT, Args >::onConnect, core::socket::stream::SocketServer< SocketAcceptorT, SocketContextFactoryT, Args >::onConnected, core::socket::stream::SocketServer< SocketAcceptorT, SocketContextFactoryT, Args >::onDisconnect, core::socket::State::operator&(), core::socket::State::operator&=(), core::socket::State::operator==(), core::RUNNING, core::timer::Timer::singleshotTimer(), core::socket::stream::SocketServer< SocketAcceptorT, SocketContextFactoryT, Args >::socketContextFactory, and core::SNodeC::state().

Referenced by core::socket::stream::SocketServer< SocketAcceptorT, SocketContextFactoryT, Args >::listen().

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

◆ setOnConnect()

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

Definition at line 202 of file SocketServer.h.

202 {
203 std::function<void(SocketConnection*)> oldOnConnect = this->onConnect;
204
205 this->onConnect = onConnect;
206
207 return oldOnConnect;
208 }

References core::socket::stream::SocketServer< SocketAcceptorT, SocketContextFactoryT, Args >::onConnect.

◆ setOnConnected()

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

Definition at line 210 of file SocketServer.h.

210 {
211 std::function<void(SocketConnection*)> oldOnConnected = this->onConnected;
212
213 this->onConnected = onConnected;
214
215 return oldOnConnected;
216 }

References core::socket::stream::SocketServer< SocketAcceptorT, SocketContextFactoryT, Args >::onConnected.

◆ setOnDisconnect()

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

Definition at line 218 of file SocketServer.h.

218 {
219 std::function<void(SocketConnection*)> oldOnDisconnect = this->onDisconnect;
220
222
223 return oldOnDisconnect;
224 }

References core::socket::stream::SocketServer< SocketAcceptorT, SocketContextFactoryT, Args >::onDisconnect.

Member Data Documentation

◆ onConnect

◆ onConnected

◆ onDisconnect

◆ socketContextFactory


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