flatbuffers/grpc/samples/greeter/client.cpp

86 lines
2.5 KiB
C++
Raw Normal View History

[C++] Improve flatbuffers + gRPC integration (#4310) * Rework flatbuffers + gRPC integration - Introduce `flatbuffers::grpc::Message<T>`, a `grpc_slice`-backed message buffer that handles refcounting and allows flatbuffers to transfer ownership to gRPC efficiently. This replaces `flatbuffers::BufferRef<T>`, which required a copy call and was also unsafe w.r.t. buffer lifetime. - Introduce `flatbuffers::grpc::MessageBuilder`, a gRPC-specific builder that forces a `grpc_slice`-backed allocator and also adds some helpful `Message<T>`-related methods. - Update serializers accordingly (now zero-copy between flatbuffers and gRPC). * gRPC: verify messages by default, but allow user to override * gRPC: fix some formatting issues * Disable verification by default, but add helper method * Make FlatBufferBuilder fields protected + remove vec accessor * Use bool add_ref parameter to toggle refcount incr * Remove unnecessary inline specifiers * Fix formatting * Use auto * Remove empty lines * Use grpc_slice helper macros * Simplify reset code * Disable Message copy ctor and assignment by default * Remove unused member * Enable gRPC verification by default * Use auto * Bake in message verification (remove template specialization) * Add RoundUp func * Consolidate gRPC message copy flag * Make vector_downward allocations fully lazy * Test message verification failure code/message * Add grpctest verification test comments * Simplify reallocate implementation * Make initial_size a size_t * Use ternary op for growth_policy * Use truthiness rather than dont explicit nullptr check * Indent preprocessor directives * Remove grpc message copy/assignment * Fix a few bugs * Add gRPC example * Add basic gRPC docs * Use doxygen EXAMPLE_PATH + @include * Reference example fbs in grpc docs * Move gRPC examples into grpc/samples * Fix pointer/reference formatting * Use std::function rather than templated callback func * Create fresh message builder for each request * Use Clear() in Reset() impl * Use FLATBUFFERS_CONSTEXPR
2017-06-07 20:56:49 +00:00
#include "greeter.grpc.fb.h"
#include "greeter_generated.h"
#include <grpc++/grpc++.h>
#include <iostream>
#include <memory>
#include <string>
class GreeterClient {
public:
GreeterClient(std::shared_ptr<grpc::Channel> channel)
: stub_(Greeter::NewStub(channel)) {}
std::string SayHello(const std::string &name) {
flatbuffers::grpc::MessageBuilder mb;
auto name_offset = mb.CreateString(name);
auto request_offset = CreateHelloRequest(mb, name_offset);
mb.Finish(request_offset);
auto request_msg = mb.ReleaseMessage<HelloRequest>();
flatbuffers::grpc::Message<HelloReply> response_msg;
grpc::ClientContext context;
auto status = stub_->SayHello(&context, request_msg, &response_msg);
if (status.ok()) {
const HelloReply *response = response_msg.GetRoot();
return response->message()->str();
} else {
std::cerr << status.error_code() << ": " << status.error_message()
<< std::endl;
return "RPC failed";
}
}
void SayManyHellos(const std::string &name, int num_greetings,
std::function<void(const std::string &)> callback) {
flatbuffers::grpc::MessageBuilder mb;
auto name_offset = mb.CreateString(name);
auto request_offset =
CreateManyHellosRequest(mb, name_offset, num_greetings);
mb.Finish(request_offset);
auto request_msg = mb.ReleaseMessage<ManyHellosRequest>();
flatbuffers::grpc::Message<HelloReply> response_msg;
grpc::ClientContext context;
auto stream = stub_->SayManyHellos(&context, request_msg);
while (stream->Read(&response_msg)) {
const HelloReply *response = response_msg.GetRoot();
callback(response->message()->str());
}
auto status = stream->Finish();
if (!status.ok()) {
std::cerr << status.error_code() << ": " << status.error_message()
<< std::endl;
callback("RPC failed");
}
}
private:
std::unique_ptr<Greeter::Stub> stub_;
};
int main(int argc, char **argv) {
std::string server_address("localhost:50051");
auto channel =
grpc::CreateChannel(server_address, grpc::InsecureChannelCredentials());
GreeterClient greeter(channel);
std::string name("world");
std::string message = greeter.SayHello(name);
std::cerr << "Greeter received: " << message << std::endl;
int num_greetings = 10;
greeter.SayManyHellos(name, num_greetings, [](const std::string &message) {
std::cerr << "Greeter received: " << message << std::endl;
});
return 0;
}