35 static void default_rsc_(RequestType&) {}
36 static void default_rhc_(
const ResponseType&) {}
37 static void default_ehc_(const ::grpc::Status& status) {
42 explicit ClientHelper(ClientType* client, StubType* stub, MethodType pfn)
43 : client_(client), stub_(stub), pfn_(pfn) {}
46 return with(extra, default_rsc_);
49 template <
typename RequestSetupCallable>
51 std::forward<RequestSetupCallable>(rsc)(request_);
55 template <
typename RequestSetupCallable>
56 ClientHelper& with(
const ProtoStruct& extra, RequestSetupCallable&& rsc) {
57 auto key = extra.find(impl::debug_map_key);
58 if (key != extra.end()) {
60 debug_key_ = *value.
get<std::string>();
62 *request_.mutable_extra() = map_to_struct(extra);
63 return with(std::forward<RequestSetupCallable>(rsc));
66 template <
typename ResponseHandlerCallable = decltype(default_rhc_)>
67 auto invoke(ResponseHandlerCallable&& rhc = default_rhc_) {
68 return invoke(std::forward<ResponseHandlerCallable>(rhc), default_ehc_);
71 template <
typename ResponseHandlerCallable,
typename ErrorHandlerCallable>
72 auto invoke(ResponseHandlerCallable&& rhc, ErrorHandlerCallable&& ehc) {
73 *request_.mutable_name() = client_->name();
76 if (debug_key_ !=
"") {
77 ctx.set_debug_key(debug_key_);
79 const auto result = (stub_->*pfn_)(ctx, request_, &response_);
81 return std::forward<ResponseHandlerCallable>(rhc)(
82 const_cast<const ResponseType&
>(response_));
85 std::forward<ErrorHandlerCallable>(ehc)(result);
86 client_helper_details::errorHandlerReturnedUnexpectedly(result);
92 template <
typename ResponseHandlerCallable,
93 typename ErrorHandlerCallable =
decltype(default_ehc_)>
94 auto invoke_stream(ResponseHandlerCallable rhc, ErrorHandlerCallable&& ehc = default_ehc_) {
95 *request_.mutable_name() = client_->name();
98 auto reader = (stub_->*pfn_)(ctx, request_);
100 bool cancelled_by_handler =
false;
102 while (reader->Read(&response_)) {
103 if (!rhc(response_)) {
104 cancelled_by_handler =
true;
105 static_cast<::grpc::ClientContext*
>(ctx)->TryCancel();
110 const auto result = reader->Finish();
113 (cancelled_by_handler && result.error_code() == ::grpc::StatusCode::CANCELLED)) {
117 std::forward<ErrorHandlerCallable>(ehc)(result);
118 client_helper_details::errorHandlerReturnedUnexpectedly(result);
124 std::string debug_key_;
126 RequestType request_;
127 ResponseType response_;
138 SyncMethodType<StubType, RequestType, ResponseType>>(client, &stub, method);