1
Fork 0
mirror of https://github.com/SapphireServer/Sapphire.git synced 2025-04-27 14:57:44 +00:00
sapphire/src/servers/Server_Common/Network/Acceptor.h

78 lines
2.6 KiB
C
Raw Normal View History

2017-08-08 13:53:47 +02:00
#ifndef ACCEPTOR_H_
#define ACCEPTOR_H_
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/shared_ptr.hpp>
2017-08-19 00:18:40 +02:00
#include "src/servers/Server_Common/Forwards.h"
2017-08-08 13:53:47 +02:00
namespace Core
{
namespace Network
{
class Connection;
class Acceptor : public boost::enable_shared_from_this< Acceptor >
{
friend class Hive;
private:
HivePtr m_hive;
boost::asio::ip::tcp::acceptor m_acceptor;
boost::asio::strand m_io_strand;
volatile uint32_t m_error_state;
private:
Acceptor( const Acceptor & rhs );
Acceptor & operator =( const Acceptor & rhs );
void StartError( const boost::system::error_code & error );
void DispatchAccept( ConnectionPtr connection );
void HandleAccept( const boost::system::error_code & error, ConnectionPtr connection );
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.
virtual void OnError( const boost::system::error_code & error );
public:
Acceptor( HivePtr hive );
virtual ~Acceptor();
// Returns the Hive object.
HivePtr GetHive();
// Returns the acceptor object.
boost::asio::ip::tcp::acceptor & GetAcceptor();
// Returns the strand object.
boost::asio::strand & GetStrand();
// 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();
};
}
}
#endif