diff --git a/CMakeLists.txt b/CMakeLists.txt index 484acc7..8d45dcf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,7 +4,10 @@ # and implements IPluginInterface with the reusable VoidTeamspeak client. cmake_minimum_required(VERSION 4.0.0) -project(EchoionStandalone VERSION 1.0.7 LANGUAGES CXX) +if(NOT DEFINED ECHOION_PRODUCT_VERSION) + set(ECHOION_PRODUCT_VERSION 0.0.0) +endif() +project(EchoionStandalone VERSION "${ECHOION_PRODUCT_VERSION}" LANGUAGES CXX) set(CMAKE_CXX_STANDARD 26) set(CMAKE_CXX_STANDARD_REQUIRED ON) @@ -105,7 +108,7 @@ set_target_properties(EchoionStandalone PROPERTIES install(TARGETS EchoionStandalone RUNTIME DESTINATION bin) -if(BUILD_TESTS) +if(BUILD_TESTS AND TARGET EchoionCore) add_executable(echoion_core_loader_reload_tests tests/test_core_loader_reload.cpp) target_link_libraries(echoion_core_loader_reload_tests PRIVATE @@ -116,7 +119,9 @@ if(BUILD_TESTS) add_dependencies(echoion_core_loader_reload_tests EchoionCore) add_test(NAME echoion_core_loader_reload_tests COMMAND echoion_core_loader_reload_tests) +endif() +if(BUILD_TESTS) add_executable(echoion_standalone_message_tests tests/test_message_chunker.cpp src/MessageChunker.cpp diff --git a/include/echoion/standalone/UpdateService.hpp b/include/echoion/standalone/UpdateService.hpp index 0e36bab..2b6f800 100644 --- a/include/echoion/standalone/UpdateService.hpp +++ b/include/echoion/standalone/UpdateService.hpp @@ -97,4 +97,14 @@ namespace Echoion::Standalone class Impl; std::unique_ptr impl_; }; + + using UpdateControlHandler = std::function( + const std::string&, const std::string&)>; + + // Keep the service alive for the complete duration of an in-flight host + // control request. StandaloneClientInterface intentionally invokes a copied + // handler outside its mutex, so clearing the registered handler cannot + // cancel a callback which has already started. + [[nodiscard]] UpdateControlHandler + bindUpdateService(std::shared_ptr service); } // namespace Echoion::Standalone diff --git a/src/UpdateService.cpp b/src/UpdateService.cpp index 19d0ede..58f0a22 100644 --- a/src/UpdateService.cpp +++ b/src/UpdateService.cpp @@ -3162,6 +3162,15 @@ namespace Echoion::Standalone return impl_->handle(method, requestJson); } + UpdateControlHandler bindUpdateService(std::shared_ptr service) + { + return [service = std::move(service)]( + const std::string& method, + const std::string& requestJson) { + return service->handle(method, requestJson); + }; + } + Api::Result selectManagedCoreForStartup(const std::filesystem::path& stateDirectory, const std::filesystem::path& bundledCore, diff --git a/src/main.cpp b/src/main.cpp index b670e02..724c0ba 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -720,7 +720,7 @@ int main(int argc, char** argv) const auto updaterExecutable = runningExecutable->parent_path() / "echoion-updater"; - auto updateService = std::make_unique( + auto updateService = std::make_shared( Echoion::Standalone::UpdateService::Options{ .configPath = cfgPath, .stateDirectory = stateDirectory, @@ -784,10 +784,7 @@ int main(int argc, char** argv) updateService.reset(); } else { interface->setHostControlHandler( - [service = updateService.get()](const std::string& method, - const std::string& request) { - return service->handle(method, request); - }); + Echoion::Standalone::bindUpdateService(updateService)); } core->triggerAutoConnect(); diff --git a/tests/test_update_service.cpp b/tests/test_update_service.cpp index 67b2b39..567ddc9 100644 --- a/tests/test_update_service.cpp +++ b/tests/test_update_service.cpp @@ -250,6 +250,25 @@ int main() }; TemporaryTree tree; + { + auto ownedService = + std::make_shared( + Echoion::Standalone::UpdateService::Options{}, + Echoion::Standalone::UpdateService::Hooks{}); + std::weak_ptr observedService = + ownedService; + auto handler = + Echoion::Standalone::bindUpdateService(ownedService); + ownedService.reset(); + check(!observedService.expired(), + "host control handler did not retain UpdateService"); + check(handler("updates.getStatus", "{}").has_value(), + "retained UpdateService handler could not serve status"); + handler = {}; + check(observedService.expired(), + "cleared host control handler retained UpdateService"); + } + const auto startupState = tree.path() / "startup-state"; const auto bundledStartupCore = tree.path() / "startup-runtime" / "lib" / "Echoion.ts";