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 67 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 72 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 69 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 73 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 74 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 105 of file SocketAcceptor.hpp.

112 : core::eventreceiver::AcceptEventReceiver(config->getInstanceName() + " SocketAcceptor", 0)
118 , config(config) {
119 atNextTick([this]() {
121 init();
122 } else {
123 destruct();
124 }
125 });
126 }
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

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

130 : core::eventreceiver::AcceptEventReceiver(socketAcceptor.config->getInstanceName() + " SocketAcceptor", 0)
131 , socketContextFactory(socketAcceptor.socketContextFactory)
132 , onConnect(socketAcceptor.onConnect)
133 , onConnected(socketAcceptor.onConnected)
134 , onDisconnect(socketAcceptor.onDisconnect)
135 , onStatus(socketAcceptor.onStatus)
136 , config(socketAcceptor.config) {
137 atNextTick([this]() {
139 init();
140 } else {
141 destruct();
142 }
143 });
144 }

◆ ~SocketAcceptor()

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

Definition at line 147 of file SocketAcceptor.hpp.

147 {
148 }

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

242 {
243 int acceptsPerTick = config->getAcceptsPerTick();
244
245 do {
246 PhysicalServerSocket connectedPhysicalSocket(physicalServerSocket.accept4(PhysicalServerSocket::Flags::NONBLOCK),
247 physicalServerSocket.getBindAddress());
248 if (connectedPhysicalSocket.isValid()) {
249 LOG(DEBUG) << "[" << connectedPhysicalSocket.getFd() << " ]" << config->getInstanceName() << ": accept success: '"
250 << connectedPhysicalSocket.getBindAddress().toString() << "'";
251
252 SocketConnection* socketConnection =
253 new SocketConnection(config->getInstanceName(),
254 std::move(connectedPhysicalSocket),
256 localAddress.toString(false),
257 getLocalSocketAddress<SocketAddress>(connectedPhysicalSocket, config),
258 getRemoteSocketAddress<SocketAddress>(connectedPhysicalSocket, config),
259 config->getReadTimeout(),
260 config->getWriteTimeout(),
261 config->getReadBlockSize(),
262 config->getWriteBlockSize(),
263 config->getTerminateTimeout());
264
265 onConnect(socketConnection);
266 onConnected(socketConnection);
267 } else if (errno != EINTR && errno != EAGAIN && errno != EWOULDBLOCK) {
268 PLOG(WARNING) << config->getInstanceName() << " accept failed: '" << physicalServerSocket.getBindAddress().toString()
269 << "'";
270 }
271 } while (--acceptsPerTick > 0);
272 }
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 280 of file SocketAcceptor.hpp.

280 {
281 delete this;
282 }

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

151 {
152 if (!config->getDisabled()) {
153 try {
154 LOG(TRACE) << config->getInstanceName() << " Starting";
155
156 localAddress = config->Local::getSocketAddress();
157
158 core::socket::State state = core::socket::STATE_OK;
159
160 if (physicalServerSocket.open(config->getSocketOptions(), PhysicalServerSocket::Flags::NONBLOCK) < 0) {
161 switch (errno) {
162 case EMFILE:
163 case ENFILE:
164 case ENOBUFS:
165 case ENOMEM:
166 PLOG(DEBUG) << config->getInstanceName() << " open: '" << localAddress.toString() << "'";
167
168 state = core::socket::STATE_ERROR;
169 break;
170 default:
171 PLOG(DEBUG) << config->getInstanceName() << " open: '" << localAddress.toString() << "'";
172
173 state = core::socket::STATE_FATAL;
174 break;
175 }
176 } else if (physicalServerSocket.bind(localAddress) < 0) {
177 switch (errno) {
178 case EADDRINUSE:
179 PLOG(DEBUG) << config->getInstanceName() << " bind: '" << localAddress.toString() << "'";
180
181 state = core::socket::STATE_ERROR;
182 break;
183 default:
184 PLOG(DEBUG) << config->getInstanceName() << " bind: '" << localAddress.toString() << "'";
185
186 state = core::socket::STATE_FATAL;
187 break;
188 }
189 } else if (physicalServerSocket.listen(config->getBacklog()) < 0) {
190 switch (errno) {
191 case EADDRINUSE:
192 PLOG(DEBUG) << config->getInstanceName() << " listen: '" << localAddress.toString() << "'";
193
194 state = core::socket::STATE_ERROR;
195 break;
196 default:
197 PLOG(DEBUG) << config->getInstanceName() << " listen: '" << localAddress.toString() << "'";
198
199 state = core::socket::STATE_FATAL;
200 break;
201 }
202 } else {
203 if (enable(physicalServerSocket.getFd())) {
204 LOG(DEBUG) << config->getInstanceName() << " enabled: '" << localAddress.toString() << "' success";
205 } else {
206 LOG(DEBUG) << config->getInstanceName() << " enabled: '" << localAddress.toString() << "' failed";
207
208 state = core::socket::STATE(core::socket::STATE_FATAL, ECANCELED, "SocketAcceptor not enabled");
209 }
210 }
211
212 SocketAddress currentLocalAddress = localAddress;
213 if (localAddress.useNext()) {
214 onStatus(currentLocalAddress, (state | core::socket::State::NO_RETRY));
215
216 LOG(DEBUG) << config->getInstanceName() << " using next SocketAddress: '"
217 << config->Local::getSocketAddress().toString() << "'";
218
220 } else {
221 onStatus(currentLocalAddress, state);
222 }
223 } catch (const typename SocketAddress::BadSocketAddress& badSocketAddress) {
224 LOG(DEBUG) << config->getInstanceName() << " " << badSocketAddress.what();
225
226 onStatus({}, core::socket::STATE(badSocketAddress.getState(), badSocketAddress.getErrnum(), badSocketAddress.what()));
227 }
228 } else {
229 LOG(DEBUG) << config->getInstanceName() << " disabled";
230
231 onStatus({}, core::socket::STATE_DISABLED);
232 }
233
234 if (isEnabled()) {
236 } else {
237 destruct();
238 }
239 }
void setTimeout(const utils::Timeval &timeout)
static constexpr int NO_RETRY
Definition State.h:59
typename PhysicalServerSocket::SocketAddress SocketAddress

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

275 {
276 destruct();
277 }

◆ 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 114 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 103 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 108 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 109 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 110 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 112 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 102 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 106 of file SocketAcceptor.h.


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