GRPC C++  1.19.0-dev
client_context.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
33 
34 #ifndef GRPCPP_IMPL_CODEGEN_CLIENT_CONTEXT_H
35 #define GRPCPP_IMPL_CODEGEN_CLIENT_CONTEXT_H
36 
37 #include <map>
38 #include <memory>
39 #include <mutex>
40 #include <string>
41 
55 
56 struct census_context;
57 struct grpc_call;
58 
59 namespace grpc {
60 
61 class Channel;
62 class ChannelInterface;
63 class CompletionQueue;
64 class CallCredentials;
65 class ClientContext;
66 
67 namespace internal {
68 class RpcMethod;
69 class CallOpClientRecvStatus;
70 class CallOpRecvInitialMetadata;
71 template <class InputMessage, class OutputMessage>
72 class BlockingUnaryCallImpl;
73 template <class InputMessage, class OutputMessage>
74 class CallbackUnaryCallImpl;
75 template <class Request, class Response>
76 class ClientCallbackReaderWriterImpl;
77 template <class Response>
78 class ClientCallbackReaderImpl;
79 template <class Request>
80 class ClientCallbackWriterImpl;
81 } // namespace internal
82 
83 template <class R>
84 class ClientReader;
85 template <class W>
86 class ClientWriter;
87 template <class W, class R>
88 class ClientReaderWriter;
89 template <class R>
90 class ClientAsyncReader;
91 template <class W>
92 class ClientAsyncWriter;
93 template <class W, class R>
94 class ClientAsyncReaderWriter;
95 template <class R>
96 class ClientAsyncResponseReader;
97 class ServerContext;
98 
105  public:
107 
109  propagate_ |= GRPC_PROPAGATE_DEADLINE;
110  return *this;
111  }
112 
114  propagate_ &= ~GRPC_PROPAGATE_DEADLINE;
115  return *this;
116  }
117 
120  return *this;
121  }
122 
125  return *this;
126  }
127 
130  return *this;
131  }
132 
135  return *this;
136  }
137 
139  propagate_ |= GRPC_PROPAGATE_CANCELLATION;
140  return *this;
141  }
142 
144  propagate_ &= ~GRPC_PROPAGATE_CANCELLATION;
145  return *this;
146  }
147 
148  uint32_t c_bitmask() const { return propagate_; }
149 
150  private:
151  uint32_t propagate_;
152 };
153 
154 namespace testing {
155 class InteropClientContextInspector;
156 } // namespace testing
157 
175  public:
176  ClientContext();
177  ~ClientContext();
178 
189  static std::unique_ptr<ClientContext> FromServerContext(
190  const ServerContext& server_context,
192 
210  void AddMetadata(const grpc::string& meta_key,
211  const grpc::string& meta_value);
212 
221  const std::multimap<grpc::string_ref, grpc::string_ref>&
223  GPR_CODEGEN_ASSERT(initial_metadata_received_);
224  return *recv_initial_metadata_.map();
225  }
226 
233  const std::multimap<grpc::string_ref, grpc::string_ref>&
235  // TODO(yangg) check finished
236  return *trailing_metadata_.map();
237  }
238 
245  template <typename T>
246  void set_deadline(const T& deadline) {
247  TimePoint<T> deadline_tp(deadline);
248  deadline_ = deadline_tp.raw_time();
249  }
250 
256  void set_idempotent(bool idempotent) { idempotent_ = idempotent; }
257 
261  void set_cacheable(bool cacheable) { cacheable_ = cacheable; }
262 
269  void set_wait_for_ready(bool wait_for_ready) {
270  wait_for_ready_ = wait_for_ready;
271  wait_for_ready_explicitly_set_ = true;
272  }
273 
275  void set_fail_fast(bool fail_fast) { set_wait_for_ready(!fail_fast); }
276 
278  std::chrono::system_clock::time_point deadline() const {
279  return Timespec2Timepoint(deadline_);
280  }
281 
283  gpr_timespec raw_deadline() const { return deadline_; }
284 
287  void set_authority(const grpc::string& authority) { authority_ = authority; }
288 
292  std::shared_ptr<const AuthContext> auth_context() const {
293  if (auth_context_.get() == nullptr) {
294  auth_context_ = CreateAuthContext(call_);
295  }
296  return auth_context_;
297  }
298 
307  void set_credentials(const std::shared_ptr<CallCredentials>& creds) {
308  creds_ = creds;
309  }
310 
315  return compression_algorithm_;
316  }
317 
321  void set_compression_algorithm(grpc_compression_algorithm algorithm);
322 
333  void set_initial_metadata_corked(bool corked) {
334  initial_metadata_corked_ = corked;
335  }
336 
344  grpc::string peer() const;
345 
347  void set_census_context(struct census_context* ccp) { census_context_ = ccp; }
348  struct census_context* census_context() const {
349  return census_context_;
350  }
351 
361  void TryCancel();
362 
368  public:
369  virtual ~GlobalCallbacks() {}
370  virtual void DefaultConstructor(ClientContext* context) = 0;
371  virtual void Destructor(ClientContext* context) = 0;
372  };
373  static void SetGlobalCallbacks(GlobalCallbacks* callbacks);
374 
377  grpc_call* c_call() { return call_; }
378 
384  grpc::string debug_error_string() const { return debug_error_string_; }
385 
386  private:
387  // Disallow copy and assign.
389  ClientContext& operator=(const ClientContext&);
390 
391  friend class ::grpc::testing::InteropClientContextInspector;
392  friend class ::grpc::internal::CallOpClientRecvStatus;
393  friend class ::grpc::internal::CallOpRecvInitialMetadata;
394  friend class Channel;
395  template <class R>
396  friend class ::grpc::ClientReader;
397  template <class W>
398  friend class ::grpc::ClientWriter;
399  template <class W, class R>
400  friend class ::grpc::ClientReaderWriter;
401  template <class R>
402  friend class ::grpc::ClientAsyncReader;
403  template <class W>
404  friend class ::grpc::ClientAsyncWriter;
405  template <class W, class R>
406  friend class ::grpc::ClientAsyncReaderWriter;
407  template <class R>
408  friend class ::grpc::ClientAsyncResponseReader;
409  template <class InputMessage, class OutputMessage>
410  friend class ::grpc::internal::BlockingUnaryCallImpl;
411  template <class InputMessage, class OutputMessage>
412  friend class ::grpc::internal::CallbackUnaryCallImpl;
413  template <class Request, class Response>
414  friend class ::grpc::internal::ClientCallbackReaderWriterImpl;
415  template <class Response>
416  friend class ::grpc::internal::ClientCallbackReaderImpl;
417  template <class Request>
418  friend class ::grpc::internal::ClientCallbackWriterImpl;
419 
420  // Used by friend class CallOpClientRecvStatus
421  void set_debug_error_string(const grpc::string& debug_error_string) {
422  debug_error_string_ = debug_error_string;
423  }
424 
425  grpc_call* call() const { return call_; }
426  void set_call(grpc_call* call, const std::shared_ptr<Channel>& channel);
427 
428  experimental::ClientRpcInfo* set_client_rpc_info(
429  const char* method, internal::RpcMethod::RpcType type,
430  grpc::ChannelInterface* channel,
431  const std::vector<
432  std::unique_ptr<experimental::ClientInterceptorFactoryInterface>>&
433  creators,
434  size_t interceptor_pos) {
435  rpc_info_ = experimental::ClientRpcInfo(this, type, method, channel);
436  rpc_info_.RegisterInterceptors(creators, interceptor_pos);
437  return &rpc_info_;
438  }
439 
440  uint32_t initial_metadata_flags() const {
441  return (idempotent_ ? GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST : 0) |
442  (wait_for_ready_ ? GRPC_INITIAL_METADATA_WAIT_FOR_READY : 0) |
443  (cacheable_ ? GRPC_INITIAL_METADATA_CACHEABLE_REQUEST : 0) |
444  (wait_for_ready_explicitly_set_
446  : 0) |
447  (initial_metadata_corked_ ? GRPC_INITIAL_METADATA_CORKED : 0);
448  }
449 
450  grpc::string authority() { return authority_; }
451 
452  void SendCancelToInterceptors();
453 
454  bool initial_metadata_received_;
455  bool wait_for_ready_;
456  bool wait_for_ready_explicitly_set_;
457  bool idempotent_;
458  bool cacheable_;
459  std::shared_ptr<Channel> channel_;
460  std::mutex mu_;
461  grpc_call* call_;
462  bool call_canceled_;
463  gpr_timespec deadline_;
464  grpc::string authority_;
465  std::shared_ptr<CallCredentials> creds_;
466  mutable std::shared_ptr<const AuthContext> auth_context_;
467  struct census_context* census_context_;
468  std::multimap<grpc::string, grpc::string> send_initial_metadata_;
469  mutable internal::MetadataMap recv_initial_metadata_;
470  mutable internal::MetadataMap trailing_metadata_;
471 
472  grpc_call* propagate_from_call_;
473  PropagationOptions propagation_options_;
474 
475  grpc_compression_algorithm compression_algorithm_;
476  bool initial_metadata_corked_;
477 
478  grpc::string debug_error_string_;
479 
480  experimental::ClientRpcInfo rpc_info_;
481 };
482 
483 } // namespace grpc
484 
485 #endif // GRPCPP_IMPL_CODEGEN_CLIENT_CONTEXT_H
const std::multimap< grpc::string_ref, grpc::string_ref > & GetServerInitialMetadata() const
Return a collection of initial metadata key-value pairs.
Definition: client_context.h:222
struct grpc_call grpc_call
A Call represents an RPC.
Definition: grpc_types.h:70
std::shared_ptr< const AuthContext > CreateAuthContext(grpc_call *call)
grpc::string debug_error_string() const
EXPERIMENTAL debugging API.
Definition: client_context.h:384
struct census_context * census_context() const
Definition: client_context.h:348
void set_idempotent(bool idempotent)
EXPERIMENTAL: Indicate that this request is idempotent.
Definition: client_context.h:256
PropagationOptions & enable_census_tracing_propagation()
Definition: client_context.h:128
PropagationOptions & enable_deadline_propagation()
Definition: client_context.h:108
#define GPR_CODEGEN_ASSERT(x)
Codegen specific version of GPR_ASSERT.
Definition: core_codegen_interface.h:141
std::string string
Definition: config.h:35
const std::multimap< grpc::string_ref, grpc::string_ref > & GetServerTrailingMetadata() const
Return a collection of trailing metadata key-value pairs.
Definition: client_context.h:234
void set_wait_for_ready(bool wait_for_ready)
EXPERIMENTAL: Trigger wait-for-ready or not on this request.
Definition: client_context.h:269
Definition: metadata_map.h:33
grpc_call * c_call()
Should be used for framework-level extensions only.
Definition: client_context.h:377
void set_deadline(const T &deadline)
Set the deadline for the client call.
Definition: client_context.h:246
void set_initial_metadata_corked(bool corked)
Flag whether the initial metadata should be corked.
Definition: client_context.h:333
PropagationOptions & disable_cancellation_propagation()
Definition: client_context.h:143
PropagationOptions & enable_census_stats_propagation()
Definition: client_context.h:118
#define GRPC_INITIAL_METADATA_WAIT_FOR_READY_EXPLICITLY_SET
Signal that GRPC_INITIAL_METADATA_WAIT_FOR_READY was explicitly set by the calling application...
Definition: grpc_types.h:436
gpr_timespec raw_time()
Definition: time.h:43
RpcType
Definition: rpc_method.h:31
If you are trying to use CompletionQueue::AsyncNext with a time class that isn&#39;t either gpr_timespec ...
Definition: time.h:40
PropagationOptions & enable_cancellation_propagation()
Definition: client_context.h:138
#define GRPC_INITIAL_METADATA_CORKED
Signal that the initial metadata should be corked.
Definition: grpc_types.h:438
A ClientContext allows the person implementing a service client to:
Definition: client_context.h:174
uint32_t c_bitmask() const
Definition: client_context.h:148
Options for ClientContext::FromServerContext specifying which traits from the ServerContext to propag...
Definition: client_context.h:104
void set_fail_fast(bool fail_fast)
DEPRECATED: Use set_wait_for_ready() instead.
Definition: client_context.h:275
void set_census_context(struct census_context *ccp)
Get and set census context.
Definition: client_context.h:347
std::shared_ptr< const AuthContext > auth_context() const
Return the authentication context for this client call.
Definition: client_context.h:292
#define GRPC_PROPAGATE_CENSUS_TRACING_CONTEXT
Definition: propagation_bits.h:34
void set_cacheable(bool cacheable)
EXPERIMENTAL: Set this request to be cacheable.
Definition: client_context.h:261
Global Callbacks.
Definition: client_context.h:367
grpc_compression_algorithm
The various compression algorithms supported by gRPC (not sorted by compression level) ...
Definition: compression_types.h:57
grpc_compression_algorithm compression_algorithm() const
Return the compression algorithm the client call will request be used.
Definition: client_context.h:314
This header provides an object that reads bytes directly from a grpc::ByteBuffer, via the ZeroCopyInp...
Definition: alarm.h:24
Codegen interface for grpc::Channel.
Definition: channel_interface.h:65
#define GRPC_INITIAL_METADATA_CACHEABLE_REQUEST
Signal that the call is cacheable.
Definition: grpc_types.h:433
A ServerContext allows the person implementing a service handler to:
Definition: server_context.h:109
PropagationOptions & disable_census_tracing_propagation()
Definition: client_context.h:133
#define GRPC_INITIAL_METADATA_WAIT_FOR_READY
Signal that the call should not return UNAVAILABLE before it has started.
Definition: grpc_types.h:431
#define GRPC_PROPAGATE_DEFAULTS
Default propagation mask: clients of the core API are encouraged to encode deltas from this in their ...
Definition: propagation_bits.h:43
#define GRPC_PROPAGATE_CENSUS_STATS_CONTEXT
Propagate census context.
Definition: propagation_bits.h:33
void set_authority(const grpc::string &authority)
Set the per call authority header (see https://tools.ietf.org/html/rfc7540#section-8.1.2.3).
Definition: client_context.h:287
Analogous to struct timespec.
Definition: gpr_types.h:47
std::chrono::system_clock::time_point Timespec2Timepoint(gpr_timespec t)
#define GRPC_PROPAGATE_DEADLINE
Propagation bits: this can be bitwise or-ed to form propagation_mask for grpc_call.
Definition: propagation_bits.h:31
PropagationOptions()
Definition: client_context.h:106
gpr_timespec raw_deadline() const
Return a gpr_timespec representation of the client call&#39;s deadline.
Definition: client_context.h:283
void set_credentials(const std::shared_ptr< CallCredentials > &creds)
Set credentials for the client call.
Definition: client_context.h:307
PropagationOptions & disable_deadline_propagation()
Definition: client_context.h:113
Definition: client_interceptor.h:66
std::chrono::system_clock::time_point deadline() const
Return the deadline for the client call.
Definition: client_context.h:278
PropagationOptions & disable_census_stats_propagation()
Definition: client_context.h:123
virtual ~GlobalCallbacks()
Definition: client_context.h:369
#define GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST
Initial metadata flags.
Definition: grpc_types.h:429
#define GRPC_PROPAGATE_CANCELLATION
Propagate cancellation.
Definition: propagation_bits.h:36
Channels represent a connection to an endpoint. Created by CreateChannel.
Definition: channel.h:44