forked from ggerganov/whisper.cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RpcApi.cpp
175 lines (144 loc) · 3.15 KB
/
RpcApi.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include "RpcApi.h"
namespace media {
SDKServer::SDKServer(int port)
{
thread_ = new std::thread([&,port]() {
srv_.reset(new rpc::server(port));
std::function<void(float)> fuc = std::bind(&SDKServer::onProgress, this, std::placeholders::_1);
srv_->bind("progress", fuc);
std::function<int(int)> fuc2 = std::bind(&SDKServer::onState, this, std::placeholders::_1);
srv_->bind("state", fuc2);
fprintf(stderr, "start rpc srv!\n");
srv_->run();
});
}
SDKServer::~SDKServer()
{
stopServer();
delete thread_;
}
int SDKServer::stopServer()
{
srv_->stop();
thread_->join();
return 0;
}
void SDKServer::setCallback(std::function<void(int state, float progress)> cb)
{
cb_ = cb;
}
void SDKServer::onProgress(float v)
{
fprintf(stderr, "%s - %d %f\n", __FUNCTION__, __LINE__, v);
if (cb_)
{
cb_(Running, v);
}
}
int SDKServer::onState(int state)
{
fprintf(stderr, "%s - %d state:%d\n", __FUNCTION__, __LINE__, state);
if (cb_)
{
cb_(state, 100);
}
return 0;
}
SDKClient::SDKClient(int port)
{
client_.reset(new rpc::client("127.0.0.1", port));
port_ = port;
}
SDKClient::~SDKClient()
{}
void SDKClient::stop()
{
if (is_connect())
client_->call("stop");
}
bool SDKClient::is_connect()
{
auto state = client_->get_connection_state();
if (state != rpc::client::connection_state::connected)
client_.reset(new rpc::client("127.0.0.1", port_));
state = client_->get_connection_state();
if (state == rpc::client::connection_state::connected)
return true;
return false;
}
AppServer::AppServer(int port)
{
thread_ = new std::thread([&,port]() {
srv_.reset(new rpc::server(port));
std::function<void()> fuc2 = std::bind(&AppServer::onStop, this);
srv_->bind("stop", fuc2);
fprintf(stderr, "start rpc srv!\n");
srv_->run();
});
}
AppServer::~AppServer()
{
stopServer();
}
int AppServer::stopServer()
{
srv_->stop();
thread_->join();
return 0;
}
bool AppServer::isQuit()
{
return bQuit_;
}
void AppServer::onStop()
{
fprintf(stderr, "onStop\n");
bQuit_ = true;
}
AppClient::AppClient(int port)
{
client_.reset(new rpc::client("127.0.0.1", port));
port_ = port;
}
AppClient::~AppClient()
{}
void AppClient::progress(float v)
{
if (is_connect())
client_->call("progress", v);
}
int AppClient::state(int s)
{
if (is_connect())
client_->call("state", s);
return 0;
}
bool AppClient::is_connect()
{
auto state = client_->get_connection_state();
if (state != rpc::client::connection_state::connected)
client_.reset(new rpc::client("127.0.0.1", port_));
state = client_->get_connection_state();
if (state == rpc::client::connection_state::connected)
return true;
return false;
}
static AppServer *g_srv = nullptr;
void setGlobalAppServer(AppServer *srv)
{
g_srv = srv;
}
AppServer *getGlobalAppServer()
{
return g_srv;
}
static AppClient *g_cli = nullptr;
void setGlobalAppClient(AppClient *cli)
{
g_cli = cli;
}
AppClient *getGlobalAppClient()
{
return g_cli;
}
}