Guest User

Async grpc c++

a guest
Mar 16th, 2018
713
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.56 KB | None | 0 0
  1. //Some pseudocode involved
  2.     class GenericCallData
  3.     {
  4.         //code..
  5.         virtual void Proceed();
  6.         //code..
  7.     }
  8.    
  9.     class ServerImpl final {
  10.      public:
  11.       ~ServerImpl() {
  12.         //code
  13.       }
  14.    
  15.       void Run() {
  16.         // some server initialization and other code
  17.         server_ = builder.BuildAndStart();
  18.    
  19.         HandleRpcs();
  20.       }
  21.    
  22.      private:
  23.       class CallDataForRPC1 : public GenericCallData {
  24.        public:
  25.         CallDataForRPC1(Greeter::AsyncService* service, ServerCompletionQueue* cq)
  26.             : service_(service), cq_(cq), responder_(&ctx_), status_(CREATE) {
  27.           Proceed();
  28.         }
  29.    
  30.         void Proceed() {
  31.           if (status_ == CREATE) {
  32.             status_ = PROCESS;
  33.             service_->RPC1(&ctx_, &request_, &responder_, cq_, cq_,
  34.                                       this);
  35.           } else if (status_ == PROCESS) {
  36.             new CallData(service_, cq_);
  37.             //code..
  38.           } else {
  39.               //code..
  40.           }
  41.         }
  42.    
  43.        private:
  44.         Greeter::AsyncService* service_;
  45.         ServerCompletionQueue* cq_;
  46.         ServerContext ctx_;
  47.         HelloRequest request_;
  48.         HelloReply reply_;
  49.         ServerAsyncResponseWriter<HelloReply> responder_;
  50.         enum CallStatus { CREATE, PROCESS, FINISH };
  51.         CallStatus status_;  // The current serving state.
  52.       };
  53.      
  54.       class CallDataForRPC2 : public GenericCallData {
  55.          // similar structure
  56.          service_->RPC2(&ctx_, &request_, &responder_, cq_, cq_,
  57.                                       this);
  58.          // similar structure
  59.       }
  60.    
  61.       class CallDataForRPC3 : public GenericCallData {
  62.           // similar structure
  63.       }
  64.    
  65.       // so on... If there are 400 RPC I'll have to create a new class for every RPC
  66.    
  67.       void HandleRpcs() {
  68.         new CallDataForRPC1(&service_, cq_.get());
  69.         new CallDataForRPC2(&service_, cq_.get());
  70.         new CallDataForRPC3(&service, cq_.get());
  71.         // so on...
  72.        
  73.         // Here is the event loop to fetch the next rpc request and proceed with the corresponding class's Proceed function
  74.         void* tag;  // uniquely identifies a request.
  75.         bool ok;
  76.         while (true) {
  77.           GPR_ASSERT(cq_->Next(&tag, &ok));
  78.           GPR_ASSERT(ok);
  79.           static_cast<CallData*>(tag)->Proceed();
  80.         }
  81.       }
  82.    
  83.       std::unique_ptr<ServerCompletionQueue> cq_;
  84.       Greeter::AsyncService service_;
  85.       std::unique_ptr<Server> server_;
  86.     };
Add Comment
Please, Sign In to add comment