TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
AsyncAcceptor Class Reference

#include <AsyncAcceptor.h>

Public Types

typedef void(* AcceptCallback )(tcp::socket &&newSocket, uint32 threadIndex)
 

Public Member Functions

 AsyncAcceptor (boost::asio::io_service &ioService, std::string const &bindIp, uint16 port)
 
template<class T >
void AsyncAccept ()
 
template<AcceptCallback acceptCallback>
void AsyncAcceptWithCallback ()
 
void Close ()
 
void SetSocketFactory (std::function< std::pair< tcp::socket *, uint32 >()> func)
 

Private Member Functions

std::pair< tcp::socket *, uint32DefeaultSocketFactory ()
 

Private Attributes

tcp::acceptor _acceptor
 
tcp::socket _socket
 
std::atomic< bool_closed
 
std::function< std::pair
< tcp::socket *, uint32 >)> 
_socketFactory
 

Member Typedef Documentation

typedef void(* AsyncAcceptor::AcceptCallback)(tcp::socket &&newSocket, uint32 threadIndex)

Constructor & Destructor Documentation

AsyncAcceptor::AsyncAcceptor ( boost::asio::io_service &  ioService,
std::string const bindIp,
uint16  port 
)
inline
33  :
34  _acceptor(ioService, tcp::endpoint(boost::asio::ip::address::from_string(bindIp), port)),
35  _socket(ioService), _closed(false), _socketFactory(std::bind(&AsyncAcceptor::DefeaultSocketFactory, this))
36  {
37  }
std::function< std::pair< tcp::socket *, uint32 >)> _socketFactory
Definition: AsyncAcceptor.h:86
tcp::socket _socket
Definition: AsyncAcceptor.h:84
tcp::acceptor _acceptor
Definition: AsyncAcceptor.h:83
std::pair< tcp::socket *, uint32 > DefeaultSocketFactory()
Definition: AsyncAcceptor.h:81
std::atomic< bool > _closed
Definition: AsyncAcceptor.h:85

Member Function Documentation

template<class T >
void AsyncAcceptor::AsyncAccept ( )
91 {
92  _acceptor.async_accept(_socket, [this](boost::system::error_code error)
93  {
94  if (!error)
95  {
96  try
97  {
98  // this-> is required here to fix an segmentation fault in gcc 4.7.2 - reason is lambdas in a templated class
99  std::make_shared<T>(std::move(this->_socket))->Start();
100  }
101  catch (boost::system::system_error const& err)
102  {
103  TC_LOG_INFO("network", "Failed to retrieve client's remote address %s", err.what());
104  }
105  }
106 
107  // lets slap some more this-> on this so we can fix this bug with gcc 4.7.2 throwing internals in yo face
108  if (!_closed)
109  this->AsyncAccept<T>();
110  });
111 }
tcp::socket _socket
Definition: AsyncAcceptor.h:84
tcp::acceptor _acceptor
Definition: AsyncAcceptor.h:83
#define TC_LOG_INFO(filterType__,...)
Definition: Log.h:201
std::atomic< bool > _closed
Definition: AsyncAcceptor.h:85

+ Here is the caller graph for this function:

template<AcceptCallback acceptCallback>
void AsyncAcceptor::AsyncAcceptWithCallback ( )
inline
44  {
45  tcp::socket* socket;
46  uint32 threadIndex;
47  std::tie(socket, threadIndex) = _socketFactory();
48  _acceptor.async_accept(*socket, [this, socket, threadIndex](boost::system::error_code error)
49  {
50  if (!error)
51  {
52  try
53  {
54  socket->non_blocking(true);
55 
56  acceptCallback(std::move(*socket), threadIndex);
57  }
58  catch (boost::system::system_error const& err)
59  {
60  TC_LOG_INFO("network", "Failed to initialize client's socket %s", err.what());
61  }
62  }
63 
64  if (!_closed)
65  this->AsyncAcceptWithCallback<acceptCallback>();
66  });
67  }
std::function< std::pair< tcp::socket *, uint32 >)> _socketFactory
Definition: AsyncAcceptor.h:86
tcp::acceptor _acceptor
Definition: AsyncAcceptor.h:83
uint32_t uint32
Definition: Define.h:150
#define TC_LOG_INFO(filterType__,...)
Definition: Log.h:201
std::atomic< bool > _closed
Definition: AsyncAcceptor.h:85

+ Here is the caller graph for this function:

void AsyncAcceptor::Close ( )
inline
70  {
71  if (_closed.exchange(true))
72  return;
73 
74  boost::system::error_code err;
75  _acceptor.close(err);
76  }
tcp::acceptor _acceptor
Definition: AsyncAcceptor.h:83
std::atomic< bool > _closed
Definition: AsyncAcceptor.h:85

+ Here is the caller graph for this function:

std::pair<tcp::socket*, uint32> AsyncAcceptor::DefeaultSocketFactory ( )
inlineprivate
81 { return std::make_pair(&_socket, 0); }
tcp::socket _socket
Definition: AsyncAcceptor.h:84
void AsyncAcceptor::SetSocketFactory ( std::function< std::pair< tcp::socket *, uint32 >()>  func)
inline
78 { _socketFactory = func; }
std::function< std::pair< tcp::socket *, uint32 >)> _socketFactory
Definition: AsyncAcceptor.h:86

+ Here is the caller graph for this function:

Member Data Documentation

tcp::acceptor AsyncAcceptor::_acceptor
private
std::atomic<bool> AsyncAcceptor::_closed
private
tcp::socket AsyncAcceptor::_socket
private
std::function<std::pair<tcp::socket*, uint32>)> AsyncAcceptor::_socketFactory
private

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