Skip to content

Commit

Permalink
[#89] - :tadta: network layer in app integrated and fixed some 🐛
Browse files Browse the repository at this point in the history
  • Loading branch information
aconstlink committed May 28, 2024
1 parent f41678f commit 8d04e1b
Show file tree
Hide file tree
Showing 7 changed files with 237 additions and 100 deletions.
188 changes: 117 additions & 71 deletions application/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,84 @@

using namespace motor::application ;

class motor::application::app::_app_client_handler_wrapper : public motor::network::iclient_handler
{
motor::network::iclient_handler_mtr_t _handler ;
motor::application::app_ptr_t _app ;

bool_t _shutdown = false ;

public:

_app_client_handler_wrapper( motor::application::app_ptr_t owner, motor::network::iclient_handler_ptr_t hnd ) noexcept :
_handler( hnd ), _app( owner ) {}

_app_client_handler_wrapper( _app_client_handler_wrapper && rhv ) noexcept :
_handler( motor::move( rhv._handler ) ), _app( motor::move( rhv._app ) ) {}

virtual ~_app_client_handler_wrapper( void_t ) noexcept
{
motor::memory::release_ptr( _handler ) ;
}

virtual motor::network::user_decision on_connect( motor::network::connect_result const res, size_t const tries ) noexcept
{
auto const ret_res = _handler->on_connect( res, tries ) ;
if ( ret_res == motor::network::user_decision::shutdown )
{
_app->remove( this ) ;
}
return ret_res ;
}

virtual motor::network::user_decision on_sync( void_t ) noexcept
{
auto const ret_res = _handler->on_sync() ;
if ( ret_res == motor::network::user_decision::shutdown )
{
_app->remove( this ) ;
}
return ret_res ;
}

virtual motor::network::user_decision on_update( void_t ) noexcept
{
auto const ret_res = _handler->on_update() ;
if ( ret_res == motor::network::user_decision::shutdown ||
_shutdown )
{
_app->remove( this ) ;
return motor::network::user_decision::shutdown ;
}
return ret_res ;
}

virtual void_t on_receive( byte_cptr_t buffer, size_t const sib ) noexcept
{
_handler->on_receive( buffer, sib ) ;
}

virtual void_t on_received( void_t ) noexcept
{
_handler->on_received() ;
}

virtual void_t on_send( byte_cptr_t & buffer, size_t & num_sib ) noexcept
{
_handler->on_send( buffer, num_sib ) ;
}

virtual void_t on_sent( motor::network::transmit_result const res ) noexcept
{
_handler->on_sent( res ) ;
}

void_t shutdown( void_t ) noexcept
{
_shutdown = true ;
}
};

//**************************************************************************************************************
app::app( void_t ) noexcept
{
Expand Down Expand Up @@ -329,25 +407,36 @@ bool_t app::carrier_shutdown( void_t ) noexcept
_shutdown_called = true ;
}

for( auto & d : _destruction_queue )
{
if( !this_t::clear_out_window_data( d ) )
for ( auto & d : _destruction_queue )
{
return false ;
if ( !this_t::clear_out_window_data( d ) )
{
return false ;
}
}
}

_destruction_queue.clear() ;
_destruction_queue.clear() ;

for( auto & d : _windows )
{
if( !this_t::clear_out_window_data( d ) )
for ( auto & d : _windows )
{
return false ;
if ( !this_t::clear_out_window_data( d ) )
{
return false ;
}
}

_windows.clear() ;
}

_windows.clear() ;
// clear out network clients
{
for( auto & ns : _networks )
{
ns.wrapper->shutdown() ;
motor::memory::release_ptr( motor::move( ns.wrapper ) ) ;
}
}

return true ;
}
Expand Down Expand Up @@ -562,65 +651,6 @@ void_t app::display_profiling_data( void_t ) noexcept
_engine_profiling.display() ;
}

class motor::application::app::_app_client_handler_wrapper : public motor::network::iclient_handler
{
motor::network::iclient_handler_mtr_t _handler ;
motor::application::app_ptr_t _app ;

public:

_app_client_handler_wrapper( motor::application::app_ptr_t owner, motor::network::iclient_handler_ptr_t hnd ) noexcept :
_handler( hnd ), _app( owner ) {}

_app_client_handler_wrapper( _app_client_handler_wrapper && rhv ) noexcept :
_handler( motor::move(rhv._handler) ), _app( motor::move(rhv._app) ) {}

~_app_client_handler_wrapper( void_t ) noexcept
{
motor::memory::release_ptr( _handler ) ;
}

virtual motor::network::user_decision on_connect( motor::network::connect_result const res, size_t const tries ) noexcept
{
if ( res == motor::network::connect_result::closed )
{
// need to remove from network list
//_app->remove...
}
return _handler->on_connect( res, tries ) ;
}

virtual motor::network::user_decision on_sync( void_t ) noexcept
{
return _handler->on_sync() ;
}

virtual motor::network::user_decision on_update( void_t ) noexcept
{
return _handler->on_update() ;
}

virtual void_t on_receive( byte_cptr_t buffer, size_t const sib ) noexcept
{
_handler->on_receive( buffer, sib ) ;
}

virtual void_t on_received( void_t ) noexcept
{
_handler->on_received() ;
}

virtual void_t on_send( byte_cptr_t & buffer, size_t & num_sib ) noexcept
{
_handler->on_send( buffer, num_sib ) ;
}

virtual void_t on_sent( motor::network::transmit_result const res ) noexcept
{
_handler->on_sent( res ) ;
}
};

//**********************************************************************
void_t app::create_tcp_client( motor::string_in_t name, motor::network::ipv4::binding_point_host_in_t bp,
motor::network::iclient_handler_mtr_rref_t handler ) noexcept
Expand All @@ -629,15 +659,31 @@ void_t app::create_tcp_client( motor::string_in_t name, motor::network::ipv4::bi
{
_app_client_handler_wrapper * wrapper = motor::shared(
_app_client_handler_wrapper( this, motor::move(handler) ),
"[app::create_tcp_client] : network_handler_wrapper" ) ;
"[app::create_tcp_client] : network_client_handler_wrapper" ) ;

auto const sid = mod->create_tcp_client( { name, bp, motor::share( wrapper ) } ) ;

if( sid != motor::network::socket_id_t( -1 ) )
{
_networks.emplace_back( this_t::network_store{ sid, motor::share(mod), wrapper } ) ;
std::lock_guard< std::mutex > lk( _mtx_networks ) ;
_networks.emplace_back( this_t::network_store{ sid, wrapper } ) ;
return true ;
}
return false ;
} ) ;
}

//**********************************************************************
void_t app::remove( _app_client_handler_wrapper * handler ) noexcept
{
std::lock_guard< std::mutex > lk( _mtx_networks ) ;

auto const iter = std::find_if( _networks.begin(), _networks.end(), [&]( this_t::network_store const & d )
{
return d.wrapper == handler ;
} ) ;

if( iter == _networks.end() ) return ;
motor::memory::release_ptr( iter->wrapper ) ;
_networks.erase( iter ) ;
}
6 changes: 5 additions & 1 deletion application/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,18 @@ namespace motor
private: // netork

class _app_client_handler_wrapper ;
friend class _app_client_handler_wrapper ;

struct network_store
{
motor::network::socket_id_t sid ;
motor::network::imodule_mtr_t mod ;
_app_client_handler_wrapper * wrapper ;
};
motor::vector< network_store > _networks ;

std::mutex _mtx_networks ;
void_t remove( _app_client_handler_wrapper * ) noexcept ;

public: // profiling

motor::tool::engine_profiling_t _engine_profiling ;
Expand Down
13 changes: 12 additions & 1 deletion network/imodule.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ namespace motor{ namespace network

virtual motor::network::transmit_result on_send(
motor::network::client_id_t const, byte_cptr_t & buffer, size_t & num_sib ) noexcept = 0 ;

virtual motor::network::server_decision on_update( motor::network::client_id_t const ) noexcept = 0 ;
};
motor_typedef( iserver_handler ) ;

Expand All @@ -33,8 +35,13 @@ namespace motor{ namespace network
};
motor_typedef( create_tcp_server_info ) ;

class iclient_handler
class MOTOR_NETWORK_API iclient_handler
{

public:

virtual ~iclient_handler( void_t ) noexcept {}

public:

virtual motor::network::user_decision on_connect( motor::network::connect_result const, size_t const tries ) noexcept = 0 ;
Expand All @@ -59,6 +66,10 @@ namespace motor{ namespace network

class MOTOR_NETWORK_API imodule
{
public:

virtual ~imodule( void_t ) noexcept {}

public:

virtual socket_id_t create_tcp_client( motor::network::create_tcp_client_info_rref_t ) noexcept = 0 ;
Expand Down
4 changes: 4 additions & 0 deletions network/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ system::system( this_rref_t rhv ) noexcept
//*******************************************************************
system::~system( void_t ) noexcept
{
for( auto * mod : _mods )
{
motor::memory::release_ptr( mod ) ;
}
}

//*******************************************************************
Expand Down
9 changes: 8 additions & 1 deletion network/typedefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,15 @@ namespace motor
{
initial,
established,
refused,
closed,
failed
};

static motor::string_t to_string( connect_result const res ) noexcept
{
static char const * const __connect_result_strings[] =
{ "initial", "established", "closed", "failed" } ;
{ "initial", "established", "refused", "closed", "failed" } ;

return __connect_result_strings[ size_t( res ) ] ;
}
Expand All @@ -61,6 +62,12 @@ namespace motor
shutdown
};

enum class server_decision
{
keep_going,
shutdown,
shutdown_client
};

static const size_t send_buffer_sib = 2048 ;

Expand Down
Loading

0 comments on commit 8d04e1b

Please sign in to comment.