diff --git a/examples/cpp/helloworld/greeter_async_client.cc b/examples/cpp/helloworld/greeter_async_client.cc index 055f82fe2d3..1d2dad052ed 100644 --- a/examples/cpp/helloworld/greeter_async_client.cc +++ b/examples/cpp/helloworld/greeter_async_client.cc @@ -42,6 +42,7 @@ #include #include #include + #include "helloworld.grpc.pb.h" using grpc::Channel; diff --git a/examples/cpp/helloworld/greeter_client.cc b/examples/cpp/helloworld/greeter_client.cc index dd0358ac95d..8d98c2bfd0d 100644 --- a/examples/cpp/helloworld/greeter_client.cc +++ b/examples/cpp/helloworld/greeter_client.cc @@ -40,6 +40,7 @@ #include #include #include + #include "helloworld.grpc.pb.h" using grpc::Channel; @@ -55,17 +56,28 @@ class GreeterClient { GreeterClient(std::shared_ptr channel) : stub_(Greeter::NewStub(channel)) {} + // Assambles the client's payload, sends it and presents the response back + // from the server. std::string SayHello(const std::string& user) { + // Data we are sending to the server. HelloRequest request; request.set_name(user); + + // Container for the data we expect from the server. HelloReply 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 actual RPC. Status status = stub_->SayHello(&context, request, &reply); + + // Act upon its status. if (status.ok()) { return reply.message(); } else { - return "Rpc failed"; + return "RPC failed"; } } @@ -74,6 +86,11 @@ class GreeterClient { }; int main(int argc, char** argv) { + // Instantiate the client. It requires a channel, out of which the actual RPCs + // are created. This channel models a connection to an endpoint (in this case, + // localhost at port 50051). We indicate that the channel isn't authenticated + // (use of InsecureCredentials()) and we don't pass any special channel + // arguments (that could enable extra channel features, such as compression). GreeterClient greeter( grpc::CreateChannel("localhost:50051", grpc::InsecureCredentials(), ChannelArguments())); diff --git a/examples/cpp/helloworld/greeter_server.cc b/examples/cpp/helloworld/greeter_server.cc index c1efdf563c4..e34b4f8a2d7 100644 --- a/examples/cpp/helloworld/greeter_server.cc +++ b/examples/cpp/helloworld/greeter_server.cc @@ -40,6 +40,7 @@ #include #include #include + #include "helloworld.grpc.pb.h" using grpc::Server; @@ -50,6 +51,7 @@ using helloworld::HelloRequest; using helloworld::HelloReply; using helloworld::Greeter; +// Logic and data behind the server's behavior. class GreeterServiceImpl final : public Greeter::Service { Status SayHello(ServerContext* context, const HelloRequest* request, HelloReply* reply) override { @@ -64,10 +66,17 @@ void RunServer() { GreeterServiceImpl service; ServerBuilder builder; + // Listen on the given address without any authentication mechanism. builder.AddListeningPort(server_address, grpc::InsecureServerCredentials()); + // Register "service" as the instance through which we'll communicate with + // clients. In this case it corresponds to an *synchronous* service. builder.RegisterService(&service); + // Finally assemble the server. std::unique_ptr server(builder.BuildAndStart()); std::cout << "Server listening on " << server_address << std::endl; + + // Wait for the server to shutdown. Note that some other thread must be + // responsible for shutting down the server for this call to ever return. server->Wait(); }