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
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 ()
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 }
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 config, onConnect, onConnected, onDisconnect, 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 90 of file SocketAcceptor.hpp.

90 {
91 }

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

199 {
200 int acceptsPerTick = config->getAcceptsPerTick();
201
202 do {
204
205 if (connectedPhysicalSocket.isValid()) {
207
208 LOG(DEBUG) << config->getInstanceName() << " accept " << bindAddress.toString() << ": success";
209 LOG(DEBUG) << " " << socketConnection->getRemoteAddress().toString() << " -> "
210 << socketConnection->getLocalAddress().toString();
211
214 } else if (errno != EINTR && errno != EAGAIN && errno != EWOULDBLOCK) {
215 PLOG(WARNING) << config->getInstanceName() << " accept " << bindAddress.toString();
216 }
217 } while (--acceptsPerTick > 0);
218 }
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 230 of file SocketAcceptor.hpp.

230 {
231 delete this;
232 }

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

96 {
97 if (!config->getDisabled()) {
98 try {
100
101 LOG(DEBUG) << config->getInstanceName() << " Listen: starting";
102
103 bindAddress = config->Local::getSocketAddress();
104
105 if (physicalServerSocket.open(config->getSocketOptions(), PhysicalServerSocket::Flags::NONBLOCK) < 0) {
106 PLOG(ERROR) << config->getInstanceName() << " open " << bindAddress.toString();
107
108 switch (errno) {
109 case EMFILE:
110 case ENFILE:
111 case ENOBUFS:
112 case ENOMEM:
114 break;
115 default:
117 break;
118 }
119 } else {
120 LOG(DEBUG) << config->getInstanceName() << " open " << bindAddress.toString() << ": success";
121
122 if (physicalServerSocket.bind(bindAddress) < 0) {
123 PLOG(ERROR) << config->getInstanceName() << " bind " << bindAddress.toString();
124
125 switch (errno) {
126 case EADDRINUSE:
127
129 break;
130 default:
131
133 break;
134 }
135 } else {
136 LOG(DEBUG) << config->getInstanceName() << " bind " << bindAddress.toString() << ": success";
137
138 if (physicalServerSocket.listen(config->getBacklog()) < 0) {
139 PLOG(ERROR) << config->getInstanceName() << " listen " << bindAddress.toString();
140
141 switch (errno) {
142 case EADDRINUSE:
144 break;
145 default:
147 break;
148 }
149 } else {
150 LOG(DEBUG) << config->getInstanceName() << " listen " << bindAddress.toString() << ": success";
151
152 if (enable(physicalServerSocket.getFd())) {
153 LOG(DEBUG) << config->getInstanceName() << " enable " << bindAddress.toString() << ": success";
154 } else {
155 LOG(ERROR) << config->getInstanceName() << " enable " << bindAddress.toString()
156 << ": failed. No valid descriptor created";
157
158 state = core::socket::STATE(core::socket::STATE_FATAL, ECANCELED, "SocketAcceptor not enabled");
159 }
160 }
161 }
162 }
163
165 if (bindAddress.useNext()) {
167
168 LOG(INFO) << config->getInstanceName()
169 << ": Using next SocketAddress: " << config->Local::getSocketAddress().toString();
170
172 } else {
174 }
175 } catch (const typename SocketAddress::BadSocketAddress& badSocketAddress) {
178
179 LOG(ERROR) << state.what();
180
181 onStatus({}, state);
182 }
183 } else {
184 LOG(DEBUG) << config->getInstanceName() << ": disabled";
185
187 }
188
189 if (isEnabled()) {
191 } else {
192 destruct();
193 }
194 }
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().

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

223 {
224 destruct();
225 }

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: