Hello folks,
I am trying a bidi async streaming service.
I am streaming a request asynchronously from the client and expecting a
stream of replies back from the server. Can you please let me know if the
way I am handling the client is right?
This is how my async streaming client in C++ looks like :
MuffinsResponse reply;
// 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;
// Storage for the status of the RPC upon completion.
Status status;
void* got_tag_read;
void* got_tag_write;
void* got_tag_finish;
bool ok = false;
// stub_->AsyncSendMuffins() performs the RPC call, returning an
instance we
// store in "rpc". Because we are using the asynchronous API, we need to
// hold on to the "rpc" instance in order to get updates on the ongoing
RPC.i
std::unique_ptr<grpc::ClientAsyncReaderWriter<Muffins::MuffinsMessage,
Muffins::MuffinsResponse>> stream(
stub_->AsyncSendMuffins(&context, &cq, got_tag_read));
// Request that, upon completion of the RPC, "reply" be updated with the
// server's response; "status" with the indication of whether the
operation
// was successful. Tag the request with the integer 1.
// rpc->Finish(&reply, &status, (void*)1);
stream->Write(request, got_tag_write);
std::string req = request.muffin_description();
std::cout << "The req sent is " << req << std::endl;
cq.Next(&got_tag_write, &ok);
if (ok && got_tag_write == (void*)1) {
printf("\n CLient sent the req");
stream->Read(&reply, got_tag_read);
}
cq.Next(&got_tag_read, &ok);
if (ok && got_tag_read == (void*)1) {
stream->Finish(&status, got_tag_finish);
}
cq.Next(&got_tag_finish, &ok);
GPR_ASSERT(ok);
if (status.ok()) {
return reply.Muffin_description();
} else {
return "RPC failed";
}
}
--
You received this message because you are subscribed to the Google Groups
"grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/grpc-io.
To view this discussion on the web visit
https://groups.google.com/d/msgid/grpc-io/33986028-159c-4d18-9cf8-7da3cea3dc16%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.