GRPC C++  1.31.1
client_context_impl.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_IMPL_H
35 #define GRPCPP_IMPL_CODEGEN_CLIENT_CONTEXT_IMPL_H
36 
37 #include <map>
38 #include <memory>
39 #include <string>
40 
55 
56 struct census_context;
57 struct grpc_call;
58 
59 namespace grpc {
60 
61 class ChannelInterface;
62 
63 namespace internal {
64 class RpcMethod;
65 template <class InputMessage, class OutputMessage>
66 class BlockingUnaryCallImpl;
67 class CallOpClientRecvStatus;
68 class CallOpRecvInitialMetadata;
69 class ServerContextImpl;
70 } // namespace internal
71 
72 namespace testing {
73 class InteropClientContextInspector;
74 } // namespace testing
75 } // namespace grpc
76 namespace grpc_impl {
77 
78 namespace internal {
79 template <class InputMessage, class OutputMessage>
80 class CallbackUnaryCallImpl;
81 template <class Request, class Response>
82 class ClientCallbackReaderWriterImpl;
83 template <class Response>
84 class ClientCallbackReaderImpl;
85 template <class Request>
86 class ClientCallbackWriterImpl;
87 class ClientCallbackUnaryImpl;
88 class ClientContextAccessor;
89 } // namespace internal
90 
91 class CallCredentials;
92 class Channel;
93 class CompletionQueue;
94 class ServerContext;
95 template <class R>
96 class ClientReader;
97 template <class W>
98 class ClientWriter;
99 template <class W, class R>
100 class ClientReaderWriter;
101 template <class R>
102 class ClientAsyncReader;
103 template <class W>
104 class ClientAsyncWriter;
105 template <class W, class R>
107 template <class R>
109 
110 class ServerContextBase;
112 
119  public:
121 
123  propagate_ |= GRPC_PROPAGATE_DEADLINE;
124  return *this;
125  }
126 
128  propagate_ &= ~GRPC_PROPAGATE_DEADLINE;
129  return *this;
130  }
131 
134  return *this;
135  }
136 
139  return *this;
140  }
141 
144  return *this;
145  }
146 
149  return *this;
150  }
151 
153  propagate_ |= GRPC_PROPAGATE_CANCELLATION;
154  return *this;
155  }
156 
158  propagate_ &= ~GRPC_PROPAGATE_CANCELLATION;
159  return *this;
160  }
161 
162  uint32_t c_bitmask() const { return propagate_; }
163 
164  private:
165  uint32_t propagate_;
166 };
167 
185  public:
186  ClientContext();
187  ~ClientContext();
188 
199  static std::unique_ptr<ClientContext> FromServerContext(
200  const grpc_impl::ServerContext& server_context,
202  static std::unique_ptr<ClientContext> FromCallbackServerContext(
203  const grpc_impl::CallbackServerContext& server_context,
205 
223  void AddMetadata(const std::string& meta_key, const std::string& meta_value);
224 
233  const std::multimap<grpc::string_ref, grpc::string_ref>&
235  GPR_CODEGEN_ASSERT(initial_metadata_received_);
236  return *recv_initial_metadata_.map();
237  }
238 
245  const std::multimap<grpc::string_ref, grpc::string_ref>&
247  // TODO(yangg) check finished
248  return *trailing_metadata_.map();
249  }
250 
257  template <typename T>
258  void set_deadline(const T& deadline) {
259  grpc::TimePoint<T> deadline_tp(deadline);
260  deadline_ = deadline_tp.raw_time();
261  }
262 
268  void set_idempotent(bool idempotent) { idempotent_ = idempotent; }
269 
273  void set_cacheable(bool cacheable) { cacheable_ = cacheable; }
274 
281  void set_wait_for_ready(bool wait_for_ready) {
282  wait_for_ready_ = wait_for_ready;
283  wait_for_ready_explicitly_set_ = true;
284  }
285 
287  void set_fail_fast(bool fail_fast) { set_wait_for_ready(!fail_fast); }
288 
290  std::chrono::system_clock::time_point deadline() const {
291  return grpc::Timespec2Timepoint(deadline_);
292  }
293 
295  gpr_timespec raw_deadline() const { return deadline_; }
296 
299  void set_authority(const std::string& authority) { authority_ = authority; }
300 
305  std::shared_ptr<const grpc::AuthContext> auth_context() const {
306  if (auth_context_.get() == nullptr) {
307  auth_context_ = grpc::CreateAuthContext(call_);
308  }
309  return auth_context_;
310  }
311 
322  void set_credentials(
323  const std::shared_ptr<grpc_impl::CallCredentials>& creds);
324 
330  std::shared_ptr<grpc_impl::CallCredentials> credentials() { return creds_; }
331 
336  return compression_algorithm_;
337  }
338 
343 
354  void set_initial_metadata_corked(bool corked) {
355  initial_metadata_corked_ = corked;
356  }
357 
366  std::string peer() const;
367 
372  void set_census_context(struct census_context* ccp) { census_context_ = ccp; }
373 
375  struct census_context* census_context() const {
376  return census_context_;
377  }
378 
388  void TryCancel();
389 
395  public:
396  virtual ~GlobalCallbacks() {}
397  virtual void DefaultConstructor(ClientContext* context) = 0;
398  virtual void Destructor(ClientContext* context) = 0;
399  };
400  static void SetGlobalCallbacks(GlobalCallbacks* callbacks);
401 
404  grpc_call* c_call() { return call_; }
405 
411  std::string debug_error_string() const { return debug_error_string_; }
412 
413  private:
414  // Disallow copy and assign.
416  ClientContext& operator=(const ClientContext&);
417 
418  friend class ::grpc::testing::InteropClientContextInspector;
419  friend class ::grpc::internal::CallOpClientRecvStatus;
420  friend class ::grpc::internal::CallOpRecvInitialMetadata;
422  template <class R>
424  template <class W>
426  template <class W, class R>
428  template <class R>
430  template <class W>
432  template <class W, class R>
434  template <class R>
436  template <class InputMessage, class OutputMessage>
437  friend class ::grpc::internal::BlockingUnaryCallImpl;
438  template <class InputMessage, class OutputMessage>
439  friend class ::grpc_impl::internal::CallbackUnaryCallImpl;
440  template <class Request, class Response>
441  friend class ::grpc_impl::internal::ClientCallbackReaderWriterImpl;
442  template <class Response>
443  friend class ::grpc_impl::internal::ClientCallbackReaderImpl;
444  template <class Request>
445  friend class ::grpc_impl::internal::ClientCallbackWriterImpl;
446  friend class ::grpc_impl::internal::ClientCallbackUnaryImpl;
447  friend class ::grpc_impl::internal::ClientContextAccessor;
448 
449  // Used by friend class CallOpClientRecvStatus
450  void set_debug_error_string(const std::string& debug_error_string) {
451  debug_error_string_ = debug_error_string;
452  }
453 
454  grpc_call* call() const { return call_; }
455  void set_call(grpc_call* call,
456  const std::shared_ptr<::grpc_impl::Channel>& channel);
457 
458  grpc::experimental::ClientRpcInfo* set_client_rpc_info(
459  const char* method, grpc::internal::RpcMethod::RpcType type,
460  grpc::ChannelInterface* channel,
461  const std::vector<std::unique_ptr<
463  size_t interceptor_pos) {
464  rpc_info_ = grpc::experimental::ClientRpcInfo(this, type, method, channel);
465  rpc_info_.RegisterInterceptors(creators, interceptor_pos);
466  return &rpc_info_;
467  }
468 
469  uint32_t initial_metadata_flags() const {
470  return (idempotent_ ? GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST : 0) |
471  (wait_for_ready_ ? GRPC_INITIAL_METADATA_WAIT_FOR_READY : 0) |
472  (cacheable_ ? GRPC_INITIAL_METADATA_CACHEABLE_REQUEST : 0) |
473  (wait_for_ready_explicitly_set_
475  : 0) |
476  (initial_metadata_corked_ ? GRPC_INITIAL_METADATA_CORKED : 0);
477  }
478 
479  std::string authority() { return authority_; }
480 
481  void SendCancelToInterceptors();
482 
483  static std::unique_ptr<ClientContext> FromInternalServerContext(
484  const grpc_impl::ServerContextBase& server_context,
485  PropagationOptions options);
486 
487  bool initial_metadata_received_;
488  bool wait_for_ready_;
489  bool wait_for_ready_explicitly_set_;
490  bool idempotent_;
491  bool cacheable_;
492  std::shared_ptr<::grpc_impl::Channel> channel_;
494  grpc_call* call_;
495  bool call_canceled_;
496  gpr_timespec deadline_;
497  std::string authority_;
498  std::shared_ptr<grpc_impl::CallCredentials> creds_;
499  mutable std::shared_ptr<const grpc::AuthContext> auth_context_;
500  struct census_context* census_context_;
501  std::multimap<std::string, std::string> send_initial_metadata_;
502  mutable grpc::internal::MetadataMap recv_initial_metadata_;
503  mutable grpc::internal::MetadataMap trailing_metadata_;
504 
505  grpc_call* propagate_from_call_;
506  PropagationOptions propagation_options_;
507 
508  grpc_compression_algorithm compression_algorithm_;
509  bool initial_metadata_corked_;
510 
511  std::string debug_error_string_;
512 
514 };
515 
516 } // namespace grpc_impl
517 
518 #endif // GRPCPP_IMPL_CODEGEN_CLIENT_CONTEXT_IMPL_H
grpc_impl::PropagationOptions::disable_census_stats_propagation
PropagationOptions & disable_census_stats_propagation()
Definition: client_context_impl.h:137
grpc_impl::ClientContext::set_census_context
void set_census_context(struct census_context *ccp)
Sets the census context.
Definition: client_context_impl.h:372
grpc_impl::ClientContext::ClientContext
ClientContext()
census_context
struct census_context census_context
A Census Context is a handle used by Census to represent the current tracing and stats collection inf...
Definition: census.h:34
grpc_impl::ClientContext::set_authority
void set_authority(const std::string &authority)
Set the per call authority header (see https://tools.ietf.org/html/rfc7540#section-8....
Definition: client_context_impl.h:299
grpc_impl::ClientContext::debug_error_string
std::string debug_error_string() const
EXPERIMENTAL debugging API.
Definition: client_context_impl.h:411
grpc_impl::ClientContext::GetServerTrailingMetadata
const std::multimap< grpc::string_ref, grpc::string_ref > & GetServerTrailingMetadata() const
Return a collection of trailing metadata key-value pairs.
Definition: client_context_impl.h:246
time.h
GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST
#define GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST
Initial metadata flags.
Definition: grpc_types.h:482
grpc
This header provides an object that reads bytes directly from a grpc::ByteBuffer, via the ZeroCopyInp...
Definition: alarm.h:24
grpc::experimental::CallbackServerContext
::grpc_impl::CallbackServerContext CallbackServerContext
Definition: server_context.h:37
status.h
grpc::ClientAsyncWriter
::grpc_impl::ClientAsyncWriter< W > ClientAsyncWriter
Definition: async_stream.h:49
grpc_impl::PropagationOptions::enable_census_tracing_propagation
PropagationOptions & enable_census_tracing_propagation()
Definition: client_context_impl.h:142
grpc_impl::ClientContext::set_idempotent
void set_idempotent(bool idempotent)
EXPERIMENTAL: Indicate that this request is idempotent.
Definition: client_context_impl.h:268
grpc_impl::ClientContext::GlobalCallbacks::~GlobalCallbacks
virtual ~GlobalCallbacks()
Definition: client_context_impl.h:396
grpc::CompletionQueue
::grpc_impl::CompletionQueue CompletionQueue
Definition: completion_queue.h:26
grpc_impl::ClientContext::set_initial_metadata_corked
void set_initial_metadata_corked(bool corked)
Flag whether the initial metadata should be corked.
Definition: client_context_impl.h:354
config.h
grpc_compression_algorithm
grpc_compression_algorithm
The various compression algorithms supported by gRPC (not sorted by compression level)
Definition: compression_types.h:57
grpc_impl::PropagationOptions::enable_cancellation_propagation
PropagationOptions & enable_cancellation_propagation()
Definition: client_context_impl.h:152
grpc_impl::ClientContext::compression_algorithm
grpc_compression_algorithm compression_algorithm() const
Return the compression algorithm the client call will request be used.
Definition: client_context_impl.h:335
grpc_impl::PropagationOptions::disable_cancellation_propagation
PropagationOptions & disable_cancellation_propagation()
Definition: client_context_impl.h:157
grpc::ClientAsyncReader
::grpc_impl::ClientAsyncReader< R > ClientAsyncReader
Definition: async_stream.h:43
grpc::ClientWriter
::grpc_impl::ClientWriter< W > ClientWriter
Definition: sync_stream.h:62
propagation_bits.h
grpc_impl::ClientContext::GlobalCallbacks
Global Callbacks.
Definition: client_context_impl.h:394
auth_context.h
core_codegen_interface.h
grpc_impl::ServerContextBase
Base class of ServerContext. Experimental until callback API is final.
Definition: server_context_impl.h:123
grpc_impl::PropagationOptions::PropagationOptions
PropagationOptions()
Definition: client_context_impl.h:120
grpc_impl::CallbackServerContext
Definition: server_context_impl.h:560
grpc_impl::ClientContext::peer
std::string peer() const
Return the peer uri in a string.
grpc_impl::ClientContext::set_cacheable
void set_cacheable(bool cacheable)
EXPERIMENTAL: Set this request to be cacheable.
Definition: client_context_impl.h:273
grpc_impl::ClientContext::~ClientContext
~ClientContext()
grpc_impl::ClientContext::c_call
grpc_call * c_call()
Should be used for framework-level extensions only.
Definition: client_context_impl.h:404
grpc_impl::ClientContext::SetGlobalCallbacks
static void SetGlobalCallbacks(GlobalCallbacks *callbacks)
grpc_impl::ClientContext::auth_context
std::shared_ptr< const grpc::AuthContext > auth_context() const
Return the authentication context for the associated client call.
Definition: client_context_impl.h:305
grpc::internal::MetadataMap
Definition: metadata_map.h:33
metadata_map.h
grpc::experimental::ClientRpcInfo
Definition: client_interceptor.h:69
grpc::internal::MetadataMap::map
std::multimap< grpc::string_ref, grpc::string_ref > * map()
Definition: metadata_map.h:66
grpc_impl::ClientContext::deadline
std::chrono::system_clock::time_point deadline() const
Return the deadline for the client call.
Definition: client_context_impl.h:290
grpc_impl::PropagationOptions::enable_census_stats_propagation
PropagationOptions & enable_census_stats_propagation()
Definition: client_context_impl.h:132
grpc_impl::ServerContext
A ServerContext or CallbackServerContext allows the code implementing a service handler to:
Definition: server_context_impl.h:520
GRPC_INITIAL_METADATA_WAIT_FOR_READY
#define GRPC_INITIAL_METADATA_WAIT_FOR_READY
Signal that the call should not return UNAVAILABLE before it has started.
Definition: grpc_types.h:484
grpc_impl::ClientContext::GlobalCallbacks::DefaultConstructor
virtual void DefaultConstructor(ClientContext *context)=0
grpc::CreateAuthContext
std::shared_ptr< const AuthContext > CreateAuthContext(grpc_call *call)
grpc_impl::ClientContext::GetServerInitialMetadata
const std::multimap< grpc::string_ref, grpc::string_ref > & GetServerInitialMetadata() const
Return a collection of initial metadata key-value pairs.
Definition: client_context_impl.h:234
GRPC_INITIAL_METADATA_WAIT_FOR_READY_EXPLICITLY_SET
#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:489
GRPC_INITIAL_METADATA_CACHEABLE_REQUEST
#define GRPC_INITIAL_METADATA_CACHEABLE_REQUEST
Signal that the call is cacheable.
Definition: grpc_types.h:486
GRPC_INITIAL_METADATA_CORKED
#define GRPC_INITIAL_METADATA_CORKED
Signal that the initial metadata should be corked.
Definition: grpc_types.h:491
grpc_call
struct grpc_call grpc_call
A Call represents an RPC.
Definition: grpc_types.h:70
grpc::TimePoint::raw_time
gpr_timespec raw_time()=delete
GRPC_PROPAGATE_CENSUS_STATS_CONTEXT
#define GRPC_PROPAGATE_CENSUS_STATS_CONTEXT
Propagate census context.
Definition: propagation_bits.h:33
grpc::ServerContext
::grpc_impl::ServerContext ServerContext
Definition: server_context.h:26
grpc::ChannelInterface
Codegen interface for grpc::Channel.
Definition: channel_interface.h:74
sync.h
rpc_method.h
grpc::internal::RpcMethod::RpcType
RpcType
Definition: rpc_method.h:31
grpc_impl::PropagationOptions
Options for ClientContext::FromServerContext specifying which traits from the ServerContext to propag...
Definition: client_context_impl.h:118
grpc_impl::PropagationOptions::disable_census_tracing_propagation
PropagationOptions & disable_census_tracing_propagation()
Definition: client_context_impl.h:147
grpc::experimental::ServerContextBase
::grpc_impl::ServerContextBase ServerContextBase
Definition: server_context.h:36
client_interceptor.h
compression_types.h
grpc::ClientAsyncResponseReader
grpc_impl::ClientAsyncResponseReader< R > ClientAsyncResponseReader
Definition: async_unary_call.h:31
string_ref.h
GRPC_PROPAGATE_DEFAULTS
#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
grpc_impl::ClientContext::raw_deadline
gpr_timespec raw_deadline() const
Return a gpr_timespec representation of the client call's deadline.
Definition: client_context_impl.h:295
grpc::CallCredentials
::grpc_impl::CallCredentials CallCredentials
Definition: credentials.h:27
grpc::experimental::ClientInterceptorFactoryInterface
Definition: client_interceptor.h:49
GRPC_PROPAGATE_CENSUS_TRACING_CONTEXT
#define GRPC_PROPAGATE_CENSUS_TRACING_CONTEXT
Definition: propagation_bits.h:34
grpc_impl::ClientContext::TryCancel
void TryCancel()
Send a best-effort out-of-band cancel on the call associated with this client context.
create_auth_context.h
grpc_impl::ClientContext::FromCallbackServerContext
static std::unique_ptr< ClientContext > FromCallbackServerContext(const grpc_impl::CallbackServerContext &server_context, PropagationOptions options=PropagationOptions())
grpc::ClientReader
::grpc_impl::ClientReader< R > ClientReader
Definition: sync_stream.h:56
grpc_impl::ClientContext::AddMetadata
void AddMetadata(const std::string &meta_key, const std::string &meta_value)
Add the (meta_key, meta_value) pair to the metadata associated with a client call.
grpc::ClientAsyncReaderWriter
::grpc_impl::ClientAsyncReaderWriter< W, R > ClientAsyncReaderWriter
Definition: async_stream.h:56
grpc_impl::PropagationOptions::c_bitmask
uint32_t c_bitmask() const
Definition: client_context_impl.h:162
grpc_impl
An Alarm posts the user-provided tag to its associated completion queue or invokes the user-provided ...
Definition: alarm_impl.h:33
grpc_impl::ClientContext::set_deadline
void set_deadline(const T &deadline)
Set the deadline for the client call.
Definition: client_context_impl.h:258
grpc_impl::ClientContext::set_wait_for_ready
void set_wait_for_ready(bool wait_for_ready)
EXPERIMENTAL: Trigger wait-for-ready or not on this request.
Definition: client_context_impl.h:281
grpc::ClientReaderWriter
::grpc_impl::ClientReaderWriter< W, R > ClientReaderWriter
Definition: sync_stream.h:69
GPR_CODEGEN_ASSERT
#define GPR_CODEGEN_ASSERT(x)
Codegen specific version of GPR_ASSERT.
Definition: core_codegen_interface.h:146
slice.h
grpc_impl::ClientContext::census_context
struct census_context * census_context() const
Returns the census context that has been set, or nullptr if not set.
Definition: client_context_impl.h:375
grpc_impl::ClientContext
A ClientContext allows the person implementing a service client to:
Definition: client_context_impl.h:184
grpc::internal::Mutex
Definition: sync.h:47
grpc_impl::ClientContext::set_compression_algorithm
void set_compression_algorithm(grpc_compression_algorithm algorithm)
Set algorithm to be the compression algorithm used for the client call.
grpc_impl::ClientContext::set_credentials
void set_credentials(const std::shared_ptr< grpc_impl::CallCredentials > &creds)
Set credentials for the client call.
grpc_impl::ClientContext::set_fail_fast
void set_fail_fast(bool fail_fast)
DEPRECATED: Use set_wait_for_ready() instead.
Definition: client_context_impl.h:287
GRPC_PROPAGATE_DEADLINE
#define GRPC_PROPAGATE_DEADLINE
Propagation bits: this can be bitwise or-ed to form propagation_mask for grpc_call.
Definition: propagation_bits.h:31
grpc_impl::PropagationOptions::disable_deadline_propagation
PropagationOptions & disable_deadline_propagation()
Definition: client_context_impl.h:127
gpr_timespec
Analogous to struct timespec.
Definition: gpr_types.h:47
grpc::PropagationOptions
::grpc_impl::PropagationOptions PropagationOptions
Definition: client_context.h:27
grpc_impl::ClientContext::GlobalCallbacks::Destructor
virtual void Destructor(ClientContext *context)=0
grpc_impl::ClientContext::credentials
std::shared_ptr< grpc_impl::CallCredentials > credentials()
EXPERIMENTAL debugging API.
Definition: client_context_impl.h:330
grpc::Timespec2Timepoint
std::chrono::system_clock::time_point Timespec2Timepoint(gpr_timespec t)
grpc::TimePoint
If you are trying to use CompletionQueue::AsyncNext with a time class that isn't either gpr_timespec ...
Definition: time.h:40
grpc::Channel
::grpc_impl::Channel Channel
Definition: channel.h:26
grpc_impl::ClientContext::FromServerContext
static std::unique_ptr< ClientContext > FromServerContext(const grpc_impl::ServerContext &server_context, PropagationOptions options=PropagationOptions())
Create a new ClientContext as a child of an incoming server call, according to options (.
grpc_impl::PropagationOptions::enable_deadline_propagation
PropagationOptions & enable_deadline_propagation()
Definition: client_context_impl.h:122
GRPC_PROPAGATE_CANCELLATION
#define GRPC_PROPAGATE_CANCELLATION
Propagate cancellation.
Definition: propagation_bits.h:36