mirror of
https://github.com/SapphireServer/Sapphire.git
synced 2025-04-27 14:57:44 +00:00
Refactor -> this has been bothering me for a while...
This commit is contained in:
parent
b4f1f3afe4
commit
f3ff018ade
13 changed files with 165 additions and 172 deletions
|
@ -2,15 +2,15 @@
|
|||
#include "Acceptor.h"
|
||||
#include "Connection.h"
|
||||
|
||||
namespace Sapphire {
|
||||
namespace Network {
|
||||
namespace Sapphire::Network
|
||||
{
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
Acceptor::Acceptor( HivePtr hive ) :
|
||||
m_hive( hive ),
|
||||
m_acceptor( hive->GetService() ),
|
||||
m_io_strand( hive->GetService() ),
|
||||
m_acceptor( hive->getService() ),
|
||||
m_io_strand( hive->getService() ),
|
||||
m_error_state( 0 )
|
||||
{
|
||||
}
|
||||
|
@ -20,18 +20,17 @@ Acceptor::~Acceptor()
|
|||
}
|
||||
|
||||
|
||||
bool Acceptor::OnAccept( ConnectionPtr connection, const std::string& host, uint16_t port )
|
||||
bool Acceptor::onAccept( ConnectionPtr connection, const std::string& host, uint16_t port )
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
void Acceptor::OnError( const asio::error_code& error )
|
||||
void Acceptor::onError( const asio::error_code& error )
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
void Acceptor::StartError( const asio::error_code& error )
|
||||
void Acceptor::startError( const asio::error_code& error )
|
||||
{
|
||||
uint32_t v1 = 1;
|
||||
uint32_t v2 = 0;
|
||||
|
@ -40,60 +39,60 @@ void Acceptor::StartError( const asio::error_code& error )
|
|||
asio::error_code ec;
|
||||
m_acceptor.cancel( ec );
|
||||
m_acceptor.close( ec );
|
||||
OnError( error );
|
||||
onError( error );
|
||||
}
|
||||
}
|
||||
|
||||
void Acceptor::DispatchAccept( ConnectionPtr connection )
|
||||
void Acceptor::dispatchAccept( ConnectionPtr connection )
|
||||
{
|
||||
m_acceptor.async_accept( connection->GetSocket(),
|
||||
connection->GetStrand().wrap( std::bind( &Acceptor::HandleAccept,
|
||||
m_acceptor.async_accept( connection->getSocket(),
|
||||
connection->getStrand().wrap( std::bind( &Acceptor::handleAccept,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1,
|
||||
connection ) ) );
|
||||
}
|
||||
|
||||
void Acceptor::HandleAccept( const asio::error_code& error, ConnectionPtr connection )
|
||||
void Acceptor::handleAccept( const asio::error_code& error, ConnectionPtr connection )
|
||||
{
|
||||
if( error || HasError() || m_hive->HasStopped() )
|
||||
if( error || hasError() || m_hive->hasStopped() )
|
||||
{
|
||||
connection->StartError( error );
|
||||
connection->startError( error );
|
||||
}
|
||||
else
|
||||
{
|
||||
if( connection->GetSocket().is_open() )
|
||||
if( connection->getSocket().is_open() )
|
||||
{
|
||||
if( OnAccept( connection,
|
||||
connection->GetSocket().remote_endpoint().address().to_string(),
|
||||
connection->GetSocket().remote_endpoint().port() ) )
|
||||
if( onAccept( connection,
|
||||
connection->getSocket().remote_endpoint().address().to_string(),
|
||||
connection->getSocket().remote_endpoint().port() ) )
|
||||
{
|
||||
connection->OnAccept( m_acceptor.local_endpoint().address().to_string(),
|
||||
connection->onAccept( m_acceptor.local_endpoint().address().to_string(),
|
||||
m_acceptor.local_endpoint().port() );
|
||||
connection->Recv();
|
||||
connection->recv();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
connection->StartError( error );
|
||||
connection->startError( error );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Acceptor::Stop()
|
||||
void Acceptor::stop()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void Acceptor::Accept( ConnectionPtr connection )
|
||||
void Acceptor::accept( ConnectionPtr connection )
|
||||
{
|
||||
m_io_strand.post( std::bind( &Acceptor::DispatchAccept, shared_from_this(), connection ) );
|
||||
m_io_strand.post( std::bind( &Acceptor::dispatchAccept, shared_from_this(), connection ) );
|
||||
}
|
||||
|
||||
void Acceptor::Listen( const std::string& host, const uint16_t& port )
|
||||
void Acceptor::listen( const std::string& host, const uint16_t& port )
|
||||
{
|
||||
try
|
||||
{
|
||||
asio::ip::tcp::resolver resolver( m_hive->GetService() );
|
||||
asio::ip::tcp::resolver resolver( m_hive->getService() );
|
||||
asio::ip::tcp::resolver::query query( host, std::to_string( port ) );
|
||||
asio::ip::tcp::endpoint endpoint = *resolver.resolve( query );
|
||||
|
||||
|
@ -110,17 +109,17 @@ void Acceptor::Listen( const std::string& host, const uint16_t& port )
|
|||
|
||||
}
|
||||
|
||||
HivePtr Acceptor::GetHive()
|
||||
HivePtr Acceptor::getHive()
|
||||
{
|
||||
return m_hive;
|
||||
}
|
||||
|
||||
asio::ip::tcp::acceptor& Acceptor::GetAcceptor()
|
||||
asio::ip::tcp::acceptor& Acceptor::getAcceptor()
|
||||
{
|
||||
return m_acceptor;
|
||||
}
|
||||
|
||||
bool Acceptor::HasError()
|
||||
bool Acceptor::hasError()
|
||||
{
|
||||
uint32_t v1 = 1;
|
||||
uint32_t v2 = 1;
|
||||
|
@ -128,4 +127,3 @@ bool Acceptor::HasError()
|
|||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -33,11 +33,11 @@ namespace Sapphire::Network
|
|||
|
||||
Acceptor& operator=( const Acceptor& rhs );
|
||||
|
||||
void StartError( const asio::error_code& error );
|
||||
void startError( const asio::error_code& error );
|
||||
|
||||
void DispatchAccept( ConnectionPtr connection );
|
||||
void dispatchAccept( ConnectionPtr connection );
|
||||
|
||||
void HandleAccept( const asio::error_code& error, ConnectionPtr connection );
|
||||
void handleAccept( const asio::error_code& error, ConnectionPtr connection );
|
||||
|
||||
private:
|
||||
// Called when a connection has connected to the server. This function
|
||||
|
@ -45,12 +45,12 @@ namespace Sapphire::Network
|
|||
// 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 );
|
||||
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 asio::error_code& error );
|
||||
virtual void onError( const asio::error_code& error );
|
||||
|
||||
public:
|
||||
Acceptor( HivePtr hive );
|
||||
|
@ -58,28 +58,28 @@ namespace Sapphire::Network
|
|||
virtual ~Acceptor();
|
||||
|
||||
// Returns the Hive object.
|
||||
HivePtr GetHive();
|
||||
HivePtr getHive();
|
||||
|
||||
// Returns the acceptor object.
|
||||
asio::ip::tcp::acceptor& GetAcceptor();
|
||||
asio::ip::tcp::acceptor& getAcceptor();
|
||||
|
||||
// Returns the strand object.
|
||||
asio::strand& GetStrand();
|
||||
asio::strand& getStrand();
|
||||
|
||||
// Returns true if this object has an error associated with it.
|
||||
bool HasError();
|
||||
bool hasError();
|
||||
|
||||
public:
|
||||
// Begin listening on the specific network interface.
|
||||
void Listen( const std::string& host, const uint16_t& port );
|
||||
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 );
|
||||
void accept( ConnectionPtr connection );
|
||||
|
||||
// Stop the Acceptor from listening.
|
||||
void Stop();
|
||||
void stop();
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -3,14 +3,14 @@
|
|||
#include <functional>
|
||||
#include "Framework.h"
|
||||
|
||||
namespace Sapphire {
|
||||
namespace Network {
|
||||
namespace Sapphire::Network
|
||||
{
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
Connection::Connection( HivePtr hive, FrameworkPtr pFw ) :
|
||||
m_hive( hive ),
|
||||
m_socket( hive->GetService() ),
|
||||
m_io_strand( hive->GetService() ),
|
||||
m_socket( hive->getService() ),
|
||||
m_io_strand( hive->getService() ),
|
||||
m_receive_buffer_size( 32000 ),
|
||||
m_error_state( 0 ),
|
||||
m_pFw( pFw )
|
||||
|
@ -21,7 +21,7 @@ Connection::~Connection()
|
|||
{
|
||||
}
|
||||
|
||||
void Connection::Bind( const std::string& ip, uint16_t port )
|
||||
void Connection::bind( const std::string& ip, uint16_t port )
|
||||
{
|
||||
asio::ip::tcp::endpoint endpoint( asio::ip::address::from_string( ip ), port );
|
||||
m_socket.open( endpoint.protocol() );
|
||||
|
@ -29,27 +29,27 @@ void Connection::Bind( const std::string& ip, uint16_t port )
|
|||
m_socket.bind( endpoint );
|
||||
}
|
||||
|
||||
void Connection::StartSend()
|
||||
void Connection::startSend()
|
||||
{
|
||||
if( !m_pending_sends.empty() )
|
||||
{
|
||||
asio::async_write( m_socket,
|
||||
asio::buffer( m_pending_sends.front() ),
|
||||
m_io_strand.wrap( std::bind( &Connection::HandleSend,
|
||||
m_io_strand.wrap( std::bind( &Connection::handleSend,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1,
|
||||
m_pending_sends.begin() ) ) );
|
||||
}
|
||||
}
|
||||
|
||||
void Connection::StartRecv( int32_t total_bytes )
|
||||
void Connection::startRecv( int32_t total_bytes )
|
||||
{
|
||||
if( total_bytes > 0 )
|
||||
{
|
||||
m_recv_buffer.resize( total_bytes );
|
||||
asio::async_read( m_socket,
|
||||
asio::buffer( m_recv_buffer ),
|
||||
m_io_strand.wrap( std::bind( &Connection::HandleRecv,
|
||||
m_io_strand.wrap( std::bind( &Connection::handleRecv,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1,
|
||||
std::placeholders::_2 ) ) );
|
||||
|
@ -58,14 +58,14 @@ void Connection::StartRecv( int32_t total_bytes )
|
|||
{
|
||||
m_recv_buffer.resize( m_receive_buffer_size );
|
||||
m_socket.async_read_some( asio::buffer( m_recv_buffer ),
|
||||
m_io_strand.wrap( std::bind( &Connection::HandleRecv,
|
||||
m_io_strand.wrap( std::bind( &Connection::handleRecv,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1,
|
||||
std::placeholders::_2 ) ) );
|
||||
}
|
||||
}
|
||||
|
||||
void Connection::StartError( const asio::error_code& error )
|
||||
void Connection::startError( const asio::error_code& error )
|
||||
{
|
||||
uint32_t v1 = 1;
|
||||
uint32_t v2 = 0;
|
||||
|
@ -74,144 +74,140 @@ void Connection::StartError( const asio::error_code& error )
|
|||
asio::error_code ec;
|
||||
m_socket.shutdown( asio::ip::tcp::socket::shutdown_both, ec );
|
||||
m_socket.close( ec );
|
||||
OnError( error );
|
||||
onError( error );
|
||||
}
|
||||
}
|
||||
|
||||
void Connection::HandleConnect( const asio::error_code& error )
|
||||
void Connection::handleConnect( const asio::error_code& error )
|
||||
{
|
||||
if( error || HasError() || m_hive->HasStopped() )
|
||||
if( error || hasError() || m_hive->hasStopped() )
|
||||
{
|
||||
StartError( error );
|
||||
startError( error );
|
||||
}
|
||||
else
|
||||
{
|
||||
if( m_socket.is_open() )
|
||||
{
|
||||
OnConnect( m_socket.remote_endpoint().address().to_string(), m_socket.remote_endpoint().port() );
|
||||
Recv();
|
||||
onConnect( m_socket.remote_endpoint().address().to_string(), m_socket.remote_endpoint().port() );
|
||||
recv();
|
||||
}
|
||||
else
|
||||
{
|
||||
StartError( error );
|
||||
startError( error );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
Connection::HandleSend( const asio::error_code& error, std::list< std::vector< uint8_t > >::iterator itr )
|
||||
Connection::handleSend( const asio::error_code& error, std::list< std::vector< uint8_t > >::iterator itr )
|
||||
{
|
||||
if( error || HasError() || m_hive->HasStopped() )
|
||||
if( error || hasError() || m_hive->hasStopped() )
|
||||
{
|
||||
StartError( error );
|
||||
startError( error );
|
||||
}
|
||||
else
|
||||
{
|
||||
OnSend( *itr );
|
||||
onSend( *itr );
|
||||
m_pending_sends.erase( itr );
|
||||
StartSend();
|
||||
startSend();
|
||||
}
|
||||
}
|
||||
|
||||
void Connection::HandleRecv( const asio::error_code& error, int32_t actual_bytes )
|
||||
void Connection::handleRecv( const asio::error_code& error, int32_t actual_bytes )
|
||||
{
|
||||
if( error || HasError() || m_hive->HasStopped() )
|
||||
if( error || hasError() || m_hive->hasStopped() )
|
||||
{
|
||||
StartError( error );
|
||||
startError( error );
|
||||
}
|
||||
else
|
||||
{
|
||||
m_recv_buffer.resize( actual_bytes );
|
||||
OnRecv( m_recv_buffer );
|
||||
Recv();
|
||||
onRecv( m_recv_buffer );
|
||||
recv();
|
||||
m_pending_recvs.pop_front();
|
||||
if( !m_pending_recvs.empty() )
|
||||
{
|
||||
StartRecv( m_pending_recvs.front() );
|
||||
startRecv( m_pending_recvs.front() );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Connection::DispatchSend( std::vector< uint8_t > buffer )
|
||||
void Connection::dispatchSend( std::vector< uint8_t > buffer )
|
||||
{
|
||||
bool should_start_send = m_pending_sends.empty();
|
||||
m_pending_sends.push_back( buffer );
|
||||
if( should_start_send )
|
||||
{
|
||||
StartSend();
|
||||
startSend();
|
||||
}
|
||||
}
|
||||
|
||||
void Connection::DispatchRecv( int32_t total_bytes )
|
||||
void Connection::dispatchRecv( int32_t total_bytes )
|
||||
{
|
||||
bool should_start_receive = m_pending_recvs.empty();
|
||||
m_pending_recvs.push_back( total_bytes );
|
||||
if( should_start_receive )
|
||||
{
|
||||
StartRecv( total_bytes );
|
||||
startRecv( total_bytes );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Connection::Connect( const std::string& host, uint16_t port )
|
||||
void Connection::connect( const std::string& host, uint16_t port )
|
||||
{
|
||||
asio::ip::tcp::resolver resolver( m_hive->GetService() );
|
||||
asio::ip::tcp::resolver resolver( m_hive->getService() );
|
||||
asio::ip::tcp::resolver::query query( host, std::to_string( port ) );
|
||||
asio::ip::tcp::resolver::iterator iterator = resolver.resolve( query );
|
||||
m_socket.async_connect( *iterator,
|
||||
m_io_strand.wrap( std::bind( &Connection::HandleConnect, shared_from_this(), std::placeholders::_1 ) ) );
|
||||
m_io_strand.wrap( std::bind( &Connection::handleConnect, shared_from_this(), std::placeholders::_1 ) ) );
|
||||
|
||||
}
|
||||
|
||||
void Connection::Disconnect()
|
||||
void Connection::disconnect()
|
||||
{
|
||||
OnDisconnect();
|
||||
onDisconnect();
|
||||
m_socket.close();
|
||||
}
|
||||
|
||||
void Connection::Recv( int32_t total_bytes )
|
||||
void Connection::recv( int32_t total_bytes )
|
||||
{
|
||||
m_io_strand.post( std::bind( &Connection::DispatchRecv, shared_from_this(), total_bytes ) );
|
||||
m_io_strand.post( std::bind( &Connection::dispatchRecv, shared_from_this(), total_bytes ) );
|
||||
}
|
||||
|
||||
void Connection::Send( const std::vector< uint8_t >& buffer )
|
||||
void Connection::send( const std::vector< uint8_t >& buffer )
|
||||
{
|
||||
m_io_strand.post( std::bind( &Connection::DispatchSend, shared_from_this(), buffer ) );
|
||||
m_io_strand.post( std::bind( &Connection::dispatchSend, shared_from_this(), buffer ) );
|
||||
}
|
||||
|
||||
asio::ip::tcp::socket& Connection::GetSocket()
|
||||
asio::ip::tcp::socket& Connection::getSocket()
|
||||
{
|
||||
return m_socket;
|
||||
}
|
||||
|
||||
asio::strand& Connection::GetStrand()
|
||||
asio::strand& Connection::getStrand()
|
||||
{
|
||||
return m_io_strand;
|
||||
}
|
||||
|
||||
HivePtr Connection::GetHive()
|
||||
HivePtr Connection::getHive()
|
||||
{
|
||||
return m_hive;
|
||||
}
|
||||
|
||||
void Connection::SetReceiveBufferSize( int32_t size )
|
||||
void Connection::setReceiveBufferSize( int32_t size )
|
||||
{
|
||||
m_receive_buffer_size = size;
|
||||
}
|
||||
|
||||
int32_t Connection::GetReceiveBufferSize() const
|
||||
int32_t Connection::getReceiveBufferSize() const
|
||||
{
|
||||
return m_receive_buffer_size;
|
||||
}
|
||||
|
||||
bool Connection::HasError()
|
||||
bool Connection::hasError()
|
||||
{
|
||||
uint32_t v1 = 1;
|
||||
uint32_t v2 = 1;
|
||||
return ( m_error_state.compare_exchange_strong( v1, v2 ) );
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
//-----------------------------------------------------------------------------
|
||||
|
|
|
@ -55,94 +55,94 @@ namespace Sapphire::Network
|
|||
|
||||
Connection& operator=( const Connection& rhs );
|
||||
|
||||
void StartSend();
|
||||
void startSend();
|
||||
|
||||
void StartRecv( int32_t total_bytes );
|
||||
void startRecv( int32_t total_bytes );
|
||||
|
||||
void StartError( const asio::error_code& error );
|
||||
void startError( const asio::error_code& error );
|
||||
|
||||
void DispatchSend( std::vector< uint8_t > buffer );
|
||||
void dispatchSend( std::vector< uint8_t > buffer );
|
||||
|
||||
void DispatchRecv( int32_t total_bytes );
|
||||
void dispatchRecv( int32_t total_bytes );
|
||||
|
||||
void HandleConnect( const asio::error_code& error );
|
||||
void handleConnect( const asio::error_code& error );
|
||||
|
||||
void HandleSend( const asio::error_code& error, std::list< std::vector< uint8_t > >::iterator itr );
|
||||
void handleSend( const asio::error_code& error, std::list< std::vector< uint8_t > >::iterator itr );
|
||||
|
||||
void HandleRecv( const asio::error_code& error, int32_t actual_bytes );
|
||||
void handleRecv( const asio::error_code& error, int32_t actual_bytes );
|
||||
|
||||
|
||||
private:
|
||||
// Called when the connection has successfully connected to the local host.
|
||||
virtual void OnAccept( const std::string& host, uint16_t port )
|
||||
virtual void onAccept( const std::string& host, uint16_t port )
|
||||
{
|
||||
};
|
||||
|
||||
// Called when the connection has successfully connected to the remote host.
|
||||
virtual void OnConnect( const std::string& host, uint16_t port )
|
||||
virtual void onConnect( const std::string& host, uint16_t port )
|
||||
{
|
||||
};
|
||||
|
||||
// Called when data has been sent by the connection.
|
||||
virtual void OnSend( const std::vector< uint8_t >& buffer )
|
||||
virtual void onSend( const std::vector< uint8_t >& buffer )
|
||||
{
|
||||
};
|
||||
|
||||
// Called when data has been received by the connection.
|
||||
virtual void OnRecv( std::vector< uint8_t >& buffer )
|
||||
virtual void onRecv( std::vector< uint8_t >& buffer )
|
||||
{
|
||||
};
|
||||
|
||||
// Called when an error is encountered.
|
||||
virtual void OnError( const asio::error_code& error )
|
||||
virtual void onError( const asio::error_code& error )
|
||||
{
|
||||
};
|
||||
|
||||
// Called when the connection has been disconnected
|
||||
virtual void OnDisconnect()
|
||||
virtual void onDisconnect()
|
||||
{
|
||||
};
|
||||
|
||||
public:
|
||||
// Returns the Hive object.
|
||||
HivePtr GetHive();
|
||||
HivePtr getHive();
|
||||
|
||||
// Returns the socket object.
|
||||
asio::ip::tcp::socket& GetSocket();
|
||||
asio::ip::tcp::socket& getSocket();
|
||||
|
||||
// Returns the strand object.
|
||||
asio::strand& GetStrand();
|
||||
asio::strand& getStrand();
|
||||
|
||||
// Sets the application specific receive buffer size used. For stream
|
||||
// based protocols such as HTTP, you want this to be pretty large, like
|
||||
// 64kb. For packet based protocols, then it will be much smaller,
|
||||
// usually 512b - 8kb depending on the protocol. The default value is
|
||||
// 4kb.
|
||||
void SetReceiveBufferSize( int32_t size );
|
||||
void setReceiveBufferSize( int32_t size );
|
||||
|
||||
// Returns the size of the receive buffer size of the current object.
|
||||
int32_t GetReceiveBufferSize() const;
|
||||
int32_t getReceiveBufferSize() const;
|
||||
|
||||
// Returns true if this object has an error associated with it.
|
||||
bool HasError();
|
||||
bool hasError();
|
||||
|
||||
// Binds the socket to the specified interface.
|
||||
void Bind( const std::string& ip, uint16_t port );
|
||||
void bind( const std::string& ip, uint16_t port );
|
||||
|
||||
// Starts an a/synchronous connect.
|
||||
void Connect( const std::string& host, uint16_t port );
|
||||
void connect( const std::string& host, uint16_t port );
|
||||
|
||||
// Posts data to be sent to the connection.
|
||||
void Send( const std::vector< uint8_t >& buffer );
|
||||
void send( const std::vector< uint8_t >& buffer );
|
||||
|
||||
// Posts a recv for the connection to process. If total_bytes is 0, then
|
||||
// as many bytes as possible up to GetReceiveBufferSize() will be
|
||||
// waited for. If Recv is not 0, then the connection will wait for exactly
|
||||
// total_bytes before invoking OnRecv.
|
||||
void Recv( int32_t total_bytes = 0 );
|
||||
void recv( int32_t total_bytes = 0 );
|
||||
|
||||
// Posts an asynchronous disconnect event for the object to process.
|
||||
void Disconnect();
|
||||
void disconnect();
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
@ -155,9 +155,9 @@ namespace Sapphire::Network
|
|||
try
|
||||
{
|
||||
AcceptorPtr acceptor( new Acceptor( pHive ) );
|
||||
acceptor->Listen( listenIp, port );
|
||||
acceptor->listen( listenIp, port );
|
||||
std::shared_ptr< T > connection( new T( pHive, acceptor, pFw ) );
|
||||
acceptor->Accept( connection );
|
||||
acceptor->accept( connection );
|
||||
return connection;
|
||||
}
|
||||
catch( std::runtime_error e )
|
||||
|
|
|
@ -2,8 +2,8 @@
|
|||
#include <functional>
|
||||
#include "Hive.h"
|
||||
|
||||
namespace Sapphire {
|
||||
namespace Network {
|
||||
namespace Sapphire::Network
|
||||
{
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
|
@ -17,29 +17,29 @@ Hive::~Hive()
|
|||
{
|
||||
}
|
||||
|
||||
asio::io_service& Hive::GetService()
|
||||
asio::io_service& Hive::getService()
|
||||
{
|
||||
return m_io_service;
|
||||
}
|
||||
|
||||
bool Hive::HasStopped()
|
||||
bool Hive::hasStopped()
|
||||
{
|
||||
uint32_t v1 = 1;
|
||||
uint32_t v2 = 1;
|
||||
return m_shutdown.compare_exchange_strong( v1, v2 );
|
||||
}
|
||||
|
||||
void Hive::Poll()
|
||||
void Hive::poll()
|
||||
{
|
||||
m_io_service.poll();
|
||||
}
|
||||
|
||||
void Hive::Run()
|
||||
void Hive::run()
|
||||
{
|
||||
m_io_service.run();
|
||||
}
|
||||
|
||||
void Hive::Stop()
|
||||
void Hive::stop()
|
||||
{
|
||||
uint32_t v1 = 1;
|
||||
uint32_t v2 = 0;
|
||||
|
@ -51,7 +51,7 @@ void Hive::Stop()
|
|||
}
|
||||
}
|
||||
|
||||
void Hive::Reset()
|
||||
void Hive::reset()
|
||||
{
|
||||
uint32_t v1 = 0;
|
||||
uint32_t v2 = 1;
|
||||
|
@ -63,4 +63,3 @@ void Hive::Reset()
|
|||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -26,28 +26,28 @@ namespace Sapphire:: Network
|
|||
virtual ~Hive();
|
||||
|
||||
// Returns the io_service of this object.
|
||||
asio::io_service& GetService();
|
||||
asio::io_service& getService();
|
||||
|
||||
// Returns true if the Stop function has been called.
|
||||
bool HasStopped();
|
||||
bool hasStopped();
|
||||
|
||||
// Polls the networking subsystem once from the current thread and
|
||||
// returns.
|
||||
void Poll();
|
||||
void poll();
|
||||
|
||||
// Runs the networking system on the current thread. This function blocks
|
||||
// until the networking system is stopped, so do not call on a single
|
||||
// threaded application with no other means of being able to call Stop
|
||||
// unless you code in such logic.
|
||||
void Run();
|
||||
void run();
|
||||
|
||||
// Stops the networking system. All work is finished and no more
|
||||
// networking interactions will be possible afterwards until Reset is called.
|
||||
void Stop();
|
||||
void stop();
|
||||
|
||||
// Restarts the networking system after Stop as been called. A new work
|
||||
// object is created ad the shutdown flag is cleared.
|
||||
void Reset();
|
||||
void reset();
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -38,21 +38,21 @@ Sapphire::Network::GameConnection::~GameConnection()
|
|||
|
||||
|
||||
// overwrite the parents onConnect for our game socket needs
|
||||
void Sapphire::Network::GameConnection::OnAccept( const std::string& host, uint16_t port )
|
||||
void Sapphire::Network::GameConnection::onAccept( const std::string& host, uint16_t port )
|
||||
{
|
||||
auto connection = make_GameConnection( m_hive, m_pAcceptor, m_pFw );
|
||||
m_pAcceptor->Accept( connection );
|
||||
m_pAcceptor->accept( connection );
|
||||
|
||||
Logger::info( "Connect from {0}", m_socket.remote_endpoint().address().to_string() );
|
||||
}
|
||||
|
||||
|
||||
void Sapphire::Network::GameConnection::OnDisconnect()
|
||||
void Sapphire::Network::GameConnection::onDisconnect()
|
||||
{
|
||||
Logger::debug( "DISCONNECT" );
|
||||
}
|
||||
|
||||
void Sapphire::Network::GameConnection::OnRecv( std::vector< uint8_t >& buffer )
|
||||
void Sapphire::Network::GameConnection::onRecv( std::vector< uint8_t >& buffer )
|
||||
{
|
||||
Packets::FFXIVARR_PACKET_HEADER packetHeader;
|
||||
const auto headerResult = Packets::getHeader( buffer, 0, packetHeader );
|
||||
|
@ -61,14 +61,14 @@ void Sapphire::Network::GameConnection::OnRecv( std::vector< uint8_t >& buffer )
|
|||
{
|
||||
Logger::info( "Dropping connection due to incomplete packet header." );
|
||||
Logger::info( "FIXME: Packet message bounary is not implemented." );
|
||||
Disconnect();
|
||||
disconnect();
|
||||
return;
|
||||
}
|
||||
|
||||
if( headerResult == Malformed )
|
||||
{
|
||||
Logger::info( "Dropping connection due to malformed packet header." );
|
||||
Disconnect();
|
||||
disconnect();
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -81,14 +81,14 @@ void Sapphire::Network::GameConnection::OnRecv( std::vector< uint8_t >& buffer )
|
|||
{
|
||||
Logger::info( "Dropping connection due to incomplete packets." );
|
||||
Logger::info( "FIXME: Packet message bounary is not implemented." );
|
||||
Disconnect();
|
||||
disconnect();
|
||||
return;
|
||||
}
|
||||
|
||||
if( packetResult == Malformed )
|
||||
{
|
||||
Logger::info( "Dropping connection due to malformed packets." );
|
||||
Disconnect();
|
||||
disconnect();
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -97,7 +97,7 @@ void Sapphire::Network::GameConnection::OnRecv( std::vector< uint8_t >& buffer )
|
|||
|
||||
}
|
||||
|
||||
void Sapphire::Network::GameConnection::OnError( const asio::error_code& error )
|
||||
void Sapphire::Network::GameConnection::onError( const asio::error_code& error )
|
||||
{
|
||||
Logger::info( "GameConnection closed: {0}", error.message() );
|
||||
}
|
||||
|
@ -432,7 +432,7 @@ void Sapphire::Network::GameConnection::sendPacket( Packets::LobbyPacketContaine
|
|||
uint8_t* dataPtr = pLpc.getRawData( false );
|
||||
std::vector< uint8_t > sendBuffer;
|
||||
sendBuffer.assign( dataPtr, dataPtr + size );
|
||||
Send( sendBuffer );
|
||||
send( sendBuffer );
|
||||
}
|
||||
|
||||
void Sapphire::Network::GameConnection::sendPackets( Packets::PacketContainer* pPacket )
|
||||
|
@ -440,7 +440,7 @@ void Sapphire::Network::GameConnection::sendPackets( Packets::PacketContainer* p
|
|||
std::vector< uint8_t > sendBuffer;
|
||||
|
||||
pPacket->fillSendBuffer( sendBuffer );
|
||||
Send( sendBuffer );
|
||||
send( sendBuffer );
|
||||
}
|
||||
|
||||
void Sapphire::Network::GameConnection::sendSinglePacket( FFXIVPacketBasePtr pPacket )
|
||||
|
|
|
@ -47,13 +47,13 @@ namespace Sapphire::Network
|
|||
void generateEncryptionKey( uint32_t key, const std::string& keyPhrase );
|
||||
|
||||
// overwrite the parents onConnect for our game socket needs
|
||||
void OnAccept( const std::string& host, uint16_t port ) override;
|
||||
void onAccept( const std::string& host, uint16_t port ) override;
|
||||
|
||||
void OnDisconnect() override;
|
||||
void onDisconnect() override;
|
||||
|
||||
void OnRecv( std::vector< uint8_t >& buffer ) override;
|
||||
void onRecv( std::vector< uint8_t >& buffer ) override;
|
||||
|
||||
void OnError( const asio::error_code& error ) override;
|
||||
void onError( const asio::error_code& error ) override;
|
||||
|
||||
void sendError( uint64_t sequence, uint32_t errorcode, uint16_t messageId, uint32_t tmpId );
|
||||
|
||||
|
|
|
@ -73,7 +73,7 @@ namespace Sapphire
|
|||
|
||||
std::vector< std::thread > threadGroup;
|
||||
|
||||
threadGroup.emplace_back( std::bind( &Network::Hive::Run, hive.get() ) );
|
||||
threadGroup.emplace_back( std::bind( &Network::Hive::run, hive.get() ) );
|
||||
|
||||
for( auto& thread : threadGroup )
|
||||
if( thread.joinable() )
|
||||
|
|
|
@ -138,21 +138,21 @@ Sapphire::Network::GameConnection::~GameConnection() = default;
|
|||
|
||||
|
||||
// overwrite the parents onConnect for our game socket needs
|
||||
void Sapphire::Network::GameConnection::OnAccept( const std::string& host, uint16_t port )
|
||||
void Sapphire::Network::GameConnection::onAccept( const std::string& host, uint16_t port )
|
||||
{
|
||||
GameConnectionPtr connection( new GameConnection( m_hive, m_pAcceptor, m_pFw ) );
|
||||
m_pAcceptor->Accept( connection );
|
||||
m_pAcceptor->accept( connection );
|
||||
Logger::info( "Connect from {0}", m_socket.remote_endpoint().address().to_string() );
|
||||
}
|
||||
|
||||
|
||||
void Sapphire::Network::GameConnection::OnDisconnect()
|
||||
void Sapphire::Network::GameConnection::onDisconnect()
|
||||
{
|
||||
Logger::debug( "GameConnection DISCONNECT" );
|
||||
m_pSession = nullptr;
|
||||
}
|
||||
|
||||
void Sapphire::Network::GameConnection::OnRecv( std::vector< uint8_t >& buffer )
|
||||
void Sapphire::Network::GameConnection::onRecv( std::vector< uint8_t >& buffer )
|
||||
{
|
||||
// This is assumed packet always start with valid FFXIVARR_PACKET_HEADER for now.
|
||||
Packets::FFXIVARR_PACKET_HEADER packetHeader{};
|
||||
|
@ -162,13 +162,13 @@ void Sapphire::Network::GameConnection::OnRecv( std::vector< uint8_t >& buffer )
|
|||
{
|
||||
Logger::info( "Dropping connection due to incomplete packet header." );
|
||||
Logger::info( "FIXME: Packet message bounary is not implemented." );
|
||||
Disconnect();
|
||||
disconnect();
|
||||
return;
|
||||
}
|
||||
else if( headerResult == Malformed )
|
||||
{
|
||||
Logger::info( "Dropping connection due to malformed packet header." );
|
||||
Disconnect();
|
||||
disconnect();
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -181,13 +181,13 @@ void Sapphire::Network::GameConnection::OnRecv( std::vector< uint8_t >& buffer )
|
|||
{
|
||||
Logger::info( "Dropping connection due to incomplete packets." );
|
||||
Logger::info( "FIXME: Packet message bounary is not implemented." );
|
||||
Disconnect();
|
||||
disconnect();
|
||||
return;
|
||||
}
|
||||
else if( packetResult == Malformed )
|
||||
{
|
||||
Logger::info( "Dropping connection due to malformed packets." );
|
||||
Disconnect();
|
||||
disconnect();
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -195,7 +195,7 @@ void Sapphire::Network::GameConnection::OnRecv( std::vector< uint8_t >& buffer )
|
|||
handlePackets( packetHeader, packetList );
|
||||
}
|
||||
|
||||
void Sapphire::Network::GameConnection::OnError( const asio::error_code& error )
|
||||
void Sapphire::Network::GameConnection::onError( const asio::error_code& error )
|
||||
{
|
||||
Logger::debug( "GameConnection ERROR: {0}", error.message() );
|
||||
}
|
||||
|
@ -280,7 +280,7 @@ void Sapphire::Network::GameConnection::sendPackets( Packets::PacketContainer* p
|
|||
std::vector< uint8_t > sendBuffer;
|
||||
|
||||
pPacket->fillSendBuffer( sendBuffer );
|
||||
Send( sendBuffer );
|
||||
send( sendBuffer );
|
||||
}
|
||||
|
||||
void Sapphire::Network::GameConnection::processInQueue()
|
||||
|
@ -410,7 +410,7 @@ void Sapphire::Network::GameConnection::handlePackets( const Sapphire::Network::
|
|||
// return;
|
||||
if( !pServerZone->createSession( playerId ) )
|
||||
{
|
||||
Disconnect();
|
||||
disconnect();
|
||||
return;
|
||||
}
|
||||
session = pServerZone->getSession( playerId );
|
||||
|
@ -419,7 +419,7 @@ void Sapphire::Network::GameConnection::handlePackets( const Sapphire::Network::
|
|||
else if( !session->isValid() || ( session->getPlayer() && session->getPlayer()->getLastPing() != 0 ) )
|
||||
{
|
||||
Logger::error( "[{0}] Session INVALID, disconnecting", id );
|
||||
Disconnect();
|
||||
disconnect();
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
@ -62,13 +62,13 @@ namespace Sapphire::Network
|
|||
~GameConnection();
|
||||
|
||||
// overwrite the parents onConnect for our game socket needs
|
||||
void OnAccept( const std::string& host, uint16_t port ) override;
|
||||
void onAccept( const std::string& host, uint16_t port ) override;
|
||||
|
||||
void OnDisconnect() override;
|
||||
void onDisconnect() override;
|
||||
|
||||
void OnRecv( std::vector< uint8_t >& buffer ) override;
|
||||
void onRecv( std::vector< uint8_t >& buffer ) override;
|
||||
|
||||
void OnError( const asio::error_code& error ) override;
|
||||
void onError( const asio::error_code& error ) override;
|
||||
|
||||
void handlePackets( const Packets::FFXIVARR_PACKET_HEADER& ipcHeader,
|
||||
const std::vector< Packets::FFXIVARR_PACKET_RAW >& packetData );
|
||||
|
|
|
@ -211,7 +211,7 @@ void Sapphire::World::ServerMgr::run( int32_t argc, char* argv[] )
|
|||
Network::addServerToHive< Network::GameConnection >( m_ip, m_port, hive, framework() );
|
||||
|
||||
std::vector< std::thread > thread_list;
|
||||
thread_list.emplace_back( std::thread( std::bind( &Network::Hive::Run, hive.get() ) ) );
|
||||
thread_list.emplace_back( std::thread( std::bind( &Network::Hive::run, hive.get() ) ) );
|
||||
|
||||
auto pDebugCom = std::make_shared< DebugCommandMgr >( framework() );
|
||||
auto pPlayerMgr = std::make_shared< Manager::PlayerMgr >( framework() );
|
||||
|
|
|
@ -68,10 +68,10 @@ bool Sapphire::World::Session::loadPlayer()
|
|||
void Sapphire::World::Session::close()
|
||||
{
|
||||
if( m_pZoneConnection )
|
||||
m_pZoneConnection->Disconnect();
|
||||
m_pZoneConnection->disconnect();
|
||||
|
||||
if( m_pChatConnection )
|
||||
m_pChatConnection->Disconnect();
|
||||
m_pChatConnection->disconnect();
|
||||
|
||||
// remove the session from the player
|
||||
if( m_pPlayer )
|
||||
|
|
Loading…
Add table
Reference in a new issue