Skip to content
Snippets Groups Projects
Commit 31e01d72 authored by LisaFC's avatar LisaFC
Browse files

Merge pull request #4043 from jeady/patch-1

Use code type highlight for the code blocks in the cpp helloworld
parents 847be493 2e9ce91e
No related branches found
No related tags found
No related merge requests found
...@@ -41,7 +41,7 @@ message from the remote client containing the user's name, then send back ...@@ -41,7 +41,7 @@ message from the remote client containing the user's name, then send back
a greeting in a single `HelloReply`. This is the simplest type of RPC you a greeting in a single `HelloReply`. This is the simplest type of RPC you
can specify in gRPC - we'll look at some other types later in this document. can specify in gRPC - we'll look at some other types later in this document.
``` ```protobuf
syntax = "proto3"; syntax = "proto3";
option java_package = "ex.grpc"; option java_package = "ex.grpc";
...@@ -93,20 +93,20 @@ $ protoc -I ../../protos/ --cpp_out=. ../../protos/helloworld.proto ...@@ -93,20 +93,20 @@ $ protoc -I ../../protos/ --cpp_out=. ../../protos/helloworld.proto
channel can be created with the target address, credentials to use and channel can be created with the target address, credentials to use and
arguments as follows arguments as follows
``` ```cpp
auto channel = CreateChannel("localhost:50051", InsecureChannelCredentials()); auto channel = CreateChannel("localhost:50051", InsecureChannelCredentials());
``` ```
- Create a stub. A stub implements the rpc methods of a service and in the - Create a stub. A stub implements the rpc methods of a service and in the
generated code, a method is provided to created a stub with a channel: generated code, a method is provided to created a stub with a channel:
``` ```cpp
auto stub = helloworld::Greeter::NewStub(channel); auto stub = helloworld::Greeter::NewStub(channel);
``` ```
- Make a unary rpc, with `ClientContext` and request/response proto messages. - Make a unary rpc, with `ClientContext` and request/response proto messages.
``` ```cpp
ClientContext context; ClientContext context;
HelloRequest request; HelloRequest request;
request.set_name("hello"); request.set_name("hello");
...@@ -116,7 +116,7 @@ $ protoc -I ../../protos/ --cpp_out=. ../../protos/helloworld.proto ...@@ -116,7 +116,7 @@ $ protoc -I ../../protos/ --cpp_out=. ../../protos/helloworld.proto
- Check returned status and response. - Check returned status and response.
``` ```cpp
if (status.ok()) { if (status.ok()) {
// check reply.message() // check reply.message()
} else { } else {
...@@ -130,7 +130,7 @@ For a working example, refer to [greeter_client.cc](greeter_client.cc). ...@@ -130,7 +130,7 @@ For a working example, refer to [greeter_client.cc](greeter_client.cc).
- Implement the service interface - Implement the service interface
``` ```cpp
class GreeterServiceImpl final : public Greeter::Service { class GreeterServiceImpl final : public Greeter::Service {
Status SayHello(ServerContext* context, const HelloRequest* request, Status SayHello(ServerContext* context, const HelloRequest* request,
HelloReply* reply) override { HelloReply* reply) override {
...@@ -144,7 +144,7 @@ For a working example, refer to [greeter_client.cc](greeter_client.cc). ...@@ -144,7 +144,7 @@ For a working example, refer to [greeter_client.cc](greeter_client.cc).
- Build a server exporting the service - Build a server exporting the service
``` ```cpp
GreeterServiceImpl service; GreeterServiceImpl service;
ServerBuilder builder; ServerBuilder builder;
builder.AddListeningPort("0.0.0.0:50051", grpc::InsecureServerCredentials()); builder.AddListeningPort("0.0.0.0:50051", grpc::InsecureServerCredentials());
...@@ -170,14 +170,14 @@ The channel and stub creation code is the same as the sync client. ...@@ -170,14 +170,14 @@ The channel and stub creation code is the same as the sync client.
- Initiate the rpc and create a handle for the rpc. Bind the rpc to a - Initiate the rpc and create a handle for the rpc. Bind the rpc to a
`CompletionQueue`. `CompletionQueue`.
``` ```cpp
CompletionQueue cq; CompletionQueue cq;
auto rpc = stub->AsyncSayHello(&context, request, &cq); auto rpc = stub->AsyncSayHello(&context, request, &cq);
``` ```
- Ask for reply and final status, with a unique tag - Ask for reply and final status, with a unique tag
``` ```cpp
Status status; Status status;
rpc->Finish(&reply, &status, (void*)1); rpc->Finish(&reply, &status, (void*)1);
``` ```
...@@ -185,7 +185,7 @@ The channel and stub creation code is the same as the sync client. ...@@ -185,7 +185,7 @@ The channel and stub creation code is the same as the sync client.
- Wait for the completion queue to return the next tag. The reply and status are - Wait for the completion queue to return the next tag. The reply and status are
ready once the tag passed into the corresponding `Finish()` call is returned. ready once the tag passed into the corresponding `Finish()` call is returned.
``` ```cpp
void* got_tag; void* got_tag;
bool ok = false; bool ok = false;
cq.Next(&got_tag, &ok); cq.Next(&got_tag, &ok);
...@@ -203,7 +203,7 @@ completion queue to return the tag. The basic flow is ...@@ -203,7 +203,7 @@ completion queue to return the tag. The basic flow is
- Build a server exporting the async service - Build a server exporting the async service
``` ```cpp
helloworld::Greeter::AsyncService service; helloworld::Greeter::AsyncService service;
ServerBuilder builder; ServerBuilder builder;
builder.AddListeningPort("0.0.0.0:50051", InsecureServerCredentials()); builder.AddListeningPort("0.0.0.0:50051", InsecureServerCredentials());
...@@ -214,7 +214,7 @@ completion queue to return the tag. The basic flow is ...@@ -214,7 +214,7 @@ completion queue to return the tag. The basic flow is
- Request one rpc - Request one rpc
``` ```cpp
ServerContext context; ServerContext context;
HelloRequest request; HelloRequest request;
ServerAsyncResponseWriter<HelloReply> responder; ServerAsyncResponseWriter<HelloReply> responder;
...@@ -224,7 +224,7 @@ completion queue to return the tag. The basic flow is ...@@ -224,7 +224,7 @@ completion queue to return the tag. The basic flow is
- Wait for the completion queue to return the tag. The context, request and - Wait for the completion queue to return the tag. The context, request and
responder are ready once the tag is retrieved. responder are ready once the tag is retrieved.
``` ```cpp
HelloReply reply; HelloReply reply;
Status status; Status status;
void* got_tag; void* got_tag;
...@@ -239,7 +239,7 @@ completion queue to return the tag. The basic flow is ...@@ -239,7 +239,7 @@ completion queue to return the tag. The basic flow is
- Wait for the completion queue to return the tag. The rpc is finished when the - Wait for the completion queue to return the tag. The rpc is finished when the
tag is back. tag is back.
``` ```cpp
void* got_tag; void* got_tag;
bool ok = false; bool ok = false;
cq.Next(&got_tag, &ok); cq.Next(&got_tag, &ok);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment