60 static void default_rsc_(RequestType&) {}
61 static void default_rhc_(
const ResponseType&) {}
62 static void default_ehc_(const ::grpc::Status* status) {
67 explicit ClientHelper(ClientType* client, StubType* stub, MethodType pfn)
68 : client_(client), stub_(stub), pfn_(pfn) {}
71 return with(extra, default_rsc_);
74 template <
typename RequestSetupCallable>
76 std::forward<RequestSetupCallable>(rsc)(request_);
80 template <
typename RequestSetupCallable>
81 ClientHelper& with(
const ProtoStruct& extra, RequestSetupCallable&& rsc) {
82 auto key = extra.find(impl::debug_map_key);
83 if (key != extra.end()) {
85 debug_key_ = *value.
get<std::string>();
89 return with(std::forward<RequestSetupCallable>(rsc));
92 template <
typename ResponseHandlerCallable = decltype(default_rhc_)>
93 auto invoke(ResponseHandlerCallable&& rhc = default_rhc_) {
94 return invoke(std::forward<ResponseHandlerCallable>(rhc), default_ehc_);
97 template <
typename ResponseHandlerCallable,
typename ErrorHandlerCallable>
98 auto invoke(ResponseHandlerCallable&& rhc, ErrorHandlerCallable&& ehc) {
99 *request_.mutable_name() = client_->name();
102 if (debug_key_ !=
"") {
103 ctx.set_debug_key(debug_key_);
105 const auto result = (stub_->*pfn_)(ctx, request_, &response_);
107 return std::forward<ResponseHandlerCallable>(rhc)(
108 const_cast<const ResponseType&
>(response_));
111 std::forward<ErrorHandlerCallable>(ehc)(&result);
112 client_helper_details::errorHandlerReturnedUnexpectedly(&result);
118 template <
typename ResponseHandlerCallable,
119 typename ErrorHandlerCallable =
decltype(default_ehc_)>
120 auto invoke_stream(ResponseHandlerCallable rhc, ErrorHandlerCallable&& ehc = default_ehc_) {
121 *request_.mutable_name() = client_->name();
124 auto reader = (stub_->*pfn_)(ctx, request_);
126 bool cancelled_by_handler =
false;
128 while (reader->Read(&response_)) {
129 if (!rhc(response_)) {
130 cancelled_by_handler =
true;
136 const auto result = reader->Finish();
138 if (result.ok() || (cancelled_by_handler &&
139 client_helper_details::isStatusCancelled(result.error_code()))) {
143 std::forward<ErrorHandlerCallable>(ehc)(&result);
144 client_helper_details::errorHandlerReturnedUnexpectedly(&result);
150 std::string debug_key_;
152 RequestType request_;
153 ResponseType response_;
164 SyncMethodType<StubType, RequestType, ResponseType>>(client, &stub, method);
Definition client_helper.hpp:25