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>
 

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 SocketConnectionT>
class core::socket::stream::SocketAcceptor< PhysicalSocketServerT, ConfigT, SocketConnectionT >

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

Definition at line 45 of file SocketAcceptor.h.

Member Typedef Documentation

◆ Config

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

Definition at line 50 of file SocketAcceptor.h.

◆ PhysicalServerSocket

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

Definition at line 47 of file SocketAcceptor.h.

◆ SocketAddress

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

Definition at line 51 of file SocketAcceptor.h.

◆ SocketConnection

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

Definition at line 52 of file SocketAcceptor.h.

Constructor & Destructor Documentation

◆ SocketAcceptor() [1/2]

template<typename PhysicalSocketServer , typename Config , template< 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 83 of file SocketAcceptor.hpp.

90 : core::eventreceiver::AcceptEventReceiver(config->getInstanceName() + " SocketAcceptor", 0)
96 , config(config) {
97 atNextTick([this]() {
98 init();
99 });
100 }
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

◆ SocketAcceptor() [2/2]

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

Definition at line 103 of file SocketAcceptor.hpp.

104 : core::eventreceiver::AcceptEventReceiver(socketAcceptor.config->getInstanceName() + " SocketAcceptor", 0)
105 , socketContextFactory(socketAcceptor.socketContextFactory)
106 , onConnect(socketAcceptor.onConnect)
107 , onConnected(socketAcceptor.onConnected)
108 , onDisconnect(socketAcceptor.onDisconnect)
109 , onStatus(socketAcceptor.onStatus)
110 , config(socketAcceptor.config) {
111 atNextTick([this]() {
112 init();
113 });
114 }

◆ ~SocketAcceptor()

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

Definition at line 117 of file SocketAcceptor.hpp.

117 {
118 }

Member Function Documentation

◆ acceptEvent()

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

Implements core::eventreceiver::AcceptEventReceiver.

Definition at line 211 of file SocketAcceptor.hpp.

211 {
212 int acceptsPerTick = config->getAcceptsPerTick();
213
214 do {
215 PhysicalServerSocket connectedPhysicalSocket(physicalServerSocket.accept4(PhysicalServerSocket::Flags::NONBLOCK),
216 physicalServerSocket.getBindAddress());
217 if (connectedPhysicalSocket.isValid()) {
218 LOG(DEBUG) << config->getInstanceName() << " [" << connectedPhysicalSocket.getFd() << "] accept success: '"
219 << connectedPhysicalSocket.getBindAddress().toString() << "'";
220
221 SocketConnection* socketConnection =
222 new SocketConnection(config->getInstanceName(),
223 std::move(connectedPhysicalSocket),
225 localAddress.toString(false),
226 getLocalSocketAddress<SocketAddress>(connectedPhysicalSocket, config),
227 getRemoteSocketAddress<SocketAddress>(connectedPhysicalSocket, config),
228 config->getReadTimeout(),
229 config->getWriteTimeout(),
230 config->getReadBlockSize(),
231 config->getWriteBlockSize(),
232 config->getTerminateTimeout());
233
234 onConnect(socketConnection);
235 onConnected(socketConnection);
236 } else if (errno != EINTR && errno != EAGAIN && errno != EWOULDBLOCK) {
237 PLOG(WARNING) << config->getInstanceName() << " accept failed: '" << physicalServerSocket.getBindAddress().toString()
238 << "'";
239 }
240 } while (--acceptsPerTick > 0);
241 }
SocketConnectionT< PhysicalServerSocket > SocketConnection
PhysicalSocketServerT PhysicalServerSocket
PhysicalServerSocket physicalServerSocket
SocketAddress getRemoteSocketAddress(PhysicalSocket &physicalSocket, Config &config)
SocketAddress getLocalSocketAddress(PhysicalSocket &physicalSocket, Config &config)

◆ destruct()

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

Reimplemented from core::EventReceiver.

Definition at line 249 of file SocketAcceptor.hpp.

249 {
250 delete this;
251 }

◆ init()

template<typename PhysicalSocketServer , typename Config , template< 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 121 of file SocketAcceptor.hpp.

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

◆ unobservedEvent()

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

Implements core::Observer.

Definition at line 244 of file SocketAcceptor.hpp.

244 {
245 destruct();
246 }

◆ useNextSocketAddress()

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

Member Data Documentation

◆ config

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

Definition at line 92 of file SocketAcceptor.h.

◆ localAddress

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

Definition at line 81 of file SocketAcceptor.h.

◆ onConnect

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

Definition at line 86 of file SocketAcceptor.h.

◆ onConnected

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

Definition at line 87 of file SocketAcceptor.h.

◆ onDisconnect

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

Definition at line 88 of file SocketAcceptor.h.

◆ onStatus

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

Definition at line 90 of file SocketAcceptor.h.

◆ physicalServerSocket

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

Definition at line 80 of file SocketAcceptor.h.

◆ socketContextFactory

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

Definition at line 84 of file SocketAcceptor.h.


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