Update code according to comment

This commit is contained in:
Yang Gao 2015-04-15 13:58:02 -07:00
parent be8ac56e93
commit deba4a99fe
1 changed files with 44 additions and 37 deletions

View File

@ -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); private:
t.detach(); class CallData {
public:
CallData(Greeter::AsyncService* service, CompletionQueue* cq)
: service_(service), cq_(cq), responder_(&ctx_), status_(CREATE) {
Proceed();
}
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: private:
struct CallData { Greeter::AsyncService* service_;
CallData() : responder(&ctx) {} CompletionQueue* cq_;
ServerContext ctx; ServerContext ctx_;
HelloRequest request; HelloRequest request_;
HelloReply reply; HelloReply reply_;
ServerAsyncResponseWriter<HelloReply> responder; 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) {
cq_.Next(&tag, &ok);
GPR_ASSERT(ok); GPR_ASSERT(ok);
GPR_ASSERT(got_tag == &rpc->ctx); 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_;
}; };