-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGrpcPlugin.cpp
123 lines (93 loc) · 2.92 KB
/
GrpcPlugin.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include "GrpcPlugin.h"
#include "JavascriptApi.h"
#include "PluginJsHandler.h"
#include <filesystem>
/***
* Server
* Receiving messages from the browser
*/
class grpc_plugin_objImpl final : public grpc_plugin_obj::Service
{
grpc::Status com_grpc_js_api(grpc::ServerContext *context, const grpc_js_api_Request *request, grpc_js_api_Reply *response) override
{
PluginJsHandler::instance().pushApiRequest(request->funcname(), request->params());
return grpc::Status::OK;
}
};
/***
* Client
* Sending messages to the browser
*/
grpc_plugin_objClient::grpc_plugin_objClient(std::shared_ptr<grpc::Channel> channel) : stub_(grpc_proxy_obj::NewStub(channel))
{
m_connected = channel->WaitForConnected(std::chrono::system_clock::now() + std::chrono::seconds(3));
}
bool grpc_plugin_objClient::send_executeCallback(const int functionId, const std::string &jsonStr)
{
grpc_js_api_ExecuteCallback request;
request.set_funcid(functionId);
request.set_jsonstr(jsonStr.c_str());
grpc_js_api_Reply reply;
grpc::ClientContext context;
grpc::Status status = stub_->com_grpc_js_executeCallback(&context, request, &reply);
if (!status.ok())
return m_connected = false;
return true;
}
bool grpc_plugin_objClient::send_executeJavascript(const std::string& codeStr)
{
grpc_run_javascriptOnBrowser request;
request.set_str(codeStr.c_str());
grpc_empty_Reply reply;
grpc::ClientContext context;
grpc::Status status = stub_->com_grpc_run_javascriptOnBrowser(&context, request, &reply);
if (!status.ok())
return m_connected = false;
return true;
}
bool grpc_plugin_objClient::send_windowToggleVisibility()
{
grpc_window_toggleVisibility request;
grpc_empty_Reply reply;
grpc::ClientContext context;
grpc::Status status = stub_->com_grpc_window_toggleVisibility(&context, request, &reply);
if (!status.ok())
return m_connected = false;
return true;
}
// Grpc
//
GrpcPlugin::GrpcPlugin() {}
GrpcPlugin::~GrpcPlugin() {}
bool GrpcPlugin::startServer(int32_t listenPort)
{
// Don't repeat
if (m_listenPort != 0)
return false;
m_listenPort = listenPort;
grpc::EnableDefaultHealthCheckService(true);
grpc::reflection::InitProtoReflectionServerBuilderPlugin();
m_builder = std::make_unique<grpc::ServerBuilder>();
m_builder->AddListeningPort(std::string("localhost:") + std::to_string(m_listenPort), grpc::InsecureServerCredentials());
m_serverObj = std::make_unique<grpc_plugin_objImpl>();
m_builder->RegisterService(m_serverObj.get());
m_server = m_builder->BuildAndStart();
return m_server != nullptr;
}
bool GrpcPlugin::connectToClient(int32_t portNumber)
{
m_clientObj = std::make_unique<grpc_plugin_objClient>(grpc::CreateChannel("localhost:" + std::to_string(portNumber), grpc::InsecureChannelCredentials()));
return m_clientObj != nullptr;
}
void GrpcPlugin::stop()
{
if (m_server != nullptr)
{
m_server->Shutdown();
m_server->Wait();
m_server.reset();
}
m_clientObj = nullptr;
m_serverObj = nullptr;
m_builder = nullptr;
}