GRPC C++  1.22.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 
56 
57 struct census_context;
58 struct grpc_call;
59 
60 namespace grpc_impl {
61 
62 class CallCredentials;
63 class Channel;
64 class CompletionQueue;
65 } // namespace grpc_impl
66 namespace grpc {
67 
68 class ChannelInterface;
69 class ClientContext;
70 
71 namespace internal {
72 class RpcMethod;
73 class CallOpClientRecvStatus;
74 class CallOpRecvInitialMetadata;
75 template <class InputMessage, class OutputMessage>
76 class BlockingUnaryCallImpl;
77 template <class InputMessage, class OutputMessage>
78 class CallbackUnaryCallImpl;
79 template <class Request, class Response>
80 class ClientCallbackReaderWriterImpl;
81 template <class Response>
82 class ClientCallbackReaderImpl;
83 template <class Request>
84 class ClientCallbackWriterImpl;
85 class ClientCallbackUnaryImpl;
86 } // namespace internal
87 
88 template <class R>
89 class ClientReader;
90 template <class W>
91 class ClientWriter;
92 template <class W, class R>
93 class ClientReaderWriter;
94 template <class R>
95 class ClientAsyncReader;
96 template <class W>
97 class ClientAsyncWriter;
98 template <class W, class R>
99 class ClientAsyncReaderWriter;
100 template <class R>
101 class ClientAsyncResponseReader;
102 class ServerContext;
103 
110  public:
112 
114  propagate_ |= GRPC_PROPAGATE_DEADLINE;
115  return *this;
116  }
117 
119  propagate_ &= ~GRPC_PROPAGATE_DEADLINE;
120  return *this;
121  }
122 
125  return *this;
126  }
127 
130  return *this;
131  }
132 
135  return *this;
136  }
137 
140  return *this;
141  }
142 
144  propagate_ |= GRPC_PROPAGATE_CANCELLATION;
145  return *this;
146  }
147 
149  propagate_ &= ~GRPC_PROPAGATE_CANCELLATION;
150  return *this;
151  }
152 
153  uint32_t c_bitmask() const { return propagate_; }
154 
155  private:
156  uint32_t propagate_;
157 };
158 
159 namespace testing {
160 class InteropClientContextInspector;
161 } // namespace testing
162 
180  public:
181  ClientContext();
182  ~ClientContext();
183 
194  static std::unique_ptr<ClientContext> FromServerContext(
195  const ServerContext& server_context,
197 
215  void AddMetadata(const grpc::string& meta_key,
216  const grpc::string& meta_value);
217 
226  const std::multimap<grpc::string_ref, grpc::string_ref>&
228  GPR_CODEGEN_ASSERT(initial_metadata_received_);
229  return *recv_initial_metadata_.map();
230  }
231 
238  const std::multimap<grpc::string_ref, grpc::string_ref>&
240  // TODO(yangg) check finished
241  return *trailing_metadata_.map();
242  }
243 
250  template <typename T>
251  void set_deadline(const T& deadline) {
252  TimePoint<T> deadline_tp(deadline);
253  deadline_ = deadline_tp.raw_time();
254  }
255 
261  void set_idempotent(bool idempotent) { idempotent_ = idempotent; }
262 
266  void set_cacheable(bool cacheable) { cacheable_ = cacheable; }
267 
274  void set_wait_for_ready(bool wait_for_ready) {
275  wait_for_ready_ = wait_for_ready;
276  wait_for_ready_explicitly_set_ = true;
277  }
278 
280  void set_fail_fast(bool fail_fast) { set_wait_for_ready(!fail_fast); }
281 
283  std::chrono::system_clock::time_point deadline() const {
284  return Timespec2Timepoint(deadline_);
285  }
286 
288  gpr_timespec raw_deadline() const { return deadline_; }
289 
292  void set_authority(const grpc::string& authority) { authority_ = authority; }
293 
297  std::shared_ptr<const AuthContext> auth_context() const {
298  if (auth_context_.get() == nullptr) {
299  auth_context_ = CreateAuthContext(call_);
300  }
301  return auth_context_;
302  }
303 
313  const std::shared_ptr<grpc_impl::CallCredentials>& creds) {
314  creds_ = creds;
315  }
316 
321  return compression_algorithm_;
322  }
323 
327  void set_compression_algorithm(grpc_compression_algorithm algorithm);
328 
339  void set_initial_metadata_corked(bool corked) {
340  initial_metadata_corked_ = corked;
341  }
342 
350  grpc::string peer() const;
351 
353  void set_census_context(struct census_context* ccp) { census_context_ = ccp; }
354  struct census_context* census_context() const {
355  return census_context_;
356  }
357 
367  void TryCancel();
368 
374  public:
375  virtual ~GlobalCallbacks() {}
376  virtual void DefaultConstructor(ClientContext* context) = 0;
377  virtual void Destructor(ClientContext* context) = 0;
378  };
379  static void SetGlobalCallbacks(GlobalCallbacks* callbacks);
380 
383  grpc_call* c_call() { return call_; }
384 
390  grpc::string debug_error_string() const { return debug_error_string_; }
391 
392  private:
393  // Disallow copy and assign.
395  ClientContext& operator=(const ClientContext&);
396 
397  friend class ::grpc::testing::InteropClientContextInspector;
398  friend class ::grpc::internal::CallOpClientRecvStatus;
399  friend class ::grpc::internal::CallOpRecvInitialMetadata;
401  template <class R>
402  friend class ::grpc::ClientReader;
403  template <class W>
404  friend class ::grpc::ClientWriter;
405  template <class W, class R>
406  friend class ::grpc::ClientReaderWriter;
407  template <class R>
408  friend class ::grpc::ClientAsyncReader;
409  template <class W>
410  friend class ::grpc::ClientAsyncWriter;
411  template <class W, class R>
412  friend class ::grpc::ClientAsyncReaderWriter;
413  template <class R>
414  friend class ::grpc::ClientAsyncResponseReader;
415  template <class InputMessage, class OutputMessage>
416  friend class ::grpc::internal::BlockingUnaryCallImpl;
417  template <class InputMessage, class OutputMessage>
418  friend class ::grpc::internal::CallbackUnaryCallImpl;
419  template <class Request, class Response>
420  friend class ::grpc::internal::ClientCallbackReaderWriterImpl;
421  template <class Response>
422  friend class ::grpc::internal::ClientCallbackReaderImpl;
423  template <class Request>
424  friend class ::grpc::internal::ClientCallbackWriterImpl;
425  friend class ::grpc::internal::ClientCallbackUnaryImpl;
426 
427  // Used by friend class CallOpClientRecvStatus
428  void set_debug_error_string(const grpc::string& debug_error_string) {
429  debug_error_string_ = debug_error_string;
430  }
431 
432  grpc_call* call() const { return call_; }
433  void set_call(grpc_call* call,
434  const std::shared_ptr<::grpc_impl::Channel>& channel);
435 
436  experimental::ClientRpcInfo* set_client_rpc_info(
437  const char* method, internal::RpcMethod::RpcType type,
438  grpc::ChannelInterface* channel,
439  const std::vector<
440  std::unique_ptr<experimental::ClientInterceptorFactoryInterface>>&
441  creators,
442  size_t interceptor_pos) {
443  rpc_info_ = experimental::ClientRpcInfo(this, type, method, channel);
444  rpc_info_.RegisterInterceptors(creators, interceptor_pos);
445  return &rpc_info_;
446  }
447 
448  uint32_t initial_metadata_flags() const {
449  return (idempotent_ ? GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST : 0) |
450  (wait_for_ready_ ? GRPC_INITIAL_METADATA_WAIT_FOR_READY : 0) |
451  (cacheable_ ? GRPC_INITIAL_METADATA_CACHEABLE_REQUEST : 0) |
452  (wait_for_ready_explicitly_set_
454  : 0) |
455  (initial_metadata_corked_ ? GRPC_INITIAL_METADATA_CORKED : 0);
456  }
457 
458  grpc::string authority() { return authority_; }
459 
460  void SendCancelToInterceptors();
461 
462  bool initial_metadata_received_;
463  bool wait_for_ready_;
464  bool wait_for_ready_explicitly_set_;
465  bool idempotent_;
466  bool cacheable_;
467  std::shared_ptr<::grpc_impl::Channel> channel_;
469  grpc_call* call_;
470  bool call_canceled_;
471  gpr_timespec deadline_;
472  grpc::string authority_;
473  std::shared_ptr<grpc_impl::CallCredentials> creds_;
474  mutable std::shared_ptr<const AuthContext> auth_context_;
475  struct census_context* census_context_;
476  std::multimap<grpc::string, grpc::string> send_initial_metadata_;
477  mutable internal::MetadataMap recv_initial_metadata_;
478  mutable internal::MetadataMap trailing_metadata_;
479 
480  grpc_call* propagate_from_call_;
481  PropagationOptions propagation_options_;
482 
483  grpc_compression_algorithm compression_algorithm_;
484  bool initial_metadata_corked_;
485 
486  grpc::string debug_error_string_;
487 
488  experimental::ClientRpcInfo rpc_info_;
489 };
490 
491 } // namespace grpc
492 
493 #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:227
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:390
struct census_context * census_context() const
Definition: client_context.h:354
void set_idempotent(bool idempotent)
EXPERIMENTAL: Indicate that this request is idempotent.
Definition: client_context.h:261
PropagationOptions & enable_census_tracing_propagation()
Definition: client_context.h:133
PropagationOptions & enable_deadline_propagation()
Definition: client_context.h:113
#define GPR_CODEGEN_ASSERT(x)
Codegen specific version of GPR_ASSERT.
Definition: core_codegen_interface.h:145
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:239
void set_wait_for_ready(bool wait_for_ready)
EXPERIMENTAL: Trigger wait-for-ready or not on this request.
Definition: client_context.h:274
Definition: metadata_map.h:33
grpc_call * c_call()
Should be used for framework-level extensions only.
Definition: client_context.h:383
void set_deadline(const T &deadline)
Set the deadline for the client call.
Definition: client_context.h:251
void set_initial_metadata_corked(bool corked)
Flag whether the initial metadata should be corked.
Definition: client_context.h:339
PropagationOptions & disable_cancellation_propagation()
Definition: client_context.h:148
PropagationOptions & enable_census_stats_propagation()
Definition: client_context.h:123
#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:447
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:143
#define GRPC_INITIAL_METADATA_CORKED
Signal that the initial metadata should be corked.
Definition: grpc_types.h:449
void set_credentials(const std::shared_ptr< grpc_impl::CallCredentials > &creds)
Set credentials for the client call.
Definition: client_context.h:312
A ClientContext allows the person implementing a service client to:
Definition: client_context.h:179
uint32_t c_bitmask() const
Definition: client_context.h:153
Options for ClientContext::FromServerContext specifying which traits from the ServerContext to propag...
Definition: client_context.h:109
void set_fail_fast(bool fail_fast)
DEPRECATED: Use set_wait_for_ready() instead.
Definition: client_context.h:280
::grpc_impl::Channel Channel
Definition: channel.h:26
void set_census_context(struct census_context *ccp)
Get and set census context.
Definition: client_context.h:353
std::shared_ptr< const AuthContext > auth_context() const
Return the authentication context for this client call.
Definition: client_context.h:297
#define GRPC_PROPAGATE_CENSUS_TRACING_CONTEXT
Definition: propagation_bits.h:34
::grpc_impl::CompletionQueue CompletionQueue
Definition: completion_queue.h:26
void set_cacheable(bool cacheable)
EXPERIMENTAL: Set this request to be cacheable.
Definition: client_context.h:266
Global Callbacks.
Definition: client_context.h:373
grpc_compression_algorithm
The various compression algorithms supported by gRPC (not sorted by compression level) ...
Definition: compression_types.h:57
Definition: sync.h:47
grpc_compression_algorithm compression_algorithm() const
Return the compression algorithm the client call will request be used.
Definition: client_context.h:320
This header provides an object that reads bytes directly from a grpc::ByteBuffer, via the ZeroCopyInp...
Definition: alarm.h:24
::grpc_impl::CallCredentials CallCredentials
Definition: credentials.h:27
Codegen interface for grpc::Channel.
Definition: channel_interface.h:69
#define GRPC_INITIAL_METADATA_CACHEABLE_REQUEST
Signal that the call is cacheable.
Definition: grpc_types.h:444
A ServerContext allows the person implementing a service handler to:
Definition: server_context.h:114
PropagationOptions & disable_census_tracing_propagation()
Definition: client_context.h:138
An Alarm posts the user-provided tag to its associated completion queue or invokes the user-provided ...
Definition: alarm_impl.h:33
#define GRPC_INITIAL_METADATA_WAIT_FOR_READY
Signal that the call should not return UNAVAILABLE before it has started.
Definition: grpc_types.h:442
#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:292
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:111
gpr_timespec raw_deadline() const
Return a gpr_timespec representation of the client call&#39;s deadline.
Definition: client_context.h:288
PropagationOptions & disable_deadline_propagation()
Definition: client_context.h:118
Definition: client_interceptor.h:70
std::chrono::system_clock::time_point deadline() const
Return the deadline for the client call.
Definition: client_context.h:283
PropagationOptions & disable_census_stats_propagation()
Definition: client_context.h:128
virtual ~GlobalCallbacks()
Definition: client_context.h:375
#define GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST
Initial metadata flags.
Definition: grpc_types.h:440
#define GRPC_PROPAGATE_CANCELLATION
Propagate cancellation.
Definition: propagation_bits.h:36