forked from grpc/grpc
/
greeter_async_bidi_client.cc
executable file
·213 lines (189 loc) · 7.84 KB
/
greeter_async_bidi_client.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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
*
* Copyright 2016, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <iostream>
#include <memory>
#include <string>
#include <thread>
#include <grpc++/grpc++.h>
#include "hellostreamingworld.grpc.pb.h"
using grpc::Channel;
using grpc::ClientAsyncReaderWriter;
using grpc::ClientContext;
using grpc::CompletionQueue;
using grpc::Status;
using hellostreamingworld::HelloRequest;
using hellostreamingworld::HelloReply;
using hellostreamingworld::MultiGreeter;
// NOTE: This is a complex example for an asynchronous, bidirectional streaming
// client. For a simpler example, start with the
// greeter_client/greeter_async_client first.
class AsyncBidiGreeterClient {
enum class Type {
READ = 1,
WRITE = 2,
CONNECT = 3,
WRITES_DONE = 4,
FINISH = 5
};
public:
explicit AsyncBidiGreeterClient(std::shared_ptr<Channel> channel)
: stub_(MultiGreeter::NewStub(channel)) {
grpc_thread_.reset(
new std::thread(std::bind(&AsyncBidiGreeterClient::GrpcThread, this)));
stream_ = stub_->AsyncSayHello(&context_, &cq_,
reinterpret_cast<void*>(Type::CONNECT));
}
// Similar to the async hello example in greeter_async_client but does not
// wait for the response. Instead queues up a tag in the completion queue
// that is notified when the server responds back (or when the stream is
// closed). Returns false when the stream is requested to be closed.
bool AsyncSayHello(const std::string& user) {
if (user == "quit") {
stream_->WritesDone(reinterpret_cast<void*>(Type::WRITES_DONE));
return false;
}
// Data we are sending to the server.
HelloRequest request;
request.set_name(user);
// This is important: You can have at most one write or at most one read
// at any given time. The throttling is performed by gRPC completion
// queue. If you queue more than one write/read, the stream will crash.
// Because this stream is bidirectional, you *can* have a single read
// and a single write request queued for the same stream. Writes and reads
// are independent of each other in terms of ordering/delivery.
std::cout << " ** Sending request: " << user << std::endl;
stream_->Write(request, reinterpret_cast<void*>(Type::WRITE));
return true;
}
~AsyncBidiGreeterClient() {
std::cout << "Shutting down client...." << std::endl;
grpc::Status status;
cq_.Shutdown();
grpc_thread_->join();
}
private:
void AsyncHelloRequestNextMessage() {
std::cout << " ** Got response: " << response_.message() << std::endl;
// The tag is the link between our thread (main thread) and the completion
// queue thread. The tag allows the completion queue to fan off
// notification handlers for the specified read/write requests as they
// are being processed by gRPC.
stream_->Read(&response_, reinterpret_cast<void*>(Type::READ));
}
// Runs a gRPC completion-queue processing thread. Checks for 'Next' tag
// and processes them until there are no more (or when the completion queue
// is shutdown).
void GrpcThread() {
while (true) {
void* got_tag;
bool ok = false;
// Block until the next result is available in the completion queue "cq".
// The return value of Next should always be checked. This return value
// tells us whether there is any kind of event or the cq_ is shutting
// down.
if (!cq_.Next(&got_tag, &ok)) {
std::cerr << "Client stream closed. Quitting" << std::endl;
break;
}
// It's important to process all tags even if the ok is false. One might
// want to deallocate memory that has be reinterpret_cast'ed to void*
// when the tag got initialized. For our example, we cast an int to a
// void*, so we don't have extra memory management to take care of.
if (ok) {
std::cout << std::endl
<< "**** Processing completion queue tag " << got_tag
<< std::endl;
switch (static_cast<Type>(reinterpret_cast<long>(got_tag))) {
case Type::READ:
std::cout << "Read a new message." << std::endl;
break;
case Type::WRITE:
std::cout << "Sending message (async)." << std::endl;
AsyncHelloRequestNextMessage();
break;
case Type::CONNECT:
std::cout << "Server connected." << std::endl;
break;
case Type::WRITES_DONE:
std::cout << "Server disconnecting." << std::endl;
break;
case Type::FINISH:
std::cout << "Client finish; status = "
<< (finish_status_.ok() ? "ok" : "cancelled")
<< std::endl;
context_.TryCancel();
cq_.Shutdown();
break;
default:
std::cerr << "Unexpected tag " << got_tag << std::endl;
GPR_ASSERT(false);
}
}
}
}
// Context for the client. It could be used to convey extra information to
// the server and/or tweak certain RPC behaviors.
ClientContext context_;
// The producer-consumer queue we use to communicate asynchronously with the
// gRPC runtime.
CompletionQueue cq_;
// Out of the passed in Channel comes the stub, stored here, our view of the
// server's exposed services.
std::unique_ptr<MultiGreeter::Stub> stub_;
// The bidirectional, asynchronous stream for sending/receiving messages.
std::unique_ptr<ClientAsyncReaderWriter<HelloRequest, HelloReply>> stream_;
// Allocated protobuf that holds the response. In real clients and servers,
// the memory management would a bit more complex as the thread that fills
// in the response should take care of concurrency as well as memory
// management.
HelloReply response_;
// Thread that notifies the gRPC completion queue tags.
std::unique_ptr<std::thread> grpc_thread_;
// Finish status when the client is done with the stream.
grpc::Status finish_status_ = grpc::Status::OK;
};
int main(int argc, char** argv) {
AsyncBidiGreeterClient greeter(grpc::CreateChannel(
"localhost:50051", grpc::InsecureChannelCredentials()));
std::string text;
while (true) {
std::cout << "Enter text (type quit to end): ";
std::cin >> text;
// Async RPC call that sends a message and awaits a response.
if (!greeter.AsyncSayHello(text)) {
std::cout << "Quitting." << std::endl;
break;
}
}
return 0;
}