From 65ed597702e998a2d65c3ffc42c3f9d812332187 Mon Sep 17 00:00:00 2001 From: Alex Link Date: Mon, 27 May 2024 11:12:24 +0200 Subject: [PATCH] [#89] - network layer integration into the app. app can create network client. --- application/CMakeLists.txt | 1 + application/app.cpp | 81 ++++++++++++++++++++ application/app.h | 30 +++++++- application/carrier.cpp | 16 ++++ application/carrier.h | 4 + application/typedefs.h | 6 ++ network/system.cpp | 28 ++++++- network/system.h | 13 ++++ platform/application/win32/win32_carrier.cpp | 9 +++ platform/application/win32/win32_carrier.h | 1 + platform/application/xlib/xlib_carrier.cpp | 6 ++ platform/application/xlib/xlib_carrier.h | 1 + platform/network/network_module_creator.hpp | 15 ++-- platform/network/win32/win32_net_module.cpp | 2 +- 14 files changed, 203 insertions(+), 10 deletions(-) diff --git a/application/CMakeLists.txt b/application/CMakeLists.txt index b8a937f..87b9179 100644 --- a/application/CMakeLists.txt +++ b/application/CMakeLists.txt @@ -40,6 +40,7 @@ target_link_libraries( ${cur_lib_name} PUBLIC motor::controls PUBLIC motor::tool PUBLIC motor::profiling + PUBLIC motor::network ) diff --git a/application/app.cpp b/application/app.cpp index 1c98e54..24a823d 100644 --- a/application/app.cpp +++ b/application/app.cpp @@ -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 ) ; } //************************************************************************************************************** @@ -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 ; + } ) ; } \ No newline at end of file diff --git a/application/app.h b/application/app.h index 28a743e..6a0e008 100644 --- a/application/app.h +++ b/application/app.h @@ -17,6 +17,9 @@ #include #include +#include +#include + #include #include @@ -37,6 +40,7 @@ namespace motor public: using window_id_t = size_t ; + using client_id_t = size_t ; private: @@ -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 ; @@ -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 {} ; @@ -187,6 +207,8 @@ 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, @@ -194,11 +216,11 @@ namespace motor 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 @@ -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 ; diff --git a/application/carrier.cpp b/application/carrier.cpp index 62785d9..e2d32ff 100644 --- a/application/carrier.cpp +++ b/application/carrier.cpp @@ -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" ) ; } //****************************************************** @@ -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 ) ; } //****************************************************** @@ -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 ) ; } @@ -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 { @@ -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 ; } \ No newline at end of file diff --git a/application/carrier.h b/application/carrier.h index 0c9124b..7fafb43 100644 --- a/application/carrier.h +++ b/application/carrier.h @@ -11,6 +11,7 @@ #include #include +#include #include @@ -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: @@ -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 @@ -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: diff --git a/application/typedefs.h b/application/typedefs.h index aa35acb..4bc32a7 100644 --- a/application/typedefs.h +++ b/application/typedefs.h @@ -11,5 +11,11 @@ namespace motor namespace application { using namespace motor::core::types ; + + enum class network_client_type + { + udp, + tcp + }; } } \ No newline at end of file diff --git a/network/system.cpp b/network/system.cpp index f59467c..450dffa 100644 --- a/network/system.cpp +++ b/network/system.cpp @@ -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 ; + } } \ No newline at end of file diff --git a/network/system.h b/network/system.h index e5f1b9c..fb91763 100644 --- a/network/system.h +++ b/network/system.h @@ -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 ) ; diff --git a/platform/application/win32/win32_carrier.cpp b/platform/application/win32/win32_carrier.cpp index 2bf69ae..dbadaae 100644 --- a/platform/application/win32/win32_carrier.cpp +++ b/platform/application/win32/win32_carrier.cpp @@ -15,6 +15,7 @@ #endif #include "../../audio/oal/oal.h" +#include "../../network/network_module_creator.hpp" #include @@ -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() ) ; @@ -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() ) ; @@ -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 { diff --git a/platform/application/win32/win32_carrier.h b/platform/application/win32/win32_carrier.h index 31b7175..52da76e 100644 --- a/platform/application/win32/win32_carrier.h +++ b/platform/application/win32/win32_carrier.h @@ -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 diff --git a/platform/application/xlib/xlib_carrier.cpp b/platform/application/xlib/xlib_carrier.cpp index 8a94c1d..307fddc 100644 --- a/platform/application/xlib/xlib_carrier.cpp +++ b/platform/application/xlib/xlib_carrier.cpp @@ -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() ) ; } \ No newline at end of file diff --git a/platform/application/xlib/xlib_carrier.h b/platform/application/xlib/xlib_carrier.h index 0357677..b6a06f1 100644 --- a/platform/application/xlib/xlib_carrier.h +++ b/platform/application/xlib/xlib_carrier.h @@ -119,6 +119,7 @@ namespace motor bool_t find_window_info( Window hwnd, find_window_info_funk_t ) noexcept ; void_t create_and_register_device_modules( void_t ) noexcept ; + void_t create_and_register_network_modules( void_t ) noexcept ; }; motor_typedef( xlib_carrier ) ; } diff --git a/platform/network/network_module_creator.hpp b/platform/network/network_module_creator.hpp index 0fcc09f..05ec420 100644 --- a/platform/network/network_module_creator.hpp +++ b/platform/network/network_module_creator.hpp @@ -12,17 +12,20 @@ namespace motor { namespace platform { struct network_module_creator { - static motor::network::imodule_ptr_t create( void_t ) noexcept + static motor::network::imodule_mtr_safe_t create( void_t ) noexcept { #if defined (MOTOR_TARGET_OS_WIN) - return motor::memory::global_t::alloc( motor::platform::win32::win32_net_module_t(), + + return motor::shared( motor::platform::win32::win32_net_module_t(), "[network_module_creator] : win32 network module" ) ; + + #elif defined( MOTOR_TARGET_OS_LIN) - return motor::memory::global_t::alloc( motor::platform::unx::unix_net_module_t(), - "[network_module_creator] : win32 network module" ) ; + + return motor::shared( motor::platform::unx::unix_net_module_t(), + "[network_module_creator] : unix network module" ) ; + #endif } - }; - } } \ No newline at end of file diff --git a/platform/network/win32/win32_net_module.cpp b/platform/network/win32/win32_net_module.cpp index d67f3f6..3172540 100644 --- a/platform/network/win32/win32_net_module.cpp +++ b/platform/network/win32/win32_net_module.cpp @@ -132,7 +132,7 @@ motor::network::socket_id_t win32_net_module::create_tcp_server( tcpd->t = std::thread( [=] ( void_t ) { motor::string_t const log_start = "[TCP server " + info_in.name + "] : " ; - motor::log::global::status( log_start + "online and accepting incoming connections." ) ; + motor::log::global::status( log_start + "online and accepting incoming connections at " + motor::to_string(info_in.bp.port) ) ; tcpd->running = true ;