-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasync_c.cc
74 lines (58 loc) · 1.8 KB
/
async_c.cc
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
#include <grpc/support/log.h>
#include <grpcpp/grpcpp.h>
#include <iostream>
#include <memory>
#include <string>
#include <thread>
#include"testjbj.grpc.pb.h"
using grpc::Channel;
using grpc::ClientAsyncResponseReader;
using grpc::ClientContext;
using grpc::CompletionQueue;
using grpc::Status;
using T::jbj::session;
using T::jbj::jbjRegist_req;
using T::jbj::jbjReply_res;
class sessionClient
{
private:
std::unique_ptr<session::Stub> stub_;
public:
sessionClient(std::shared_ptr<Channel> channel)
:stub_(session::NewStub(channel))
{}
std::string Regist(const std::string user,const std::string passwd){
jbjRegist_req request;
request.set_user_name(user);
request.set_password(passwd);
jbjReply_res reply;
ClientContext context;
CompletionQueue cq;
Status status;
std::unique_ptr<ClientAsyncResponseReader<jbjReply_res>> rpc(
stub_->PrepareAsyncRegist(&context,request,&cq)
);
rpc->StartCall();
rpc->Finish(&reply,&status,(void*)1);
void *got_tag;
bool ok = false;
GPR_ASSERT(cq.Next(&got_tag, &ok));
GPR_ASSERT(got_tag == (void*)1);
GPR_ASSERT(ok);
if (status.ok()) {
// 获取服务端返回的数据
std::cout <<"user:"<< reply.user_name()<<" passwod:" << reply.password() <<" encode:"<<reply.result_code() <<std::endl;
return "RPC 成功";
} else {
return "RPC failed";
}
}
~sessionClient(){}
};
int main(){
sessionClient greeter(grpc::CreateChannel(
"localhost:50051", grpc::InsecureChannelCredentials()));
std::string reply = greeter.Regist("jbj","123456"); // The actual RPC call!
std::cout << "Greeter received: " << reply << std::endl;
return 0;
}