Update code according to comment
This commit is contained in:
parent
be8ac56e93
commit
deba4a99fe
|
|
@ -57,16 +57,13 @@ using helloworld::HelloRequest;
|
||||||
using helloworld::HelloReply;
|
using helloworld::HelloReply;
|
||||||
using helloworld::Greeter;
|
using helloworld::Greeter;
|
||||||
|
|
||||||
static bool got_sigint = false;
|
|
||||||
|
|
||||||
class ServerImpl final {
|
class ServerImpl final {
|
||||||
public:
|
public:
|
||||||
ServerImpl() : service_(&service_cq_) {}
|
ServerImpl() : service_(&cq_) {}
|
||||||
|
|
||||||
~ServerImpl() {
|
~ServerImpl() {
|
||||||
server_->Shutdown();
|
server_->Shutdown();
|
||||||
rpc_cq_.Shutdown();
|
cq_.Shutdown();
|
||||||
service_cq_.Shutdown();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// There is no shutdown handling in this code.
|
// There is no shutdown handling in this code.
|
||||||
|
|
@ -79,46 +76,56 @@ class ServerImpl final {
|
||||||
server_ = builder.BuildAndStart();
|
server_ = builder.BuildAndStart();
|
||||||
std::cout << "Server listening on " << server_address << std::endl;
|
std::cout << "Server listening on " << server_address << std::endl;
|
||||||
|
|
||||||
while (true) {
|
HandleRpcs();
|
||||||
CallData* rpc = new CallData();
|
|
||||||
service_.RequestSayHello(&rpc->ctx, &rpc->request, &rpc->responder,
|
|
||||||
&rpc_cq_, rpc);
|
|
||||||
void* got_tag;
|
|
||||||
bool ok;
|
|
||||||
service_cq_.Next(&got_tag, &ok);
|
|
||||||
GPR_ASSERT(ok);
|
|
||||||
GPR_ASSERT(got_tag == rpc);
|
|
||||||
|
|
||||||
std::thread t(&ServerImpl::HandleRpc, this, rpc);
|
|
||||||
t.detach();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
struct CallData {
|
class CallData {
|
||||||
CallData() : responder(&ctx) {}
|
public:
|
||||||
ServerContext ctx;
|
CallData(Greeter::AsyncService* service, CompletionQueue* cq)
|
||||||
HelloRequest request;
|
: service_(service), cq_(cq), responder_(&ctx_), status_(CREATE) {
|
||||||
HelloReply reply;
|
Proceed();
|
||||||
ServerAsyncResponseWriter<HelloReply> responder;
|
}
|
||||||
|
|
||||||
|
void Proceed() {
|
||||||
|
if (status_ == CREATE) {
|
||||||
|
service_->RequestSayHello(&ctx_, &request_, &responder_, cq_, this);
|
||||||
|
status_ = PROCESS;
|
||||||
|
} else if (status_ == PROCESS) {
|
||||||
|
new CallData(service_, cq_);
|
||||||
|
std::string prefix("Hello ");
|
||||||
|
reply_.set_message(prefix + request_.name());
|
||||||
|
responder_.Finish(reply_, Status::OK, this);
|
||||||
|
status_ = FINISH;
|
||||||
|
} else {
|
||||||
|
delete this;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
Greeter::AsyncService* service_;
|
||||||
|
CompletionQueue* cq_;
|
||||||
|
ServerContext ctx_;
|
||||||
|
HelloRequest request_;
|
||||||
|
HelloReply reply_;
|
||||||
|
ServerAsyncResponseWriter<HelloReply> responder_;
|
||||||
|
enum CallStatus { CREATE, PROCESS, FINISH };
|
||||||
|
CallStatus status_;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Runs in a detached thread, processes rpc then deletes data.
|
// This can be run in multiple threads if needed.
|
||||||
void HandleRpc(CallData* rpc) {
|
void HandleRpcs() {
|
||||||
std::string prefix("Hello ");
|
new CallData(&service_, &cq_);
|
||||||
rpc->reply.set_message(prefix + rpc->request.name());
|
void* tag;
|
||||||
rpc->responder.Finish(rpc->reply, Status::OK, &rpc->ctx);
|
|
||||||
void* got_tag;
|
|
||||||
bool ok;
|
bool ok;
|
||||||
rpc_cq_.Next(&got_tag, &ok);
|
while (true) {
|
||||||
GPR_ASSERT(ok);
|
cq_.Next(&tag, &ok);
|
||||||
GPR_ASSERT(got_tag == &rpc->ctx);
|
GPR_ASSERT(ok);
|
||||||
|
static_cast<CallData*>(tag)->Proceed();
|
||||||
delete rpc;
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
CompletionQueue service_cq_;
|
CompletionQueue cq_;
|
||||||
CompletionQueue rpc_cq_;
|
|
||||||
Greeter::AsyncService service_;
|
Greeter::AsyncService service_;
|
||||||
std::unique_ptr<Server> server_;
|
std::unique_ptr<Server> server_;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue