Skip to content

Commit

Permalink
[#89] - network layer integration into the app. app can create networ…
Browse files Browse the repository at this point in the history
…k client.
  • Loading branch information
aconstlink committed May 27, 2024
1 parent 1c596fb commit 65ed597
Show file tree
Hide file tree
Showing 14 changed files with 203 additions and 10 deletions.
1 change: 1 addition & 0 deletions application/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ target_link_libraries( ${cur_lib_name}
PUBLIC motor::controls
PUBLIC motor::tool
PUBLIC motor::profiling
PUBLIC motor::network
)


Expand Down
81 changes: 81 additions & 0 deletions application/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ app::app( this_rref_t rhv ) noexcept

_dev_mouse = motor::move( rhv._dev_mouse ) ;
_dev_ascii = motor::move( rhv._dev_ascii ) ;
_networks = std::move( rhv._networks ) ;
}

//**************************************************************************************************************
Expand Down Expand Up @@ -559,4 +560,84 @@ void_t app::display_engine_stats( void_t ) noexcept
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
{
_carrier->network_system()->modules( [&]( motor::network::imodule_mtr_t mod )
{
_app_client_handler_wrapper * wrapper = motor::shared(
_app_client_handler_wrapper( this, motor::move(handler) ),
"[app::create_tcp_client] : network_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 } ) ;
return true ;
}
return false ;
} ) ;
}
30 changes: 28 additions & 2 deletions application/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
#include <motor/audio/frontend.hpp>
#include <motor/graphics/frontend/gen4/frontend.hpp>

#include <motor/network/ipv4_address.hpp>
#include <motor/network/imodule.h>

#include <motor/tool/imgui/imgui.h>
#include <motor/tool/imgui/engine_profiling.h>

Expand All @@ -37,6 +40,7 @@ namespace motor
public:

using window_id_t = size_t ;
using client_id_t = size_t ;

private:

Expand Down Expand Up @@ -86,6 +90,17 @@ namespace motor
bool_t _first_audio = true ;
bool_t _shutdown_called = false ;

private: // netork

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 ;

public: // profiling

motor::tool::engine_profiling_t _engine_profiling ;
Expand Down Expand Up @@ -171,6 +186,11 @@ namespace motor
};
motor_typedef( profile_data ) ;

struct network_data
{
};
motor_typedef( network_data ) ;

public:

virtual void_t on_init( void_t ) noexcept {} ;
Expand All @@ -187,18 +207,20 @@ namespace motor

virtual void_t on_profile( profile_data_in_t ) noexcept { }

virtual void_t on_network( network_data_in_t ) noexcept { }

public:

virtual void_t on_render( this_t::window_id_t const, motor::graphics::gen4::frontend_ptr_t,
motor::application::app::render_data_in_t ) noexcept {}

virtual bool_t on_tool( this_t::window_id_t const, motor::application::app::tool_data_ref_t ) noexcept { return false ; }

public: // window specific

virtual void_t on_event( window_id_t const,
motor::application::window_message_listener::state_vector_cref_t ) noexcept{}

public: // window stuff

window_id_t create_window( motor::application::window_info_cref_t ) noexcept ;

class window_view
Expand Down Expand Up @@ -244,6 +266,10 @@ namespace motor
bool_t has_closed( void_t ) noexcept { return _app->_closed ; }
};

protected:

void_t create_tcp_client( motor::string_in_t name, motor::network::ipv4::binding_point_host_in_t, motor::network::iclient_handler_mtr_rref_t ) noexcept ;

private:

bool_t carrier_init( motor::application::carrier_ptr_t ) noexcept ;
Expand Down
16 changes: 16 additions & 0 deletions application/carrier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ carrier::carrier( void_t ) noexcept
"[carrier] : device system" ) ;
_audio_system = motor::memory::global_t::alloc( motor::audio::system_t(),
"[carrier] : audio system" ) ;
_network_system = motor::memory::global_t::alloc( motor::network::system_t(),
"[carrier] : network system" ) ;
}

//******************************************************
Expand All @@ -28,6 +30,7 @@ carrier::carrier( this_rref_t rhv ) noexcept

_dev_system = motor::move( rhv._dev_system ) ;
_audio_system = motor::move( rhv._audio_system ) ;
_network_system = motor::move( rhv._network_system ) ;
}

//******************************************************
Expand All @@ -42,6 +45,7 @@ carrier::~carrier( void_t ) noexcept
motor::memory::global_t::dealloc( _sd ) ;
motor::memory::global_t::dealloc( _dev_system ) ;
motor::memory::global_t::dealloc( _audio_system ) ;
motor::memory::global_t::dealloc( _network_system ) ;

motor::memory::release_ptr( _app ) ;
}
Expand Down Expand Up @@ -135,6 +139,12 @@ motor::audio::system_ptr_t carrier::get_audio_system( void_t ) noexcept
return _audio_system ;
}

//******************************************************
motor::network::system_ptr_t carrier::get_network_system( void_t ) noexcept
{
return _network_system ;
}

