-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsendcopy_client.h
77 lines (53 loc) · 1.85 KB
/
sendcopy_client.h
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
#include<iostream>
#include<memory>
#include<string>
#include<grpcpp/grpcpp.h>
#include<grpc/support/log.h>
#include<thread>
#include"alimama.grpc.pb.h"
using grpc::Channel;
using grpc::ClientAsyncResponseReader;
using grpc::ClientContext;
using grpc::CompletionQueue;
using grpc::Status;
using alimama::proto::NodeService;
using alimama::proto::SendCopyRequest;
using alimama::proto::SendCopyResponse;
using alimama::proto::BlockData;
using namespace std;
class SendCopyClient {
public:
explicit SendCopyClient(shared_ptr<Channel> channel): stub_(NodeService::NewStub(channel)){}
void SendCopy(const BlockData& blockdata) {
SendCopyRequest request;
// request赋值
request.mutable_block_data()->CopyFrom(blockdata);
AsyncClientCall* call = new AsyncClientCall;
call->response_reader = stub_->PrepareAsyncSendCopy(&call->context, request, &cq_);
call->response_reader->StartCall();
call->response_reader->Finish(&call->reply, &call->status, (void*)call);
}
void AsyncCompleteRpc() {
void* got_tag;
bool ok = false;
while (cq_.Next(&got_tag, &ok))
{
AsyncClientCall* call = static_cast<AsyncClientCall*>(got_tag);
GPR_ASSERT(ok);
if(call->status.ok())
cout << "Copy has been send!" << endl;
else
cout << "SendCopy failed!" << endl;
delete call;
}
}
private:
struct AsyncClientCall {
SendCopyResponse reply;
ClientContext context;
Status status;
unique_ptr<ClientAsyncResponseReader<SendCopyResponse>> response_reader;
};
unique_ptr<NodeService::Stub> stub_;
CompletionQueue cq_;
};