|
|
|
@ -47,11 +47,11 @@ |
|
|
|
|
#include <grpc++/status.h> |
|
|
|
|
#include "helloworld.grpc.pb.h" |
|
|
|
|
|
|
|
|
|
using grpc::CompletionQueue; |
|
|
|
|
using grpc::Server; |
|
|
|
|
using grpc::ServerAsyncResponseWriter; |
|
|
|
|
using grpc::ServerBuilder; |
|
|
|
|
using grpc::ServerContext; |
|
|
|
|
using grpc::ServerCompletionQueue; |
|
|
|
|
using grpc::Status; |
|
|
|
|
using helloworld::HelloRequest; |
|
|
|
|
using helloworld::HelloReply; |
|
|
|
@ -59,11 +59,9 @@ using helloworld::Greeter; |
|
|
|
|
|
|
|
|
|
class ServerImpl final { |
|
|
|
|
public: |
|
|
|
|
ServerImpl() : service_(&cq_) {} |
|
|
|
|
|
|
|
|
|
~ServerImpl() { |
|
|
|
|
server_->Shutdown(); |
|
|
|
|
cq_.Shutdown(); |
|
|
|
|
cq_->Shutdown(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// There is no shutdown handling in this code.
|
|
|
|
@ -73,6 +71,7 @@ class ServerImpl final { |
|
|
|
|
ServerBuilder builder; |
|
|
|
|
builder.AddListeningPort(server_address, grpc::InsecureServerCredentials()); |
|
|
|
|
builder.RegisterAsyncService(&service_); |
|
|
|
|
cq_ = builder.AddCompletionQueue(); |
|
|
|
|
server_ = builder.BuildAndStart(); |
|
|
|
|
std::cout << "Server listening on " << server_address << std::endl; |
|
|
|
|
|
|
|
|
@ -82,14 +81,15 @@ class ServerImpl final { |
|
|
|
|
private: |
|
|
|
|
class CallData { |
|
|
|
|
public: |
|
|
|
|
CallData(Greeter::AsyncService* service, CompletionQueue* cq) |
|
|
|
|
CallData(Greeter::AsyncService* service, ServerCompletionQueue* cq) |
|
|
|
|
: service_(service), cq_(cq), responder_(&ctx_), status_(CREATE) { |
|
|
|
|
Proceed(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void Proceed() { |
|
|
|
|
if (status_ == CREATE) { |
|
|
|
|
service_->RequestSayHello(&ctx_, &request_, &responder_, cq_, this); |
|
|
|
|
service_->RequestSayHello(&ctx_, &request_, &responder_, cq_, cq_, |
|
|
|
|
this); |
|
|
|
|
status_ = PROCESS; |
|
|
|
|
} else if (status_ == PROCESS) { |
|
|
|
|
new CallData(service_, cq_); |
|
|
|
@ -104,7 +104,7 @@ class ServerImpl final { |
|
|
|
|
|
|
|
|
|
private: |
|
|
|
|
Greeter::AsyncService* service_; |
|
|
|
|
CompletionQueue* cq_; |
|
|
|
|
ServerCompletionQueue* cq_; |
|
|
|
|
ServerContext ctx_; |
|
|
|
|
HelloRequest request_; |
|
|
|
|
HelloReply reply_; |
|
|
|
@ -115,17 +115,17 @@ class ServerImpl final { |
|
|
|
|
|
|
|
|
|
// This can be run in multiple threads if needed.
|
|
|
|
|
void HandleRpcs() { |
|
|
|
|
new CallData(&service_, &cq_); |
|
|
|
|
new CallData(&service_, cq_.get()); |
|
|
|
|
void* tag; |
|
|
|
|
bool ok; |
|
|
|
|
while (true) { |
|
|
|
|
cq_.Next(&tag, &ok); |
|
|
|
|
cq_->Next(&tag, &ok); |
|
|
|
|
GPR_ASSERT(ok); |
|
|
|
|
static_cast<CallData*>(tag)->Proceed(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
CompletionQueue cq_; |
|
|
|
|
std::unique_ptr<ServerCompletionQueue> cq_; |
|
|
|
|
Greeter::AsyncService service_; |
|
|
|
|
std::unique_ptr<Server> server_; |
|
|
|
|
}; |
|
|
|
|