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::shared_ptr< core::socket::stream::SocketContextFactory > &socketContextFactory, 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::shared_ptr< core::socket::stream::SocketContextFactorysocketContextFactory = nullptr
 
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 localAddress
 

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::shared_ptr< core::socket::stream::SocketContextFactory > &  socketContextFactory,
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.

68 : core::eventreceiver::AcceptEventReceiver(config->getInstanceName() + " SocketAcceptor", 0)
74 , config(config) {
75 atNextTick([this]() {
77 init();
78 } else {
79 destruct();
80 }
81 });
82 }
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< core::socket::stream::SocketContextFactory > socketContextFactory
std::shared_ptr< Config > config
std::function< void(const SocketAddress &, core::socket::State)> onStatus
State eventLoopState()
Definition State.cpp:52

References core::EventReceiver::atNextTick(), core::socket::stream::SocketAcceptor< PhysicalSocketServerT, ConfigT, SocketConnectionT >::config, core::eventLoopState(), core::socket::stream::SocketAcceptor< PhysicalSocketServerT, ConfigT, SocketConnectionT >::init(), core::socket::stream::SocketAcceptor< PhysicalSocketServerT, ConfigT, SocketConnectionT >::onConnect, core::socket::stream::SocketAcceptor< PhysicalSocketServerT, ConfigT, SocketConnectionT >::onConnected, core::socket::stream::SocketAcceptor< PhysicalSocketServerT, ConfigT, SocketConnectionT >::onDisconnect, core::socket::stream::SocketAcceptor< PhysicalSocketServerT, ConfigT, SocketConnectionT >::onStatus, core::RUNNING, and core::socket::stream::SocketAcceptor< PhysicalSocketServerT, ConfigT, SocketConnectionT >::socketContextFactory.

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

88 : core::eventreceiver::AcceptEventReceiver(socketAcceptor.config->getInstanceName() + " SocketAcceptor", 0)
89 , socketContextFactory(socketAcceptor.socketContextFactory)
90 , onConnect(socketAcceptor.onConnect)
91 , onConnected(socketAcceptor.onConnected)
92 , onDisconnect(socketAcceptor.onDisconnect)
93 , onStatus(socketAcceptor.onStatus)
94 , config(socketAcceptor.config) {
95 atNextTick([this]() {
97 init();
98 } else {
99 destruct();
100 }
101 });
102 }

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

107 {
108 }

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

206 {
207 int acceptsPerTick = config->getAcceptsPerTick();
208
209 do {
210 PhysicalServerSocket connectedPhysicalSocket(physicalServerSocket.accept4(PhysicalServerSocket::Flags::NONBLOCK),
211 physicalServerSocket.getBindAddress());
212 if (connectedPhysicalSocket.isValid()) {
213 LOG(DEBUG) << "[" << connectedPhysicalSocket.getFd() << " ]" << config->getInstanceName() << ": accept success: '"
214 << connectedPhysicalSocket.getBindAddress().toString() << "'";
215
216 SocketConnection* socketConnection = new SocketConnection(std::move(connectedPhysicalSocket), onDisconnect, config);
217
218 onConnect(socketConnection);
219 onConnected(socketConnection);
220 } else if (errno != EINTR && errno != EAGAIN && errno != EWOULDBLOCK) {
221 PLOG(WARNING) << config->getInstanceName() << " accept failed: '" << physicalServerSocket.getBindAddress().toString()
222 << "'";
223 }
224 } while (--acceptsPerTick > 0);
225 }
SocketConnectionT< PhysicalServerSocket, Config > SocketConnection
PhysicalSocketServerT PhysicalServerSocket
PhysicalServerSocket physicalServerSocket

References core::socket::stream::SocketAcceptor< PhysicalSocketServerT, ConfigT, SocketConnectionT >::config, core::socket::stream::SocketAcceptor< PhysicalSocketServerT, ConfigT, SocketConnectionT >::onConnect, core::socket::stream::SocketAcceptor< PhysicalSocketServerT, ConfigT, SocketConnectionT >::onConnected, core::socket::stream::SocketAcceptor< PhysicalSocketServerT, ConfigT, SocketConnectionT >::onDisconnect, and core::socket::stream::SocketAcceptor< PhysicalSocketServerT, ConfigT, SocketConnectionT >::physicalServerSocket.

◆ destruct()

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

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

113 {
114 if (!config->getDisabled()) {
115 try {
116 LOG(TRACE) << config->getInstanceName() << " Starting";
117
118 localAddress = config->Local::getSocketAddress();
119
120 core::socket::State state = core::socket::STATE_OK;
121
122 if (physicalServerSocket.open(config->getSocketOptions(), PhysicalServerSocket::Flags::NONBLOCK) < 0) {
123 switch (errno) {
124 case EMFILE:
125 case ENFILE:
126 case ENOBUFS:
127 case ENOMEM:
128 PLOG(DEBUG) << config->getInstanceName() << " open: '" << localAddress.toString() << "'";
129
130 state = core::socket::STATE_ERROR;
131 break;
132 default:
133 PLOG(DEBUG) << config->getInstanceName() << " open: '" << localAddress.toString() << "'";
134
135 state = core::socket::STATE_FATAL;
136 break;
137 }
138 } else if (physicalServerSocket.bind(localAddress) < 0) {
139 switch (errno) {
140 case EADDRINUSE:
141 PLOG(DEBUG) << config->getInstanceName() << " bind: '" << localAddress.toString() << "'";
142
143 state = core::socket::STATE_ERROR;
144 break;
145 default:
146 PLOG(DEBUG) << config->getInstanceName() << " bind: '" << localAddress.toString() << "'";
147
148 state = core::socket::STATE_FATAL;
149 break;
150 }
151 } else if (physicalServerSocket.listen(config->getBacklog()) < 0) {
152 switch (errno) {
153 case EADDRINUSE:
154 PLOG(DEBUG) << config->getInstanceName() << " listen: '" << localAddress.toString() << "'";
155
156 state = core::socket::STATE_ERROR;
157 break;
158 default:
159 PLOG(DEBUG) << config->getInstanceName() << " listen: '" << localAddress.toString() << "'";
160
161 state = core::socket::STATE_FATAL;
162 break;
163 }
164 } else {
165 if (enable(physicalServerSocket.getFd())) {
166 LOG(DEBUG) << config->getInstanceName() << " enabled: '" << localAddress.toString() << "' success";
167 } else {
168 LOG(DEBUG) << config->getInstanceName() << " enabled: '" << localAddress.toString() << "' failed";
169
170 state = core::socket::STATE(core::socket::STATE_FATAL, ECANCELED, "SocketAcceptor not enabled");
171 }
172 }
173
174 SocketAddress currentLocalAddress = localAddress;
175 if (localAddress.useNext()) {
176 onStatus(currentLocalAddress, (state | core::socket::State::NO_RETRY));
177
178 LOG(DEBUG) << config->getInstanceName() << " using next SocketAddress: '"
179 << config->Local::getSocketAddress().toString() << "'";
180
182 } else {
183 onStatus(currentLocalAddress, state);
184 }
185 } catch (const typename SocketAddress::BadSocketAddress& badSocketAddress) {
186 LOG(DEBUG) << config->getInstanceName() << " " << badSocketAddress.what();
187
188 onStatus({}, core::socket::STATE(badSocketAddress.getState(), badSocketAddress.getErrnum(), badSocketAddress.what()));
189 }
190 } else {
191 LOG(DEBUG) << config->getInstanceName() << " disabled";
192
193 onStatus({}, core::socket::STATE_DISABLED);
194 }
195
196 if (isEnabled()) {
198 } else {
199 destruct();
200 }
201 }
void setTimeout(const utils::Timeval &timeout)
static constexpr int NO_RETRY
Definition State.h:59
typename PhysicalServerSocket::SocketAddress SocketAddress

References core::socket::stream::SocketAcceptor< PhysicalSocketServerT, ConfigT, SocketConnectionT >::config, core::socket::stream::SocketAcceptor< PhysicalSocketServerT, ConfigT, SocketConnectionT >::destruct(), core::DescriptorEventReceiver::enable(), core::DescriptorEventReceiver::isEnabled(), core::socket::stream::SocketAcceptor< PhysicalSocketServerT, ConfigT, SocketConnectionT >::localAddress, core::socket::State::NO_RETRY, core::socket::stream::SocketAcceptor< PhysicalSocketServerT, ConfigT, SocketConnectionT >::onStatus, core::socket::State::operator|(), core::socket::stream::SocketAcceptor< PhysicalSocketServerT, ConfigT, SocketConnectionT >::physicalServerSocket, core::DescriptorEventReceiver::setTimeout(), and core::socket::stream::SocketAcceptor< PhysicalSocketServerT, ConfigT, SocketConnectionT >::useNextSocketAddress().

Referenced by core::socket::stream::SocketAcceptor< PhysicalServerSocketT, ConfigT, core::socket::stream::legacy::SocketConnection >::acceptEvent(), and core::socket::stream::SocketAcceptor< PhysicalSocketServerT, ConfigT, SocketConnectionT >::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 230 of file SocketAcceptor.hpp.

230 {
231 destruct();
232 }

References core::socket::stream::SocketAcceptor< PhysicalSocketServerT, ConfigT, SocketConnectionT >::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 core::socket::stream::SocketAcceptor< PhysicalSocketServerT, ConfigT, SocketConnectionT >::init().

Here is the caller graph for this function:

Member Data Documentation

◆ config

◆ localAddress

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

◆ onConnect

◆ onConnected

◆ onDisconnect

◆ 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

◆ physicalServerSocket

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

◆ socketContextFactory

template<typename PhysicalSocketServerT , typename ConfigT , template< typename PhysicalSocketServer, typename Config > typename SocketConnectionT>
std::shared_ptr<core::socket::stream::SocketContextFactory> core::socket::stream::SocketAcceptor< PhysicalSocketServerT, ConfigT, SocketConnectionT >::socketContextFactory = nullptr
protected

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