//******************************************************
void_t carrier::update_device_system( void_t ) noexcept
{
Expand All @@ -151,4 +161,10 @@ motor::controls::system_mtr_t carrier::device_system( void_t ) noexcept
motor::audio::system_ptr_t carrier::audio_system( void_t ) noexcept
{
return _audio_system ;
}

//******************************************************
motor::network::system_ptr_t carrier::network_system( void_t ) noexcept
{
return _network_system ;
}
4 changes: 4 additions & 0 deletions application/carrier.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include <motor/controls/system.h>
#include <motor/audio/system.h>
#include <motor/network/system.h>

#include <thread>

Expand All @@ -37,6 +38,7 @@ namespace motor

motor::controls::system_ptr_t _dev_system = nullptr ;
motor::audio::system_ptr_t _audio_system = nullptr ;
motor::network::system_ptr_t _network_system = nullptr ;

public:

Expand All @@ -53,6 +55,7 @@ namespace motor
void_t update_device_system( void_t ) noexcept ;

motor::audio::system_ptr_t audio_system( void_t ) noexcept ;
motor::network::system_ptr_t network_system( void_t ) noexcept ;

public: // window creation interface

Expand All @@ -62,6 +65,7 @@ namespace motor

motor::controls::system_ptr_t get_dev_system( void_t ) noexcept ;
motor::audio::system_ptr_t get_audio_system( void_t ) noexcept ;
motor::network::system_ptr_t get_network_system( void_t ) noexcept ;

private:

Expand Down
6 changes: 6 additions & 0 deletions application/typedefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,11 @@ namespace motor
namespace application
{
using namespace motor::core::types ;

enum class network_client_type
{
udp,
tcp
};
}
}
28 changes: 27 additions & 1 deletion network/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,33 @@

using namespace motor::network ;

system::system( void_t ) noexcept
//*******************************************************************
system::system( void_t ) noexcept
{
}

//*******************************************************************
system::system( this_rref_t rhv ) noexcept
{
_mods = std::move( rhv._mods ) ;
}

//*******************************************************************
system::~system( void_t ) noexcept
{
}

//*******************************************************************
void_t system::add_module( motor::network::imodule_mtr_rref_t mod ) noexcept
{
_mods.emplace_back( motor::move( mod ) ) ;
}

//*******************************************************************
void_t system::modules( for_each_module_funk_t funk ) noexcept
{
for( auto * ptr : _mods )
{
if( funk( ptr ) ) break ;
}
}
13 changes: 13 additions & 0 deletions network/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,22 @@ namespace motor{ namespace network {

private:

motor::vector< motor::network::imodule_mtr_t > _mods ;

public:

system( void_t ) noexcept ;
system( this_rref_t ) noexcept ;
system( this_cref_t ) = delete ;
~system( void_t ) noexcept ;

public:

void_t add_module( motor::network::imodule_mtr_rref_t ) noexcept ;

using for_each_module_funk_t = std::function< bool_t ( motor::network::imodule_mtr_t ) > ;
void_t modules( for_each_module_funk_t ) noexcept ;


};
motor_typedef( system ) ;
Expand Down
9 changes: 9 additions & 0 deletions platform/application/win32/win32_carrier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#endif

#include "../../audio/oal/oal.h"
#include "../../network/network_module_creator.hpp"

#include <motor/concurrent/global.h>

Expand Down Expand Up @@ -47,6 +48,7 @@ win32_carrier::win32_carrier( void_t ) noexcept
{
this_t::create_and_register_device_modules() ;
this_t::create_and_register_audio_backend() ;
this_t::create_and_register_network_modules() ;

motor::log::global_t::status( "[win32::cpuv] : : " + motor::platform::cpu_id_t::vendor_string() ) ;
motor::log::global_t::status( "[win32::cpub]: " + motor::platform::cpu_id_t::brand_string() ) ;
Expand All @@ -72,6 +74,7 @@ win32_carrier::win32_carrier( motor::application::app_mtr_safe_t app ) noexcept
{
this_t::create_and_register_device_modules() ;
this_t::create_and_register_audio_backend() ;
this_t::create_and_register_network_modules() ;

motor::log::global_t::status( "[win32::cpuv] : : " + motor::platform::cpu_id_t::vendor_string() ) ;
motor::log::global_t::status( "[win32::cpub]: " + motor::platform::cpu_id_t::brand_string() ) ;
Expand Down Expand Up @@ -485,6 +488,12 @@ void_t win32_carrier::create_and_register_audio_backend( void_t ) noexcept
motor::audio::system::controller( this->get_audio_system() ).start( motor::move( bend ) ) ;
}

//***********************************************************************
void_t win32_carrier::create_and_register_network_modules( void_t ) noexcept
{
this_t::network_system()->add_module( motor::platform::network_module_creator::create() ) ;
}

//***********************************************************************
motor::application::iwindow_mtr_safe_t win32_carrier::create_window( motor::application::window_info_cref_t info ) noexcept
{
Expand Down
1 change: 1 addition & 0 deletions platform/application/win32/win32_carrier.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ namespace motor

void_t create_and_register_device_modules( void_t ) noexcept ;
void_t create_and_register_audio_backend( void_t ) noexcept ;
void_t create_and_register_network_modules( void_t ) noexcept ;


private: // window functions
Expand Down
6 changes: 6 additions & 0 deletions platform/application/xlib/xlib_carrier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -600,4 +600,10 @@ void_t xlib_carrier::create_and_register_device_modules( void_t ) noexcept
{
_device_module = motor::shared( motor::platform::xlib::xlib_module_t(), "[xlib] : device module" ) ;
this_t::get_dev_system()->add_module( motor::share( _device_module ) ) ;
}

//***********************************************************************
void_t xlib_carrier::create_and_register_network_modules( void_t ) noexcept
{
this_t::network_system()->add_module( motor::platform::network_module_creator::create() ) ;
}
Loading

0 comments on commit 65ed597

Please sign in to comment.