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 17, 2025
1 parent f0a8f27 commit fd6578c
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 16 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
16 changes: 16 additions & 0 deletions layer/layer_common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,20 @@ ServerRunner &GetServerRunner()
return runner;
}

Server::Server()
{
server_thread = std::thread(Dive::server_main);
}

void Server::StopServer()
{
Dive::StopServer();
if (server_thread.joinable())
{
server_thread.join();
}
}

Server server;

} // namespace DiveLayer
9 changes: 9 additions & 0 deletions layer/layer_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,13 @@ class ServerRunner

ServerRunner &GetServerRunner();

struct Server
{
std::thread server_thread;
Server();
void StopServer();
};

extern Server server;

} // namespace DiveLayer
8 changes: 1 addition & 7 deletions layer/openxr_layer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,6 @@ struct XrSessionData
{
XrSession session;
XrGeneratedDispatchTable dispatch_table;
ServerRunner &server;

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

static thread_local XrInstanceData *last_used_xr_instance_data = nullptr;
Expand Down Expand Up @@ -190,7 +184,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
10 changes: 4 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 Down Expand Up @@ -515,6 +509,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 fd6578c

Please sign in to comment.