Skip to content

Commit

Permalink
Run gRPC service independently
Browse files Browse the repository at this point in the history
  • Loading branch information
elviscapiaq committed Jan 9, 2025
1 parent cdf84f0 commit c3ac50b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
1 change: 1 addition & 0 deletions capture_service/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ limitations under the License.

namespace Dive
{
void StopServer();
int server_main();
}
11 changes: 10 additions & 1 deletion capture_service/service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ grpc::Status DiveServiceImpl::DownloadFile(grpc::ServerContext *cont
return grpc::Status::OK;
}

std::unique_ptr<grpc::Server> server;

void RunServer(uint16_t port)
{
std::string server_address = absl::StrFormat("0.0.0.0:%d", port);
Expand All @@ -142,11 +144,18 @@ void RunServer(uint16_t port)
builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());

builder.RegisterService(&service);
std::unique_ptr<grpc::Server> 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));
Expand Down
21 changes: 15 additions & 6 deletions layer/openxr_layer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -56,13 +58,20 @@ 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;
Expand Down Expand Up @@ -190,7 +199,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)
{
Expand Down

0 comments on commit c3ac50b

Please sign in to comment.