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(core::eventreceiver::AcceptEventReceiver *)> &onInitState, const std::function< void(const SocketAddress &, core::socket::State)> &onStatus, const std::shared_ptr< Config > &config)
 SocketAcceptor (const SocketAcceptor &socketAcceptor)
 ~SocketAcceptor () override
virtual void init ()

Protected Types

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

Protected Member Functions

virtual void useNextSocketAddress ()=0
void destruct () final
Protected Member Functions inherited from core::eventreceiver::AcceptEventReceiver
 AcceptEventReceiver (const std::string &name, const utils::Timeval &timeout)
virtual void acceptTimeout ()
void stopListen ()
Protected Member Functions inherited from core::DescriptorEventReceiver
 DescriptorEventReceiver (const std::string &name, DescriptorEventPublisher &descriptorEventPublisher, const utils::Timeval &timeout=TIMEOUT::DISABLE)
bool enable (int fd)
void disable ()
void suspend ()
void resume ()
void setTimeout (const utils::Timeval &timeout)
utils::Timeval getTimeout (const utils::Timeval &currentTime) const
void checkTimeout (const utils::Timeval &currentTime)
int getRegisteredFd () const
bool isEnabled () const
bool isSuspended () const
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(core::eventreceiver::AcceptEventReceiver *)> onInitState
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(core::eventreceiver::AcceptEventReceiver *)> & onInitState,
const std::function< void(const SocketAddress &, core::socket::State)> & onStatus,
const std::shared_ptr< Config > & config )

Definition at line 61 of file SocketAcceptor.hpp.

68 : core::eventreceiver::AcceptEventReceiver(config->getInstanceName() + " SocketAcceptor", 0)
74 , config(config) {
75 }
std::function< void(SocketConnection *)> onConnected
std::function< void(SocketConnection *)> onConnect
std::function< void(SocketConnection *)> onDisconnect
std::shared_ptr< Config > config
std::function< void(core::eventreceiver::AcceptEventReceiver *)> onInitState
std::function< void(const SocketAddress &, core::socket::State)> onStatus

References config, onConnect, onConnected, onDisconnect, onInitState, and onStatus.

◆ 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)

◆ ~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 93 of file SocketAcceptor.hpp.

93 {
94 }

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 203 of file SocketAcceptor.hpp.

203 {
204 if (isEnabled()) {
205 int acceptsPerTick = config->getAcceptsPerTick();
206
207 do {
210
211 if (connectedPhysicalSocket.isValid()) {
213
214 LOG(DEBUG) << config->getInstanceName() << " accept " << bindAddress.toString() << ": success";
215 LOG(DEBUG) << " " << socketConnection->getRemoteAddress().toString() << " -> "
216 << socketConnection->getLocalAddress().toString();
217
220 } else if (errno != EINTR && errno != EAGAIN && errno != EWOULDBLOCK) {
221 PLOG(WARNING) << config->getInstanceName() << " accept " << bindAddress.toString();
222 }
223 } while (--acceptsPerTick > 0);
224 }
225 }
#define LOG(level)
Definition Logger.h:148
#define PLOG(level)
Definition Logger.h:152
SocketConnectionT< PhysicalServerSocket, Config > SocketConnection
PhysicalSocketServerT PhysicalServerSocket
PhysicalServerSocket physicalServerSocket

References bindAddress, config, logger::DEBUG, core::DescriptorEventReceiver::isEnabled(), logger::LogMessage::LogMessage(), onConnect, onConnected, onDisconnect, physicalServerSocket, and logger::WARNING.

Here is the call graph for this function:

◆ 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 237 of file SocketAcceptor.hpp.

237 {
238 if (!config->getDisabled()) {
239 onInitState(this);
240 }
241
242 delete this;
243 }

References config, and onInitState.

Referenced by init(), 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 ( )
virtual

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

Definition at line 99 of file SocketAcceptor.hpp.

99 {
100 if (!config->getDisabled()) {
101 try {
103
104 LOG(DEBUG) << config->getInstanceName() << " Listen: starting";
105
106 bindAddress = config->Local::getSocketAddress();
107
108 if (physicalServerSocket.open(config->getSocketOptions(), PhysicalServerSocket::Flags::NONBLOCK) < 0) {
109 PLOG(ERROR) << config->getInstanceName() << " open " << bindAddress.toString();
110
111 switch (errno) {
112 case EMFILE:
113 case ENFILE:
114 case ENOBUFS:
115 case ENOMEM:
117 break;
118 default:
120 break;
121 }
122 } else {
123 LOG(DEBUG) << config->getInstanceName() << " open " << bindAddress.toString() << ": success";
124
125 if (physicalServerSocket.bind(bindAddress) < 0) {
126 PLOG(ERROR) << config->getInstanceName() << " bind " << bindAddress.toString();
127
128 switch (errno) {
129 case EADDRINUSE:
130
132 break;
133 default:
134
136 break;
137 }
138 } else {
139 LOG(DEBUG) << config->getInstanceName() << " bind " << bindAddress.toString() << ": success";
140
141 if (physicalServerSocket.listen(config->getBacklog()) < 0) {
142 PLOG(ERROR) << config->getInstanceName() << " listen " << bindAddress.toString();
143
144 switch (errno) {
145 case EADDRINUSE:
147 break;
148 default:
150 break;
151 }
152 } else {
153 LOG(DEBUG) << config->getInstanceName() << " listen " << bindAddress.toString() << ": success";
154
155 if (enable(physicalServerSocket.getFd())) {
156 LOG(DEBUG) << config->getInstanceName() << " enable " << bindAddress.toString() << ": success";
157 } else {
158 LOG(ERROR) << config->getInstanceName() << " enable " << bindAddress.toString()
159 << ": failed. No valid descriptor created";
160
161 state = core::socket::STATE(core::socket::STATE_FATAL, ECANCELED, "SocketAcceptor not enabled");
162 }
163 }
164 }
165 }
166
168 if (bindAddress.useNext()) {
170
171 LOG(INFO) << config->getInstanceName()
172 << ": Using next SocketAddress: " << config->Local::getSocketAddress().toString();
173
175 } else {
177 }
178 } catch (const typename SocketAddress::BadSocketAddress& badSocketAddress) {
181
182 LOG(ERROR) << state.what();
183
184 onStatus({}, state);
185 }
186 } else {
187 LOG(DEBUG) << config->getInstanceName() << ": disabled";
188
190 }
191
192 if (isEnabled()) {
193 onInitState(this);
195 } else {
196 destruct();
197 }
198 }
void setTimeout(const utils::Timeval &timeout)
typename PhysicalServerSocket::SocketAddress SocketAddress

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

Here is the call 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 230 of file SocketAcceptor.hpp.

230 {
231 destruct();
232 }

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 105 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 115 of file SocketAcceptor.h.

Referenced by acceptEvent(), destruct(), 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 108 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 109 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 110 of file SocketAcceptor.h.

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

◆ onInitState

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

Definition at line 111 of file SocketAcceptor.h.

Referenced by destruct(), init(), 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 113 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 104 of file SocketAcceptor.h.

Referenced by acceptEvent(), and init().


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