Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run gRPC service independently #158

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you help unify the code style here

} // 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;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of using extern, maybe it's better to using the tricky of GetServer() as you've implemented.


} // 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");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need this func? only for debug log?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you mean to stop the server in vkDestroyInstance, you will need to implement the interception functions, similar in the openxr layer.

}
if (inst == VK_NULL_HANDLE)
return NULL;

Expand Down