SNode.C
Loading...
Searching...
No Matches
core::socket::stream::SocketAcceptor< PhysicalSocketServerT, ConfigT, SocketConnectionT > Class Template Referenceabstract

#include <SocketAcceptor.h>

Inheritance diagram for core::socket::stream::SocketAcceptor< PhysicalSocketServerT, ConfigT, SocketConnectionT >:
Collaboration diagram for core::socket::stream::SocketAcceptor< PhysicalSocketServerT, ConfigT, SocketConnectionT >:

Public Member Functions

 SocketAcceptor (const std::function< void(SocketConnection *)> &onConnect, const std::function< void(SocketConnection *)> &onConnected, const std::function< void(SocketConnection *)> &onDisconnect, const std::function< void(const SocketAddress &, core::socket::State)> &onStatus, const std::shared_ptr< Config > &config)
 SocketAcceptor (const SocketAcceptor &socketAcceptor)
 ~SocketAcceptor () override

Protected Types

using Config = ConfigT
using SocketAddress = typename PhysicalServerSocket::SocketAddress
using SocketConnection = SocketConnectionT<PhysicalServerSocket, Config>

Protected Member Functions

virtual void useNextSocketAddress ()=0
virtual void init ()
void destruct () final
Protected Member Functions inherited from core::eventreceiver::AcceptEventReceiver
 AcceptEventReceiver (const std::string &name, const utils::Timeval &timeout)
virtual void acceptTimeout ()
Protected Member Functions inherited from core::DescriptorEventReceiver
bool enable (int fd)
void disable ()
void suspend ()
void resume ()
 DescriptorEventReceiver (const std::string &name, DescriptorEventPublisher &descriptorEventPublisher, const utils::Timeval &timeout=TIMEOUT::DISABLE)
int getRegisteredFd () const
bool isEnabled () const
bool isSuspended () const
void setTimeout (const utils::Timeval &timeout)
utils::Timeval getTimeout (const utils::Timeval &currentTime) const
void checkTimeout (const utils::Timeval &currentTime)
Protected Member Functions inherited from core::Observer
void observed ()
void unObserved ()
 Observer ()=default
 Observer (Observer &)=delete
 Observer (Observer &&)=delete
virtual ~Observer ()
Protected Member Functions inherited from core::EventReceiver
virtual ~EventReceiver ()=default
 EventReceiver (const std::string &name)
 EventReceiver (EventReceiver &)=delete
 EventReceiver (EventReceiver &&)=delete
EventReceiveroperator= (EventReceiver &)=delete
EventReceiveroperator= (EventReceiver &&)=delete
void span ()
void relax ()
const std::string & getName () const

Protected Attributes

std::function< void(SocketConnection *)> onConnect
std::function< void(SocketConnection *)> onConnected
std::function< void(SocketConnection *)> onDisconnect
std::function< void(const SocketAddress &, core::socket::State)> onStatus = nullptr
std::shared_ptr< Configconfig

Private Types

using PhysicalServerSocket = PhysicalSocketServerT

Private Member Functions

void acceptEvent () final
void unobservedEvent () final

Private Attributes

PhysicalServerSocket physicalServerSocket
SocketAddress bindAddress

Detailed Description

template<typename PhysicalSocketServerT, typename ConfigT, template< typename PhysicalSocketServer, typename Config > typename SocketConnectionT>
class core::socket::stream::SocketAcceptor< PhysicalSocketServerT, ConfigT, SocketConnectionT >

Sequence diagram showing how a connect to a peer is performed.

Definition at line 69 of file SocketAcceptor.h.

Member Typedef Documentation

◆ Config

template<typename PhysicalSocketServerT, typename ConfigT, template< typename PhysicalSocketServer, typename Config > typename SocketConnectionT>
using core::socket::stream::SocketAcceptor< PhysicalSocketServerT, ConfigT, SocketConnectionT >::Config = ConfigT
protected

Definition at line 74 of file SocketAcceptor.h.

◆ PhysicalServerSocket

template<typename PhysicalSocketServerT, typename ConfigT, template< typename PhysicalSocketServer, typename Config > typename SocketConnectionT>
using core::socket::stream::SocketAcceptor< PhysicalSocketServerT, ConfigT, SocketConnectionT >::PhysicalServerSocket = PhysicalSocketServerT
private

Definition at line 71 of file SocketAcceptor.h.

◆ SocketAddress

template<typename PhysicalSocketServerT, typename ConfigT, template< typename PhysicalSocketServer, typename Config > typename SocketConnectionT>
using core::socket::stream::SocketAcceptor< PhysicalSocketServerT, ConfigT, SocketConnectionT >::SocketAddress = typename PhysicalServerSocket::SocketAddress
protected

Definition at line 75 of file SocketAcceptor.h.

◆ SocketConnection

template<typename PhysicalSocketServerT, typename ConfigT, template< typename PhysicalSocketServer, typename Config > typename SocketConnectionT>
using core::socket::stream::SocketAcceptor< PhysicalSocketServerT, ConfigT, SocketConnectionT >::SocketConnection = SocketConnectionT<PhysicalServerSocket, Config>
protected

Definition at line 76 of file SocketAcceptor.h.

Constructor & Destructor Documentation

◆ SocketAcceptor() [1/2]

template<typename PhysicalSocketServer, typename Config, template< typename ConfigT, typename PhysicalSocketServerT > typename SocketConnection>
core::socket::stream::SocketAcceptor< PhysicalSocketServer, Config, SocketConnection >::SocketAcceptor ( const std::function< void(SocketConnection *)> & onConnect,
const std::function< void(SocketConnection *)> & onConnected,
const std::function< void(SocketConnection *)> & onDisconnect,
const std::function< void(const SocketAddress &, core::socket::State)> & onStatus,
const std::shared_ptr< Config > & config )

Definition at line 61 of file SocketAcceptor.hpp.

67 : core::eventreceiver::AcceptEventReceiver(config->getInstanceName() + " SocketAcceptor", 0)
72 , config(config) {
73 atNextTick([this]() {
75 init();
76 } else {
77 destruct();
78 }
79 });
80 }
static void atNextTick(const std::function< void(void)> &callBack)
std::function< void(SocketConnection *)> onConnected
std::function< void(SocketConnection *)> onConnect
std::function< void(SocketConnection *)> onDisconnect
std::shared_ptr< Config > config
std::function< void(const SocketAddress &, core::socket::State)> onStatus

References core::EventReceiver::atNextTick(), config, destruct(), core::eventLoopState(), init(), onConnect, onConnected, onDisconnect, onStatus, and core::RUNNING.

Here is the call graph for this function:

◆ SocketAcceptor() [2/2]

template<typename PhysicalSocketServer, typename Config, template< typename ConfigT, typename PhysicalSocketServerT > typename SocketConnection>
core::socket::stream::SocketAcceptor< PhysicalSocketServer, Config, SocketConnection >::SocketAcceptor ( const SocketAcceptor< PhysicalSocketServerT, ConfigT, SocketConnectionT > & socketAcceptor)

Definition at line 85 of file SocketAcceptor.hpp.

References core::EventReceiver::atNextTick(), config, destruct(), core::eventLoopState(), init(), onConnect, onConnected, onDisconnect, onStatus, and core::RUNNING.

Here is the call graph for this function:

◆ ~SocketAcceptor()

template<typename PhysicalSocketServer, typename Config, template< typename ConfigT, typename PhysicalSocketServerT > typename SocketConnection>
core::socket::stream::SocketAcceptor< PhysicalSocketServer, Config, SocketConnection >::~SocketAcceptor ( )
override

Definition at line 104 of file SocketAcceptor.hpp.

104 {
105 }

Member Function Documentation

◆ acceptEvent()

template<typename PhysicalSocketServer, typename Config, template< typename ConfigT, typename PhysicalSocketServerT > typename SocketConnection>
void core::socket::stream::SocketAcceptor< PhysicalSocketServer, Config, SocketConnection >::acceptEvent ( )
finalprivatevirtual

Implements core::eventreceiver::AcceptEventReceiver.

Definition at line 213 of file SocketAcceptor.hpp.

213 {
214 int acceptsPerTick = config->getAcceptsPerTick();
215
216 do {
218
219 if (connectedPhysicalSocket.isValid()) {
221
222 LOG(DEBUG) << config->getInstanceName() << " accept " << bindAddress.toString() << ": success";
223 LOG(DEBUG) << " " << socketConnection->getRemoteAddress().toString() << " -> "
224 << socketConnection->getLocalAddress().toString();
225
228 } else if (errno != EINTR && errno != EAGAIN && errno != EWOULDBLOCK) {
229 PLOG(WARNING) << config->getInstanceName() << " accept " << bindAddress.toString();
230 }
231 } while (--acceptsPerTick > 0);
232 }
SocketConnectionT< PhysicalServerSocket, Config > SocketConnection
PhysicalSocketServerT PhysicalServerSocket
PhysicalServerSocket physicalServerSocket

References bindAddress, config, onConnect, onConnected, onDisconnect, and physicalServerSocket.

◆ destruct()

template<typename PhysicalSocketServer, typename Config, template< typename ConfigT, typename PhysicalSocketServerT > typename SocketConnection>
void core::socket::stream::SocketAcceptor< PhysicalSocketServer, Config, SocketConnection >::destruct ( )
finalprotectedvirtual

Reimplemented from core::EventReceiver.

Definition at line 244 of file SocketAcceptor.hpp.

244 {
245 delete this;
246 }

Referenced by init(), SocketAcceptor(), SocketAcceptor(), and unobservedEvent().

Here is the caller graph for this function:

◆ init()

template<typename PhysicalSocketServer, typename Config, template< typename ConfigT, typename PhysicalSocketServerT > typename SocketConnection>
void core::socket::stream::SocketAcceptor< PhysicalSocketServer, Config, SocketConnection >::init ( )
protectedvirtual

Reimplemented in core::socket::stream::tls::SocketAcceptor< PhysicalSocketServerT, ConfigT >.

Definition at line 110 of file SocketAcceptor.hpp.

110 {
111 if (!config->getDisabled()) {
112 try {
114
115 LOG(TRACE) << config->getInstanceName() << ": Starting";
116
117 bindAddress = config->Local::getSocketAddress();
118
119 if (physicalServerSocket.open(config->getSocketOptions(), PhysicalServerSocket::Flags::NONBLOCK) < 0) {
120 PLOG(ERROR) << config->getInstanceName() << " open " << bindAddress.toString();
121
122 switch (errno) {
123 case EMFILE:
124 case ENFILE:
125 case ENOBUFS:
126 case ENOMEM:
128 break;
129 default:
131 break;
132 }
133 } else {
134 LOG(DEBUG) << config->getInstanceName() << " open " << bindAddress.toString() << ": success";
135
136 if (physicalServerSocket.bind(bindAddress) < 0) {
137 PLOG(ERROR) << config->getInstanceName() << " bind " << bindAddress.toString();
138
139 switch (errno) {
140 case EADDRINUSE:
141
143 break;
144 default:
145
147 break;
148 }
149 } else {
150 LOG(DEBUG) << config->getInstanceName() << " bind " << bindAddress.toString() << ": success";
151
152 if (physicalServerSocket.listen(config->getBacklog()) < 0) {
153 PLOG(ERROR) << config->getInstanceName() << " listen " << bindAddress.toString();
154
155 switch (errno) {
156 case EADDRINUSE:
158 break;
159 default:
161 break;
162 }
163 } else {
164 LOG(DEBUG) << config->getInstanceName() << " listen " << bindAddress.toString() << ": success";
165
166 if (enable(physicalServerSocket.getFd())) {
167 LOG(DEBUG) << config->getInstanceName() << " enable " << bindAddress.toString() << ": success";
168 } else {
169 LOG(ERROR) << config->getInstanceName() << " enable " << bindAddress.toString()
170 << ": failed. No valid descriptor created";
171
172 state = core::socket::STATE(core::socket::STATE_FATAL, ECANCELED, "SocketAcceptor not enabled");
173 }
174 }
175 }
176 }
177
179 if (bindAddress.useNext()) {
181
182 LOG(INFO) << config->getInstanceName()
183 << ": Using next SocketAddress: " << config->Local::getSocketAddress().toString();
184
186 } else {
188 }
189 } catch (const typename SocketAddress::BadSocketAddress& badSocketAddress) {
192
193 LOG(ERROR) << state.what();
194
195 onStatus({}, state);
196 }
197 } else {
198 LOG(DEBUG) << config->getInstanceName() << ": disabled";
199
201 }
202
203 if (isEnabled()) {
205 } else {
206 destruct();
207 }
208 }
void setTimeout(const utils::Timeval &timeout)
typename PhysicalServerSocket::SocketAddress SocketAddress

References bindAddress, config, destruct(), core::DescriptorEventReceiver::enable(), core::DescriptorEventReceiver::isEnabled(), core::socket::State::NO_RETRY, onStatus, core::socket::State::operator|(), physicalServerSocket, core::DescriptorEventReceiver::setTimeout(), useNextSocketAddress(), and core::socket::State::what().

Referenced by SocketAcceptor(), and SocketAcceptor().

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

◆ unobservedEvent()

template<typename PhysicalSocketServer, typename Config, template< typename ConfigT, typename PhysicalSocketServerT > typename SocketConnection>
void core::socket::stream::SocketAcceptor< PhysicalSocketServer, Config, SocketConnection >::unobservedEvent ( )
finalprivatevirtual

Implements core::Observer.

Definition at line 237 of file SocketAcceptor.hpp.

237 {
238 destruct();
239 }

References destruct().

Here is the call graph for this function:

◆ useNextSocketAddress()

template<typename PhysicalSocketServerT, typename ConfigT, template< typename PhysicalSocketServer, typename Config > typename SocketConnectionT>
virtual void core::socket::stream::SocketAcceptor< PhysicalSocketServerT, ConfigT, SocketConnectionT >::useNextSocketAddress ( )
protectedpure virtual

Implemented in core::socket::stream::legacy::SocketAcceptor< PhysicalServerSocketT, ConfigT >, and core::socket::stream::tls::SocketAcceptor< PhysicalSocketServerT, ConfigT >.

Referenced by init().

Here is the caller graph for this function:

Member Data Documentation

◆ bindAddress

template<typename PhysicalSocketServerT, typename ConfigT, template< typename PhysicalSocketServer, typename Config > typename SocketConnectionT>
SocketAddress core::socket::stream::SocketAcceptor< PhysicalSocketServerT, ConfigT, SocketConnectionT >::bindAddress
private

Definition at line 104 of file SocketAcceptor.h.

Referenced by acceptEvent(), and init().

◆ config

template<typename PhysicalSocketServerT, typename ConfigT, template< typename PhysicalSocketServer, typename Config > typename SocketConnectionT>
std::shared_ptr<Config> core::socket::stream::SocketAcceptor< PhysicalSocketServerT, ConfigT, SocketConnectionT >::config
protected

Definition at line 113 of file SocketAcceptor.h.

Referenced by acceptEvent(), init(), SocketAcceptor(), and SocketAcceptor().

◆ onConnect

template<typename PhysicalSocketServerT, typename ConfigT, template< typename PhysicalSocketServer, typename Config > typename SocketConnectionT>
std::function<void(SocketConnection*)> core::socket::stream::SocketAcceptor< PhysicalSocketServerT, ConfigT, SocketConnectionT >::onConnect
protected

Definition at line 107 of file SocketAcceptor.h.

Referenced by acceptEvent(), SocketAcceptor(), and SocketAcceptor().

◆ onConnected

template<typename PhysicalSocketServerT, typename ConfigT, template< typename PhysicalSocketServer, typename Config > typename SocketConnectionT>
std::function<void(SocketConnection*)> core::socket::stream::SocketAcceptor< PhysicalSocketServerT, ConfigT, SocketConnectionT >::onConnected
protected

Definition at line 108 of file SocketAcceptor.h.

Referenced by acceptEvent(), SocketAcceptor(), and SocketAcceptor().

◆ onDisconnect

template<typename PhysicalSocketServerT, typename ConfigT, template< typename PhysicalSocketServer, typename Config > typename SocketConnectionT>
std::function<void(SocketConnection*)> core::socket::stream::SocketAcceptor< PhysicalSocketServerT, ConfigT, SocketConnectionT >::onDisconnect
protected

Definition at line 109 of file SocketAcceptor.h.

Referenced by acceptEvent(), SocketAcceptor(), and SocketAcceptor().

◆ onStatus

template<typename PhysicalSocketServerT, typename ConfigT, template< typename PhysicalSocketServer, typename Config > typename SocketConnectionT>
std::function<void(const SocketAddress&, core::socket::State)> core::socket::stream::SocketAcceptor< PhysicalSocketServerT, ConfigT, SocketConnectionT >::onStatus = nullptr
protected

Definition at line 111 of file SocketAcceptor.h.

Referenced by init(), SocketAcceptor(), and SocketAcceptor().

◆ physicalServerSocket

template<typename PhysicalSocketServerT, typename ConfigT, template< typename PhysicalSocketServer, typename Config > typename SocketConnectionT>
PhysicalServerSocket core::socket::stream::SocketAcceptor< PhysicalSocketServerT, ConfigT, SocketConnectionT >::physicalServerSocket
private

Definition at line 103 of file SocketAcceptor.h.

Referenced by acceptEvent(), and init().


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