簡體   English   中英

如何在Protobuf中的C ++中生成BlockingStub?

[英]How to generate BlockingStub in C++ in Protobuf?

我為Protobuf使用以下.proto(2.6.1會更詳細):

service InstallService {
    rpc getWifiNetworks (WifiRequest) returns (WifiResponse);
}

我已經生成了Java文件,並且正在使用BlockingStub:

TestInstallService.BlockingInterface service = TestInstallService.newBlockingStub(channel);

而且我可以使用if阻止方式(效果很好):

Wifi.WifiResponse response = service.getWifiNetworks(controller, request);

現在,我正在創建C ++客戶端,該客戶端也應該以阻塞的方式工作,但是我既看不到原型,也不在生成的C ++代碼中看到任何Blocking接口。 如何在Protobuf中的C ++中生成BlockingStub? 如果以異步方式工作,如何通過閉包?

生成的C ++服務文件(.cpp):

class InstallService_Stub;

class InstallService : public ::google::protobuf::Service {
 protected:
  // This class should be treated as an abstract interface.
  inline InstallService() {};
 public:
  virtual ~InstallService();

  typedef InstallService_Stub Stub;

  static const ::google::protobuf::ServiceDescriptor* descriptor();

  virtual void getWifiNetworks(::google::protobuf::RpcController* controller,
                       const ::WifiRequest* request,
                       ::WifiResponse* response,
                       ::google::protobuf::Closure* done);

  // implements Service ----------------------------------------------

  const ::google::protobuf::ServiceDescriptor* GetDescriptor();
  void CallMethod(const ::google::protobuf::MethodDescriptor* method,
                  ::google::protobuf::RpcController* controller,
                  const ::google::protobuf::Message* request,
                  ::google::protobuf::Message* response,
                  ::google::protobuf::Closure* done);
  const ::google::protobuf::Message& GetRequestPrototype(
    const ::google::protobuf::MethodDescriptor* method) const;
  const ::google::protobuf::Message& GetResponsePrototype(
    const ::google::protobuf::MethodDescriptor* method) const;

 private:
  GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(InstallService);
};

class InstallService_Stub : public InstallService {
 public:
  InstallService_Stub(::google::protobuf::RpcChannel* channel);
  InstallService_Stub(::google::protobuf::RpcChannel* channel,
                   ::google::protobuf::Service::ChannelOwnership ownership);
  ~InstallService_Stub();

  inline ::google::protobuf::RpcChannel* channel() { return channel_; }

  // implements InstallService ------------------------------------------

  void getWifiNetworks(::google::protobuf::RpcController* controller,
                       const ::WifiRequest* request,
                       ::WifiResponse* response,
                       ::google::protobuf::Closure* done);
 private:
  ::google::protobuf::RpcChannel* channel_;
  bool owns_channel_;
  GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(InstallService_Stub);
};

protoc似乎沒有生成任何阻止代碼,因此我不得不使用自制的阻止功能:

bool callbackFired = false;

void myCallback() {
    // ...
    callbackFired = true;
}

// run service method
service->myMethod(rpcController, request, response, NewCallback(&myCallback));

// block the thread until callback is invoked
while (!callbackFired);

...

C ++客戶端用法示例: https : //github.com/4ntoine/protobuf-socket-rpc

您這樣做的方法是提供自己的InstallService子類,該子類將覆蓋您要實現的方法:

struct MyInstallService : public InstallService
{
    void getWifiNetworks(::google::protobuf::RpcController* controller,
                       const ::WifiRequest* request,
                       ::WifiResponse* response,
                       ::google::protobuf::Closure* done) override
  {
    // do your work here

    // fill up the response here

    done->Run();  // this will trigger the response
  }
};

客戶端:像這樣

namespace detail {
template<class F>
struct simple_closure : google::protobuf::Closure {

    simple_closure(F f)
    : _f(std::move(f))
    {}

    void Run() override {
        _f();
    }
private:
    F _f;
};
}

template<class F>
std::unique_ptr<detail::simple_closure<F>> make_closure(F&& f) {
    return std::make_unique<detail::simple_closure<F>>(std::forward<F>(f));
}

std::unique_ptr<WifiResponse> syncGetWifiNetworks(InstallService_Stub & stub, const WifiRequest& req)
{
    auto result = std::make_unique<WifiResponse>();
    auto promise = std::promise<std::unique_ptr<WifiResponse>>;
    auto future = promise.get_future();
    auto controller = allocate_controller(); // you need to write this
    auto closure = make_closure([&result, &promise]{
        promise.set_value(std::move(result));
    });
    // assumes you already have an async comms queue - otherwise just 
    // dispatch this lambda to a std::async(std::launch::async, ...)
    comms_queue.dispatch([&controller, &req, &stub, &response]{
        stub.getWifiNetworks(controller, &req, response.get(), closure);
    };

    // HERE is where the current thread blocks until the promise is fulfilled
    return future.get();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM