2017-08-08 13:53:47 +02:00
|
|
|
#ifndef ACCEPTOR_H_
|
|
|
|
#define ACCEPTOR_H_
|
|
|
|
|
2018-10-24 15:17:40 +02:00
|
|
|
#include <asio.hpp>
|
2018-10-24 13:52:27 +02:00
|
|
|
#include <atomic>
|
2018-03-06 22:22:19 +01:00
|
|
|
#include "Forwards.h"
|
2018-10-24 15:17:40 +02:00
|
|
|
#include <condition_variable>
|
|
|
|
#include <deque>
|
|
|
|
#include <memory>
|
2018-10-24 17:58:57 +02:00
|
|
|
#include <functional>
|
2018-10-24 15:17:40 +02:00
|
|
|
#include <mutex>
|
|
|
|
#include <typeinfo>
|
|
|
|
#include <vector>
|
|
|
|
|
2017-08-08 13:53:47 +02:00
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
namespace Core {
|
|
|
|
namespace Network {
|
|
|
|
|
|
|
|
class Connection;
|
|
|
|
|
2018-10-24 17:58:57 +02:00
|
|
|
class Acceptor : public std::enable_shared_from_this< Acceptor >
|
2017-08-08 13:53:47 +02:00
|
|
|
{
|
2018-08-29 21:40:59 +02:00
|
|
|
friend class Hive;
|
|
|
|
|
|
|
|
private:
|
|
|
|
HivePtr m_hive;
|
2018-10-24 15:17:40 +02:00
|
|
|
asio::ip::tcp::acceptor m_acceptor;
|
|
|
|
asio::strand m_io_strand;
|
2018-10-24 13:52:27 +02:00
|
|
|
std::atomic< uint32_t > m_error_state;
|
2018-08-29 21:40:59 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
Acceptor( const Acceptor& rhs );
|
|
|
|
|
|
|
|
Acceptor& operator=( const Acceptor& rhs );
|
|
|
|
|
2018-10-24 15:17:40 +02:00
|
|
|
void StartError( const asio::error_code& error );
|
2018-08-29 21:40:59 +02:00
|
|
|
|
|
|
|
void DispatchAccept( ConnectionPtr connection );
|
|
|
|
|
2018-10-24 15:17:40 +02:00
|
|
|
void HandleAccept( const asio::error_code& error, ConnectionPtr connection );
|
2018-08-29 21:40:59 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
// Called when a connection has connected to the server. This function
|
|
|
|
// should return true to invoke the connection's OnAccept function if the
|
|
|
|
// connection will be kept. If the connection will not be kept, the
|
|
|
|
// connection's Disconnect function should be called and the function
|
|
|
|
// should return false.
|
|
|
|
virtual bool OnAccept( ConnectionPtr connection, const std::string& host, uint16_t port );
|
|
|
|
|
|
|
|
// Called when an error is encountered. Most typically, this is when the
|
|
|
|
// acceptor is being closed via the Stop function or if the Listen is
|
|
|
|
// called on an address that is not available.
|
2018-10-24 15:17:40 +02:00
|
|
|
virtual void OnError( const asio::error_code& error );
|
2018-08-29 21:40:59 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
Acceptor( HivePtr hive );
|
|
|
|
|
|
|
|
virtual ~Acceptor();
|
|
|
|
|
|
|
|
// Returns the Hive object.
|
|
|
|
HivePtr GetHive();
|
|
|
|
|
|
|
|
// Returns the acceptor object.
|
2018-10-24 15:17:40 +02:00
|
|
|
asio::ip::tcp::acceptor& GetAcceptor();
|
2018-08-29 21:40:59 +02:00
|
|
|
|
|
|
|
// Returns the strand object.
|
2018-10-24 16:01:30 +02:00
|
|
|
asio::strand& GetStrand();
|
2018-08-29 21:40:59 +02:00
|
|
|
|
|
|
|
// Returns true if this object has an error associated with it.
|
|
|
|
bool HasError();
|
|
|
|
|
|
|
|
public:
|
|
|
|
// Begin listening on the specific network interface.
|
|
|
|
void Listen( const std::string& host, const uint16_t& port );
|
|
|
|
|
|
|
|
// Posts the connection to the listening interface. The next client that
|
|
|
|
// connections will be given this connection. If multiple calls to Accept
|
|
|
|
// are called at a time, then they are accepted in a FIFO order.
|
|
|
|
void Accept( ConnectionPtr connection );
|
|
|
|
|
|
|
|
// Stop the Acceptor from listening.
|
|
|
|
void Stop();
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
2017-08-08 13:53:47 +02:00
|
|
|
}
|
2017-12-18 12:36:52 +01:00
|
|
|
#endif
|