GRPC C++  1.81.0
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_CLIENT_CONTEXT_H
35 #define GRPCPP_CLIENT_CONTEXT_H
36 
43 #include <grpcpp/impl/rpc_method.h>
44 #include <grpcpp/impl/sync.h>
47 #include <grpcpp/support/config.h>
48 #include <grpcpp/support/slice.h>
49 #include <grpcpp/support/status.h>
51 #include <grpcpp/support/time.h>
52 
53 #include <map>
54 #include <memory>
55 #include <string>
56 
57 #include "absl/log/absl_check.h"
58 
59 struct census_context;
60 struct grpc_call;
61 
62 namespace grpc {
63 class ServerContext;
64 class ServerContextBase;
65 class CallbackServerContext;
66 
67 namespace internal {
68 template <class InputMessage, class OutputMessage>
70 
71 template <class Request, class Response>
73 template <class Response>
75 template <class Request>
78 class ClientContextAccessor;
80 } // namespace internal
81 
82 namespace experimental {
83 namespace internal {
85 } // namespace internal
86 } // namespace experimental
87 
88 template <class R>
90 template <class W>
92 template <class W, class R>
94 template <class R>
96 template <class W>
98 template <class W, class R>
100 template <class R>
102 
103 namespace testing {
104 class InteropClientContextInspector;
105 class ClientContextTestPeer;
106 } // namespace testing
107 
108 namespace internal {
109 class RpcMethod;
110 template <class InputMessage, class OutputMessage>
114 class ServerContextImpl;
115 template <class InputMessage, class OutputMessage>
117 template <class Request, class Response>
119 template <class Response>
121 template <class Request>
124 class ClientContextAccessor;
125 } // namespace internal
126 
127 class CallCredentials;
128 class Channel;
129 class ChannelInterface;
130 class CompletionQueue;
131 
138  public:
140 
142  propagate_ |= GRPC_PROPAGATE_DEADLINE;
143  return *this;
144  }
145 
147  propagate_ &= ~GRPC_PROPAGATE_DEADLINE;
148  return *this;
149  }
150 
153  return *this;
154  }
155 
158  return *this;
159  }
160 
163  return *this;
164  }
165 
168  return *this;
169  }
170 
172  propagate_ |= GRPC_PROPAGATE_CANCELLATION;
173  return *this;
174  }
175 
177  propagate_ &= ~GRPC_PROPAGATE_CANCELLATION;
178  return *this;
179  }
180 
181  uint32_t c_bitmask() const { return propagate_; }
182 
183  private:
184  uint32_t propagate_;
185 };
186 
204  public:
205  ClientContext();
206  ~ClientContext();
207 
218  static std::unique_ptr<ClientContext> FromServerContext(
219  const grpc::ServerContextBase& server_context,
221  static std::unique_ptr<ClientContext> FromCallbackServerContext(
222  const grpc::CallbackServerContext& server_context,
224 
247  void AddMetadata(const std::string& meta_key, const std::string& meta_value);
248 
257  const std::multimap<grpc::string_ref, grpc::string_ref>&
259  ABSL_CHECK(initial_metadata_received_);
260  return *recv_initial_metadata_.map();
261  }
262 
269  const std::multimap<grpc::string_ref, grpc::string_ref>&
271  // TODO(yangg) check finished
272  return *trailing_metadata_.map();
273  }
274 
281  template <typename T>
282  void set_deadline(const T& deadline) {
283  grpc::TimePoint<T> deadline_tp(deadline);
284  deadline_ = deadline_tp.raw_time();
285  }
286 
287  template <typename T>
288  void SetContext(T element) {
289  impl::CallContextRegistry::SetContext(std::move(element),
290  context_elements_);
291  }
292 
299  void set_wait_for_ready(bool wait_for_ready) {
300  wait_for_ready_ = wait_for_ready;
301  wait_for_ready_explicitly_set_ = true;
302  }
303 
305  void set_fail_fast(bool fail_fast) { set_wait_for_ready(!fail_fast); }
306 
308  std::chrono::system_clock::time_point deadline() const {
309  return grpc::Timespec2Timepoint(deadline_);
310  }
311 
313  gpr_timespec raw_deadline() const { return deadline_; }
314 
317  void set_authority(const std::string& authority) { authority_ = authority; }
318 
323  std::shared_ptr<const grpc::AuthContext> auth_context() const {
324  if (auth_context_ == nullptr) {
325  auth_context_ = grpc::CreateAuthContext(call_);
326  }
327  return auth_context_;
328  }
329 
340  void set_credentials(const std::shared_ptr<grpc::CallCredentials>& creds);
341 
347  std::shared_ptr<grpc::CallCredentials> credentials() { return creds_; }
348 
353  return compression_algorithm_;
354  }
355 
360 
371  void set_initial_metadata_corked(bool corked) {
372  initial_metadata_corked_ = corked;
373  }
374 
383  std::string peer() const;
384 
389  void set_census_context(struct census_context* ccp) { census_context_ = ccp; }
390 
392  struct census_context* census_context() const { return census_context_; }
393 
406  void TryCancel();
407 
413  public:
414  virtual ~GlobalCallbacks() {}
415  virtual void DefaultConstructor(ClientContext* context) = 0;
416  virtual void Destructor(ClientContext* context) = 0;
417  };
418  static void SetGlobalCallbacks(GlobalCallbacks* callbacks);
419 
422  grpc_call* c_call() { return call_; }
423 
429  std::string debug_error_string() const { return debug_error_string_; }
430 
431  private:
432  // Disallow copy and assign.
434  ClientContext& operator=(const ClientContext&);
435 
440  friend class grpc::Channel;
441  template <class R>
442  friend class grpc::ClientReader;
443  template <class W>
444  friend class grpc::ClientWriter;
445  template <class W, class R>
447  template <class R>
449  template <class W>
451  template <class W, class R>
453  template <class R>
456  template <class InputMessage, class OutputMessage>
458  template <class InputMessage, class OutputMessage>
461  template <class Request, class Response>
463  template <class Response>
465  template <class Request>
469 
470  // Used by friend class CallOpClientRecvStatus
471  void set_debug_error_string(const std::string& debug_error_string) {
472  debug_error_string_ = debug_error_string;
473  }
474 
475  grpc_call* call() const { return call_; }
476  void set_call(grpc_call* call, const std::shared_ptr<grpc::Channel>& channel);
477 
478  grpc::experimental::ClientRpcInfo* set_client_rpc_info(
479  const char* method, const char* suffix_for_stats,
481  const std::vector<std::unique_ptr<
483  size_t interceptor_pos) {
484  rpc_info_ = grpc::experimental::ClientRpcInfo(this, type, method,
485  suffix_for_stats, channel);
486  rpc_info_.RegisterInterceptors(creators, interceptor_pos);
487  return &rpc_info_;
488  }
489 
490  uint32_t initial_metadata_flags() const {
491  return (wait_for_ready_ ? GRPC_INITIAL_METADATA_WAIT_FOR_READY : 0) |
492  (wait_for_ready_explicitly_set_
494  : 0);
495  }
496 
497  std::string authority() { return authority_; }
498 
499  void SendCancelToInterceptors();
500 
501  static std::unique_ptr<ClientContext> FromInternalServerContext(
502  const grpc::ServerContextBase& server_context,
503  PropagationOptions options);
504 
505  bool initial_metadata_received_;
506  bool wait_for_ready_;
507  bool wait_for_ready_explicitly_set_;
508  std::shared_ptr<grpc::Channel> channel_;
510  grpc_call* call_;
511  bool call_canceled_;
512  gpr_timespec deadline_;
513  grpc::string authority_;
514  std::shared_ptr<grpc::CallCredentials> creds_;
515  mutable std::shared_ptr<const grpc::AuthContext> auth_context_;
516  struct census_context* census_context_;
517  std::multimap<std::string, std::string> send_initial_metadata_;
518  mutable grpc::internal::MetadataMap recv_initial_metadata_;
519  mutable grpc::internal::MetadataMap trailing_metadata_;
520  impl::CallContextRegistry::ElementList context_elements_ = nullptr;
521 
522  grpc_call* propagate_from_call_;
523  PropagationOptions propagation_options_;
524 
525  grpc_compression_algorithm compression_algorithm_;
526  bool initial_metadata_corked_;
527 
528  std::string debug_error_string_;
529 
531 };
532 
533 } // namespace grpc
534 
535 #endif // GRPCPP_CLIENT_CONTEXT_H
grpc::internal::ClientCallbackUnaryImpl
Definition: client_callback.h:1176
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:33
grpc::ClientContext::FromServerContext
static std::unique_ptr< ClientContext > FromServerContext(const grpc::ServerContextBase &server_context, PropagationOptions options=PropagationOptions())
Create a new ClientContext as a child of an incoming server call, according to options (.
time.h
grpc::internal::CallOpRecvInitialMetadata
Definition: call_op_set.h:723
grpc::ClientContext::compression_algorithm
grpc_compression_algorithm compression_algorithm() const
Return the compression algorithm the client call will request be used.
Definition: client_context.h:352
grpc::ClientContext::set_credentials
void set_credentials(const std::shared_ptr< grpc::CallCredentials > &creds)
Set credentials for the client call.
grpc::ClientContext::~ClientContext
~ClientContext()
grpc::internal::ClientCallbackReaderWriterImpl
Definition: client_context.h:72
grpc::ClientWriter
Synchronous (blocking) client-side API for doing client-streaming RPCs, where the outgoing message st...
Definition: client_context.h:91
grpc::ClientContext::GlobalCallbacks::Destructor
virtual void Destructor(ClientContext *context)=0
grpc
An Alarm posts the user-provided tag to its associated completion queue or invokes the user-provided ...
Definition: alarm.h:33
grpc::ClientContext::set_wait_for_ready
void set_wait_for_ready(bool wait_for_ready)
Trigger wait-for-ready or not on this request.
Definition: client_context.h:299
grpc::CallbackServerContext
Definition: server_context.h:632
allowed_call_context_types.h
grpc::ClientContext::set_deadline
void set_deadline(const T &deadline)
Set the deadline for the client call.
Definition: client_context.h:282
grpc_compression_algorithm
grpc_compression_algorithm
The various compression algorithms supported by gRPC (not sorted by compression level)
Definition: compression_types.h:61
grpc::ClientContext::GlobalCallbacks::~GlobalCallbacks
virtual ~GlobalCallbacks()
Definition: client_context.h:414
grpc::internal::BlockingUnaryCallImpl
Definition: client_context.h:111
grpc::PropagationOptions
Options for ClientContext::FromServerContext specifying which traits from the ServerContext to propag...
Definition: client_context.h:137
propagation_bits.h
grpc::ServerContextBase
Base class of ServerContext.
Definition: server_context.h:128
grpc::ClientContext::GlobalCallbacks::DefaultConstructor
virtual void DefaultConstructor(ClientContext *context)=0
grpc::ClientContext::peer
std::string peer() const
Return the peer uri in a string.
grpc::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.h:258
status.h
rpc_method.h
call_context_registry.h
grpc::ClientContext::raw_deadline
gpr_timespec raw_deadline() const
Return a gpr_timespec representation of the client call's deadline.
Definition: client_context.h:313
grpc::PropagationOptions::PropagationOptions
PropagationOptions()
Definition: client_context.h:139
grpc::PropagationOptions::disable_census_stats_propagation
PropagationOptions & disable_census_stats_propagation()
Definition: client_context.h:156
grpc::ClientContext::set_census_context
void set_census_context(struct census_context *ccp)
Sets the census context.
Definition: client_context.h:389
grpc::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.
create_auth_context.h
grpc::PropagationOptions::disable_census_tracing_propagation
PropagationOptions & disable_census_tracing_propagation()
Definition: client_context.h:166
grpc::impl::CallContextRegistry::SetContext
static void SetContext(T element, ElementList &elements)
Definition: call_context_registry.h:52
grpc::internal::MetadataMap
Definition: metadata_map.h:33
grpc::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.h:270
grpc::experimental::ClientRpcInfo
Definition: client_interceptor.h:71
grpc::internal::MetadataMap::map
std::multimap< grpc::string_ref, grpc::string_ref > * map()
Definition: metadata_map.h:66
client_interceptor.h
grpc::CallCredentials
A call credentials object encapsulates the state needed by a client to authenticate with a server for...
Definition: credentials.h:128
GRPC_INITIAL_METADATA_WAIT_FOR_READY
#define GRPC_INITIAL_METADATA_WAIT_FOR_READY
Initial metadata flags.
Definition: grpc_types.h:198
grpc::ClientContext::SetContext
void SetContext(T element)
Definition: client_context.h:288
grpc::ClientReader
Synchronous (blocking) client-side API for doing server-streaming RPCs, where the stream of messages ...
Definition: client_context.h:89
grpc::CreateAuthContext
std::shared_ptr< const AuthContext > CreateAuthContext(grpc_call *call)
TODO(ctiller): not sure we want to make this a permanent thing.
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:201
grpc::ClientContext
A ClientContext allows the person implementing a service client to:
Definition: client_context.h:203
grpc::PropagationOptions::disable_deadline_propagation
PropagationOptions & disable_deadline_propagation()
Definition: client_context.h:146
grpc::ClientAsyncResponseReader
Async API for client-side unary RPCs, where the message response received from the server is of type ...
Definition: client_context.h:101
grpc_call
struct grpc_call grpc_call
A Call represents an RPC.
Definition: grpc_types.h:68
grpc::ClientContext::TryCancel
void TryCancel()
Send a best-effort out-of-band cancel on the call associated with this client context.
grpc::ClientContext::ClientContextTestPeer
friend class grpc::testing::ClientContextTestPeer
Definition: client_context.h:437
grpc::internal::CallbackUnaryCallImpl
Definition: client_context.h:69
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:35
grpc::ClientAsyncReader
Async client-side API for doing server-streaming RPCs, where the incoming message stream coming from ...
Definition: client_context.h:95
grpc::ClientContext::InteropClientContextInspector
friend class grpc::testing::InteropClientContextInspector
Definition: client_context.h:436
grpc::ChannelInterface
Codegen interface for grpc::Channel.
Definition: channel_interface.h:79
grpc::ClientContext::SetGlobalCallbacks
static void SetGlobalCallbacks(GlobalCallbacks *callbacks)
grpc::internal::ClientAsyncResponseReaderHelper
Definition: async_unary_call.h:79
grpc::internal::ClientCallbackWriterImpl
Definition: client_context.h:76
grpc::ClientContext::deadline
std::chrono::system_clock::time_point deadline() const
Return the deadline for the client call.
Definition: client_context.h:308
grpc::internal::RpcMethod::RpcType
RpcType
Definition: rpc_method.h:31
grpc::PropagationOptions::enable_cancellation_propagation
PropagationOptions & enable_cancellation_propagation()
Definition: client_context.h:171
grpc::PropagationOptions::enable_deadline_propagation
PropagationOptions & enable_deadline_propagation()
Definition: client_context.h:141
grpc::ClientContext::ClientContextAccessor
friend class grpc::internal::ClientContextAccessor
Definition: client_context.h:468
compression_types.h
grpc::ClientAsyncWriter
Async API on the client side for doing client-streaming RPCs, where the outgoing message stream going...
Definition: client_context.h:97
grpc::ClientContext::set_fail_fast
void set_fail_fast(bool fail_fast)
DEPRECATED: Use set_wait_for_ready() instead.
Definition: client_context.h:305
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:45
grpc::PropagationOptions::c_bitmask
uint32_t c_bitmask() const
Definition: client_context.h:181
grpc::ClientAsyncReaderWriter
Async client-side interface for bi-directional streaming, where the outgoing message stream going to ...
Definition: client_context.h:99
grpc::PropagationOptions::enable_census_tracing_propagation
PropagationOptions & enable_census_tracing_propagation()
Definition: client_context.h:161
config.h
grpc::experimental::ClientInterceptorFactoryInterface
Definition: client_interceptor.h:48
grpc::ClientContext::set_initial_metadata_corked
void set_initial_metadata_corked(bool corked)
Flag whether the initial metadata should be corked.
Definition: client_context.h:371
GRPC_PROPAGATE_CENSUS_TRACING_CONTEXT
#define GRPC_PROPAGATE_CENSUS_TRACING_CONTEXT
Definition: propagation_bits.h:36
grpc::ClientReaderWriter
Synchronous (blocking) client-side API for bi-directional streaming RPCs, where the outgoing message ...
Definition: client_context.h:93
grpc::ClientContext::GlobalCallbacks
Global Callbacks.
Definition: client_context.h:412
grpc::impl::CallContextRegistry::ElementList
void ** ElementList
Definition: call_context_registry.h:48
grpc::ClientContext::FromCallbackServerContext
static std::unique_ptr< ClientContext > FromCallbackServerContext(const grpc::CallbackServerContext &server_context, PropagationOptions options=PropagationOptions())
grpc::internal::CallOpClientRecvStatus
Definition: call_op_set.h:771
grpc::CompletionQueue
A thin wrapper around grpc_completion_queue (see src/core/lib/surface/completion_queue....
Definition: completion_queue.h:104
grpc::experimental::internal::ClientCallbackSessionImpl
Definition: client_callback.h:1296
grpc::Channel
Channels represent a connection to an endpoint. Created by CreateChannel.
Definition: channel.h:59
grpc::internal::ClientCallbackReaderImpl
Definition: client_context.h:74
grpc::ClientContext::auth_context
std::shared_ptr< const grpc::AuthContext > auth_context() const
Return the authentication context for the associated client call.
Definition: client_context.h:323
grpc::PropagationOptions::enable_census_stats_propagation
PropagationOptions & enable_census_stats_propagation()
Definition: client_context.h:151
grpc::internal::Mutex
Definition: sync.h:57
grpc::PropagationOptions::disable_cancellation_propagation
PropagationOptions & disable_cancellation_propagation()
Definition: client_context.h:176
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:33
gpr_timespec
Analogous to struct timespec.
Definition: time.h:47
grpc::ClientContext::ClientContext
ClientContext()
slice.h
auth_context.h
grpc::ClientContext::credentials
std::shared_ptr< grpc::CallCredentials > credentials()
EXPERIMENTAL debugging API.
Definition: client_context.h:347
sync.h
grpc::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.h:317
grpc::ClientContext::debug_error_string
std::string debug_error_string() const
EXPERIMENTAL debugging API.
Definition: client_context.h:429
grpc::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.h:392
grpc::Timespec2Timepoint
std::chrono::system_clock::time_point Timespec2Timepoint(gpr_timespec t)
string_ref.h
grpc::TimePoint
If you are trying to use CompletionQueue::AsyncNext with a time class that isn't either gpr_timespec ...
Definition: time.h:45
grpc::ClientContext::c_call
grpc_call * c_call()
Should be used for framework-level extensions only.
Definition: client_context.h:422
metadata_map.h
grpc::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_PROPAGATE_CANCELLATION
#define GRPC_PROPAGATE_CANCELLATION
Propagate cancellation.
Definition: propagation_bits.h:38