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 10, 2025
1 parent cdf84f0 commit 952b9b9
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 14 deletions.
5 changes: 3 additions & 2 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
{
int server_main();
}
void StopServer();
int server_main();
} // namespace Dive
19 changes: 18 additions & 1 deletion capture_service/service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,22 @@ grpc::Status DiveServiceImpl::DownloadFile(grpc::ServerContext *cont
return grpc::Status::OK;
}

std::unique_ptr<grpc::Server> &GetServer()
{
static std::unique_ptr<grpc::Server> server = nullptr;
return server;
}

void StopServer()
{
auto &server = GetServer();
if (server)
{
LOGI("StopServer at service.cc");
server->Shutdown();
}
}

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

builder.RegisterService(&service);
std::unique_ptr<grpc::Server> server(builder.BuildAndStart());
auto &server = GetServer();
server = builder.BuildAndStart();
LOGI("Server listening on %s", server_address.c_str());
server->Wait();
}
Expand Down
20 changes: 15 additions & 5 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,21 @@ struct XrSessionData
{
XrSession session;
XrGeneratedDispatchTable dispatch_table;
ServerRunner &server;
};

XrSessionData() :
server(GetServerRunner())
struct Server
{
std::thread server_thread;
Server() { server_thread = std::thread(Dive::server_main); }
void StopServer()
{
Dive::StopServer();
if (server_thread.joinable())
{
server_thread.join();
}
}
};
} 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 +200,7 @@ XRAPI_ATTR XrResult XRAPI_CALL ApiDiveLayerXrDestroyInstance(XrInstance instance

LOGD("ApiDiveLayerXrDestroyInstance\n");
XrResult result = XR_SUCCESS;

server.StopServer();
auto sess_data = GetXrInstanceLayerData(DataKey(instance));
if (sess_data)
{
Expand Down
24 changes: 18 additions & 6 deletions layer/vk_layer_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,6 @@ struct InstanceData
{
VkInstance instance;
InstanceDispatchTable dispatch_table;
ServerRunner &server;

InstanceData() :
server(GetServerRunner())
{
}
};

struct DeviceData
Expand All @@ -57,6 +51,20 @@ struct DeviceData
DeviceDispatchTable dispatch_table;
};

struct Server
{
std::thread server_thread;
Server() { server_thread = std::thread(Dive::server_main); }
void StopServer()
{
Dive::StopServer();
if (server_thread.joinable())
{
server_thread.join();
}
}
} server;

namespace
{
// Generally we expect to get the same device and instance, so we keep them
Expand Down Expand Up @@ -515,6 +523,10 @@ extern "C"
return (PFN_vkVoidFunction)&DiveInterceptEnumerateInstanceExtensionProperties;
if (0 == strcmp(func, "vkCreateInstance"))
return (PFN_vkVoidFunction)&DiveInterceptCreateInstance;
if (0 == strcmp(func, "vkDestroyInstance"))
{
LOGI("vkDestroyInstance");
}
if (inst == VK_NULL_HANDLE)
return NULL;

Expand Down

0 comments on commit 952b9b9

Please sign in to comment.