-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIPCClient.cpp
More file actions
92 lines (71 loc) · 2.35 KB
/
IPCClient.cpp
File metadata and controls
92 lines (71 loc) · 2.35 KB
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
#include "IPCClient.h"
IPCClient::IPCClient(const std::string ip, const std::string port) {
auto channel = grpc::CreateChannel(ip+":"+port, grpc::InsecureChannelCredentials());
_stub = make_unique<IPCSrv::Stub>(channel);
}
IPCClient::~IPCClient() {
}
auto IPCClient::call(const json & ctx) -> tuple<json, bool> {
debug("start call", ctx.dump());
IPCRequest request;
IPCReply reply;
ClientContext c_ctx;
request.set_body(ctx.dump());
Status status = _stub->call(&c_ctx, request, &reply);
if(status.ok()) {
json re = json::parse(reply.body());
int code = re.value("code", 0);
if(code == 200) {
return { re.value("reply", json()), true };
}else {
debug("接口请求失败!", reply.body());
}
}else {
debug("接口请求异常!", status.error_code(), status.error_message());
}
return { json(), false };
}
void IPCClient::streamCall(const json & ctx, function<void(const string &)> msgHandle) {
debug("start stream call");
ClientContext c_ctx;
IPCRequest request;
IPCReply reply;
request.set_body(ctx.dump());
auto reader = _stub->streamCall(&c_ctx, request);
while(reader->Read(&reply)) {
msgHandle(reply.body());
}
Status stauts = reader->Finish();
if(!stauts.ok()) {
debug("消息回调rpc流异常!");
}
debug("消息回调结束");
}
auto IPCClient::recordDownload(const json & ctx) -> optional<string> {
debug("start record download");
ClientContext c_ctx;
IPCRequest request;
FileReply reply;
request.set_body(ctx.dump());
auto reader = _stub->recordDownload(&c_ctx, request);
ofstream outfile;
const char* data;
string root_path = QCoreApplication::applicationDirPath().toStdString() + "/record";
string save_path = root_path + "/record.flv";
QDir dir(root_path.c_str());
if(!dir.exists()) {
dir.mkdir(root_path.c_str());
}
outfile.open(save_path.c_str(), ofstream::out | ofstream::trunc | ofstream::binary);
while(reader->Read(&reply)) {
data = reply.buffer().c_str();
outfile.write(data, reply.buffer().length());
}
outfile.close();
Status stauts = reader->Finish();
if(!stauts.ok()) {
debug("获取录像文件异常!");
}
debug("获取录像文件结束");
return save_path;
}