Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Some pseudocode involved
- class GenericCallData
- {
- //code..
- virtual void Proceed();
- //code..
- }
- class ServerImpl final {
- public:
- ~ServerImpl() {
- //code
- }
- void Run() {
- // some server initialization and other code
- server_ = builder.BuildAndStart();
- HandleRpcs();
- }
- private:
- class CallDataForRPC1 : public GenericCallData {
- public:
- CallDataForRPC1(Greeter::AsyncService* service, ServerCompletionQueue* cq)
- : service_(service), cq_(cq), responder_(&ctx_), status_(CREATE) {
- Proceed();
- }
- void Proceed() {
- if (status_ == CREATE) {
- status_ = PROCESS;
- service_->RPC1(&ctx_, &request_, &responder_, cq_, cq_,
- this);
- } else if (status_ == PROCESS) {
- new CallData(service_, cq_);
- //code..
- } else {
- //code..
- }
- }
- private:
- Greeter::AsyncService* service_;
- ServerCompletionQueue* cq_;
- ServerContext ctx_;
- HelloRequest request_;
- HelloReply reply_;
- ServerAsyncResponseWriter<HelloReply> responder_;
- enum CallStatus { CREATE, PROCESS, FINISH };
- CallStatus status_; // The current serving state.
- };
- class CallDataForRPC2 : public GenericCallData {
- // similar structure
- service_->RPC2(&ctx_, &request_, &responder_, cq_, cq_,
- this);
- // similar structure
- }
- class CallDataForRPC3 : public GenericCallData {
- // similar structure
- }
- // so on... If there are 400 RPC I'll have to create a new class for every RPC
- void HandleRpcs() {
- new CallDataForRPC1(&service_, cq_.get());
- new CallDataForRPC2(&service_, cq_.get());
- new CallDataForRPC3(&service, cq_.get());
- // so on...
- // Here is the event loop to fetch the next rpc request and proceed with the corresponding class's Proceed function
- void* tag; // uniquely identifies a request.
- bool ok;
- while (true) {
- GPR_ASSERT(cq_->Next(&tag, &ok));
- GPR_ASSERT(ok);
- static_cast<CallData*>(tag)->Proceed();
- }
- }
- std::unique_ptr<ServerCompletionQueue> cq_;
- Greeter::AsyncService service_;
- std::unique_ptr<Server> server_;
- };
Add Comment
Please, Sign In to add comment