• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "greeter.grpc.fb.h"
2 #include "greeter_generated.h"
3 
4 #include <grpc++/grpc++.h>
5 
6 #include <iostream>
7 #include <memory>
8 #include <string>
9 
10 class GreeterServiceImpl final : public Greeter::Service {
SayHello(grpc::ServerContext * context,const flatbuffers::grpc::Message<HelloRequest> * request_msg,flatbuffers::grpc::Message<HelloReply> * response_msg)11   virtual grpc::Status SayHello(
12       grpc::ServerContext *context,
13       const flatbuffers::grpc::Message<HelloRequest> *request_msg,
14       flatbuffers::grpc::Message<HelloReply> *response_msg) override {
15     // flatbuffers::grpc::MessageBuilder mb_;
16     // We call GetRoot to "parse" the message. Verification is already
17     // performed by default. See the notes below for more details.
18     const HelloRequest *request = request_msg->GetRoot();
19 
20     // Fields are retrieved as usual with FlatBuffers
21     const std::string &name = request->name()->str();
22 
23     // `flatbuffers::grpc::MessageBuilder` is a `FlatBufferBuilder` with a
24     // special allocator for efficient gRPC buffer transfer, but otherwise
25     // usage is the same as usual.
26     auto msg_offset = mb_.CreateString("Hello, " + name);
27     auto hello_offset = CreateHelloReply(mb_, msg_offset);
28     mb_.Finish(hello_offset);
29 
30     // The `ReleaseMessage<T>()` function detaches the message from the
31     // builder, so we can transfer the resopnse to gRPC while simultaneously
32     // detaching that memory buffer from the builer.
33     *response_msg = mb_.ReleaseMessage<HelloReply>();
34     assert(response_msg->Verify());
35 
36     // Return an OK status.
37     return grpc::Status::OK;
38   }
39 
SayManyHellos(grpc::ServerContext * context,const flatbuffers::grpc::Message<ManyHellosRequest> * request_msg,grpc::ServerWriter<flatbuffers::grpc::Message<HelloReply>> * writer)40   virtual grpc::Status SayManyHellos(
41       grpc::ServerContext *context,
42       const flatbuffers::grpc::Message<ManyHellosRequest> *request_msg,
43       grpc::ServerWriter<flatbuffers::grpc::Message<HelloReply>> *writer)
44       override {
45     // The streaming usage below is simply a combination of standard gRPC
46     // streaming with the FlatBuffers usage shown above.
47     const ManyHellosRequest *request = request_msg->GetRoot();
48     const std::string &name = request->name()->str();
49     int num_greetings = request->num_greetings();
50 
51     for (int i = 0; i < num_greetings; i++) {
52       auto msg_offset = mb_.CreateString("Many hellos, " + name);
53       auto hello_offset = CreateHelloReply(mb_, msg_offset);
54       mb_.Finish(hello_offset);
55       writer->Write(mb_.ReleaseMessage<HelloReply>());
56     }
57 
58     return grpc::Status::OK;
59   }
60 
61   flatbuffers::grpc::MessageBuilder mb_;
62 };
63 
RunServer()64 void RunServer() {
65   std::string server_address("0.0.0.0:50051");
66   GreeterServiceImpl service;
67 
68   grpc::ServerBuilder builder;
69   builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
70   builder.RegisterService(&service);
71   std::unique_ptr<grpc::Server> server(builder.BuildAndStart());
72   std::cerr << "Server listening on " << server_address << std::endl;
73 
74   server->Wait();
75 }
76 
main(int argc,const char * argv[])77 int main(int argc, const char *argv[]) {
78   RunServer();
79   return 0;
80 }
81