From cf7a861ea113525edb8c7b546aec949e4e4ce116 Mon Sep 17 00:00:00 2001 From: Elvis Capia Date: Wed, 8 Jan 2025 23:32:38 -0500 Subject: [PATCH] Run gRPC service independently --- capture_service/server.h | 1 + capture_service/service.cc | 13 ++++++++++++- layer/openxr_layer.cc | 20 +++++++++++++++----- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/capture_service/server.h b/capture_service/server.h index 8e983b64b..de2b01bb6 100644 --- a/capture_service/server.h +++ b/capture_service/server.h @@ -18,5 +18,6 @@ limitations under the License. namespace Dive { +void StopServer(); int server_main(); } diff --git a/capture_service/service.cc b/capture_service/service.cc index 3f3ab0b00..6ea730d0e 100644 --- a/capture_service/service.cc +++ b/capture_service/service.cc @@ -132,6 +132,8 @@ grpc::Status DiveServiceImpl::DownloadFile(grpc::ServerContext *cont return grpc::Status::OK; } +std::unique_ptr server; + void RunServer(uint16_t port) { std::string server_address = absl::StrFormat("0.0.0.0:%d", port); @@ -142,11 +144,20 @@ void RunServer(uint16_t port) builder.AddListeningPort(server_address, grpc::InsecureServerCredentials()); builder.RegisterService(&service); - std::unique_ptr server(builder.BuildAndStart()); + server = builder.BuildAndStart(); LOGI("Server listening on %s", server_address.c_str()); server->Wait(); } +void StopServer() +{ + LOGI("StopServer at service.cc"); + if (server) + { + server->Shutdown(); + } +} + int server_main() { RunServer(absl::GetFlag(FLAGS_port)); diff --git a/layer/openxr_layer.cc b/layer/openxr_layer.cc index 2ad7355d5..3066cd910 100644 --- a/layer/openxr_layer.cc +++ b/layer/openxr_layer.cc @@ -28,6 +28,8 @@ limitations under the License. #include "loader_interfaces.h" #include "xr_generated_dispatch_table.h" +#include "capture_service/server.h" + #if defined(__GNUC__) && __GNUC__ >= 4 # define LAYER_EXPORT __attribute__((visibility("default"))) #elif defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590) @@ -56,13 +58,21 @@ struct XrSessionData { XrSession session; XrGeneratedDispatchTable dispatch_table; - ServerRunner &server; +}; - XrSessionData() : - server(GetServerRunner()) +struct MyServer +{ + std::thread server_thread; + MyServer() { server_thread = std::thread(Dive::server_main); } + void StopServer() { + Dive::StopServer(); + if (server_thread.joinable()) + { + server_thread.join(); + } } -}; +} my_server; static thread_local XrInstanceData *last_used_xr_instance_data = nullptr; static thread_local XrSessionData *last_used_xr_session_data = nullptr; @@ -190,7 +200,7 @@ XRAPI_ATTR XrResult XRAPI_CALL ApiDiveLayerXrDestroyInstance(XrInstance instance LOGD("ApiDiveLayerXrDestroyInstance\n"); XrResult result = XR_SUCCESS; - + my_server.StopServer(); auto sess_data = GetXrInstanceLayerData(DataKey(instance)); if (sess_data) {