1
Fork 0
mirror of https://github.com/SapphireServer/Sapphire.git synced 2025-04-23 05:07:46 +00:00

Added asio as submodule, some initial network changes

This commit is contained in:
mordred 2018-10-24 15:17:40 +02:00
parent 990a656a72
commit a9ee2a7ebc
7 changed files with 52 additions and 33 deletions

3
.gitmodules vendored Normal file
View file

@ -0,0 +1,3 @@
[submodule "deps/asio"]
path = deps/asio
url = https://github.com/chriskohlhoff/asio.git

1
deps/asio vendored Submodule

@ -0,0 +1 @@
Subproject commit 28d9b8d6df708024af5227c551673fdb2519f5bf

View file

@ -46,5 +46,6 @@ target_include_directories( common
PUBLIC PUBLIC
"${CMAKE_CURRENT_SOURCE_DIR}/" "${CMAKE_CURRENT_SOURCE_DIR}/"
PRIVATE PRIVATE
"${CMAKE_CURRENT_SOURCE_DIR}/src/libraries/external/" ) "${CMAKE_CURRENT_SOURCE_DIR}/src/libraries/external/"
"${CMAKE_CURRENT_SOURCE_DIR}/../../deps/asio/include/")

View file

@ -8,8 +8,7 @@ namespace Network {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
Acceptor::Acceptor( HivePtr hive ) Acceptor::Acceptor( HivePtr hive ) :
:
m_hive( hive ), m_hive( hive ),
m_acceptor( hive->GetService() ), m_acceptor( hive->GetService() ),
m_io_strand( hive->GetService() ), m_io_strand( hive->GetService() ),
@ -27,13 +26,13 @@ bool Acceptor::OnAccept( ConnectionPtr connection, const std::string& host, uint
return true; return true;
} }
void Acceptor::OnError( const boost::system::error_code& error ) void Acceptor::OnError( const asio::error_code& error )
{ {
} }
void Acceptor::StartError( const boost::system::error_code& error ) void Acceptor::StartError( const asio::error_code& error )
{ {
uint32_t v1 = 1; uint32_t v1 = 1;
uint32_t v2 = 0; uint32_t v2 = 0;
@ -55,7 +54,7 @@ void Acceptor::DispatchAccept( ConnectionPtr connection )
connection ) ) ); connection ) ) );
} }
void Acceptor::HandleAccept( const boost::system::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() )
{ {
@ -95,14 +94,14 @@ void Acceptor::Listen( const std::string& host, const uint16_t& port )
{ {
try try
{ {
boost::asio::ip::tcp::resolver resolver( m_hive->GetService() ); asio::ip::tcp::resolver resolver( m_hive->GetService() );
boost::asio::ip::tcp::resolver::query query( host, std::to_string( port ) ); asio::ip::tcp::resolver::query query( host, std::to_string( port ) );
boost::asio::ip::tcp::endpoint endpoint = *resolver.resolve( query ); asio::ip::tcp::endpoint endpoint = *resolver.resolve( query );
m_acceptor.open( endpoint.protocol() ); m_acceptor.open( endpoint.protocol() );
m_acceptor.set_option( boost::asio::ip::tcp::acceptor::reuse_address( false ) ); m_acceptor.set_option( asio::ip::tcp::acceptor::reuse_address( false ) );
m_acceptor.bind( endpoint ); m_acceptor.bind( endpoint );
m_acceptor.listen( boost::asio::socket_base::max_connections ); m_acceptor.listen( asio::socket_base::max_connections );
} }
catch( ... ) catch( ... )
{ {
@ -117,7 +116,7 @@ HivePtr Acceptor::GetHive()
return m_hive; return m_hive;
} }
boost::asio::ip::tcp::acceptor& Acceptor::GetAcceptor() asio::ip::tcp::acceptor& Acceptor::GetAcceptor()
{ {
return m_acceptor; return m_acceptor;
} }

View file

@ -1,26 +1,43 @@
#ifndef ACCEPTOR_H_ #ifndef ACCEPTOR_H_
#define ACCEPTOR_H_ #define ACCEPTOR_H_
#include <boost/asio.hpp> #include <asio.hpp>
#include <boost/enable_shared_from_this.hpp> #include <boost/enable_shared_from_this.hpp>
#include <boost/shared_ptr.hpp> #include <boost/shared_ptr.hpp>
#include <atomic> #include <atomic>
#include "Forwards.h" #include "Forwards.h"
#include <system_error>
#include <asio/defer.hpp>
#include <asio/executor.hpp>
#include <asio/post.hpp>
#include <asio/strand.hpp>
#include <asio/system_executor.hpp>
#include <condition_variable>
#include <deque>
#include <memory>
#include <mutex>
#include <typeinfo>
#include <vector>
using asio::defer;
using asio::executor;
using asio::post;
using asio::strand;
using asio::system_executor;
namespace Core { namespace Core {
namespace Network { namespace Network {
class Connection; class Connection;
class Acceptor : class Acceptor : public boost::enable_shared_from_this< Acceptor >
public boost::enable_shared_from_this< Acceptor >
{ {
friend class Hive; friend class Hive;
private: private:
HivePtr m_hive; HivePtr m_hive;
boost::asio::ip::tcp::acceptor m_acceptor; asio::ip::tcp::acceptor m_acceptor;
boost::asio::strand m_io_strand; asio::strand m_io_strand;
std::atomic< uint32_t > m_error_state; std::atomic< uint32_t > m_error_state;
private: private:
@ -28,11 +45,11 @@ private:
Acceptor& operator=( const Acceptor& rhs ); Acceptor& operator=( const Acceptor& rhs );
void StartError( const boost::system::error_code& error ); void StartError( const asio::error_code& error );
void DispatchAccept( ConnectionPtr connection ); void DispatchAccept( ConnectionPtr connection );
void HandleAccept( const boost::system::error_code& error, ConnectionPtr connection ); void HandleAccept( const asio::error_code& error, ConnectionPtr connection );
private: private:
// Called when a connection has connected to the server. This function // Called when a connection has connected to the server. This function
@ -45,7 +62,7 @@ private:
// Called when an error is encountered. Most typically, this is when the // 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 // acceptor is being closed via the Stop function or if the Listen is
// called on an address that is not available. // called on an address that is not available.
virtual void OnError( const boost::system::error_code& error ); virtual void OnError( const asio::error_code& error );
public: public:
Acceptor( HivePtr hive ); Acceptor( HivePtr hive );
@ -56,10 +73,10 @@ public:
HivePtr GetHive(); HivePtr GetHive();
// Returns the acceptor object. // Returns the acceptor object.
boost::asio::ip::tcp::acceptor& GetAcceptor(); asio::ip::tcp::acceptor& GetAcceptor();
// Returns the strand object. // Returns the strand object.
boost::asio::strand& GetStrand(); asio::strand< executor >& GetStrand();
// Returns true if this object has an error associated with it. // Returns true if this object has an error associated with it.
bool HasError(); bool HasError();

View file

@ -1,6 +1,5 @@
#include <boost/bind.hpp> #include <boost/bind.hpp>
#include <boost/lexical_cast.hpp> #include <boost/lexical_cast.hpp>
#include <boost/interprocess/detail/atomic.hpp>
#include "Hive.h" #include "Hive.h"
namespace Core { namespace Core {
@ -8,9 +7,8 @@ namespace Network {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
Hive::Hive() Hive::Hive() :
: m_work_ptr( new asio::io_service::work( m_io_service ) ),
m_work_ptr( new boost::asio::io_service::work( m_io_service ) ),
m_shutdown( 0 ) m_shutdown( 0 )
{ {
} }
@ -19,7 +17,7 @@ Hive::~Hive()
{ {
} }
boost::asio::io_service& Hive::GetService() asio::io_service& Hive::GetService()
{ {
return m_io_service; return m_io_service;
} }
@ -60,7 +58,7 @@ void Hive::Reset()
if( m_shutdown.compare_exchange_strong( v1, v2 ) ) if( m_shutdown.compare_exchange_strong( v1, v2 ) )
{ {
m_io_service.reset(); m_io_service.reset();
m_work_ptr.reset( new boost::asio::io_service::work( m_io_service ) ); m_work_ptr.reset( new asio::io_service::work( m_io_service ) );
} }
} }

View file

@ -1,6 +1,7 @@
#ifndef HIVE_H_ #ifndef HIVE_H_
#define HIVE_H_ #define HIVE_H_
#include <asio.hpp>
#include <boost/asio.hpp> #include <boost/asio.hpp>
#include <boost/enable_shared_from_this.hpp> #include <boost/enable_shared_from_this.hpp>
@ -9,12 +10,11 @@
namespace Core { namespace Core {
namespace Network { namespace Network {
class Hive : class Hive : public boost::enable_shared_from_this< Hive >
public boost::enable_shared_from_this< Hive >
{ {
private: private:
boost::asio::io_service m_io_service; asio::io_service m_io_service;
boost::shared_ptr< boost::asio::io_service::work > m_work_ptr; boost::shared_ptr< asio::io_service::work > m_work_ptr;
std::atomic< uint32_t > m_shutdown; std::atomic< uint32_t > m_shutdown;
private: private:
@ -28,7 +28,7 @@ public:
virtual ~Hive(); virtual ~Hive();
// Returns the io_service of this object. // Returns the io_service of this object.
boost::asio::io_service& GetService(); asio::io_service& GetService();
// Returns true if the Stop function has been called. // Returns true if the Stop function has been called.
bool HasStopped(); bool HasStopped();