GRPC C++  1.36.1
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 <string>
40 
55 
56 struct census_context;
57 struct grpc_call;
58 
59 namespace grpc {
60 class ServerContext;
61 class ServerContextBase;
63 
64 namespace internal {
65 template <class InputMessage, class OutputMessage>
66 class CallbackUnaryCallImpl;
67 template <class Request, class Response>
68 class ClientCallbackReaderWriterImpl;
69 template <class Response>
70 class ClientCallbackReaderImpl;
71 template <class Request>
72 class ClientCallbackWriterImpl;
73 class ClientCallbackUnaryImpl;
74 class ClientContextAccessor;
75 class ClientAsyncResponseReaderHelper;
76 } // namespace internal
77 
78 template <class R>
79 class ClientReader;
80 template <class W>
81 class ClientWriter;
82 template <class W, class R>
83 class ClientReaderWriter;
84 template <class R>
85 class ClientAsyncReader;
86 template <class W>
87 class ClientAsyncWriter;
88 template <class W, class R>
89 class ClientAsyncReaderWriter;
90 template <class R>
91 class ClientAsyncResponseReader;
92 
93 namespace testing {
94 class InteropClientContextInspector;
95 } // namespace testing
96 
97 namespace internal {
98 class RpcMethod;
99 template <class InputMessage, class OutputMessage>
100 class BlockingUnaryCallImpl;
101 class CallOpClientRecvStatus;
102 class CallOpRecvInitialMetadata;
103 class ServerContextImpl;
104 template <class InputMessage, class OutputMessage>
105 class CallbackUnaryCallImpl;
106 template <class Request, class Response>
107 class ClientCallbackReaderWriterImpl;
108 template <class Response>
109 class ClientCallbackReaderImpl;
110 template <class Request>
111 class ClientCallbackWriterImpl;
112 class ClientCallbackUnaryImpl;
113 class ClientContextAccessor;
114 } // namespace internal
115 
116 class CallCredentials;
117 class Channel;
118 class ChannelInterface;
119 class CompletionQueue;
120 
127  public:
129 
131  propagate_ |= GRPC_PROPAGATE_DEADLINE;
132  return *this;
133  }
134 
136  propagate_ &= ~GRPC_PROPAGATE_DEADLINE;
137  return *this;
138  }
139 
142  return *this;
143  }
144 
147  return *this;
148  }
149 
152  return *this;
153  }
154 
157  return *this;
158  }
159 
161  propagate_ |= GRPC_PROPAGATE_CANCELLATION;
162  return *this;
163  }
164 
166  propagate_ &= ~GRPC_PROPAGATE_CANCELLATION;
167  return *this;
168  }
169 
170  uint32_t c_bitmask() const { return propagate_; }
171 
172  private:
173  uint32_t propagate_;
174 };
175 
193  public:
194  ClientContext();
195  ~ClientContext();
196 
207  static std::unique_ptr<ClientContext> FromServerContext(
208  const grpc::ServerContext& server_context,
210  static std::unique_ptr<ClientContext> FromCallbackServerContext(
211  const grpc::CallbackServerContext& server_context,
213 
231  void AddMetadata(const std::string& meta_key, const std::string& meta_value);
232 
241  const std::multimap<grpc::string_ref, grpc::string_ref>&
243  GPR_CODEGEN_ASSERT(initial_metadata_received_);
244  return *recv_initial_metadata_.map();
245  }
246 
253  const std::multimap<grpc::string_ref, grpc::string_ref>&
255  // TODO(yangg) check finished
256  return *trailing_metadata_.map();
257  }
258 
265  template <typename T>
266  void set_deadline(const T& deadline) {
267  grpc::TimePoint<T> deadline_tp(deadline);
268  deadline_ = deadline_tp.raw_time();
269  }
270 
276  void set_idempotent(bool idempotent) { idempotent_ = idempotent; }
277 
281  void set_cacheable(bool cacheable) { cacheable_ = cacheable; }
282 
289  void set_wait_for_ready(bool wait_for_ready) {
290  wait_for_ready_ = wait_for_ready;
291  wait_for_ready_explicitly_set_ = true;
292  }
293 
295  void set_fail_fast(bool fail_fast) { set_wait_for_ready(!fail_fast); }
296 
298  std::chrono::system_clock::time_point deadline() const {
299  return grpc::Timespec2Timepoint(deadline_);
300  }
301 
303  gpr_timespec raw_deadline() const { return deadline_; }
304 
307  void set_authority(const std::string& authority) { authority_ = authority; }
308 
313  std::shared_ptr<const grpc::AuthContext> auth_context() const {
314  if (auth_context_ == nullptr) {
315  auth_context_ = grpc::CreateAuthContext(call_);
316  }
317  return auth_context_;
318  }
319 
330  void set_credentials(const std::shared_ptr<grpc::CallCredentials>& creds);
331 
337  std::shared_ptr<grpc::CallCredentials> credentials() { return creds_; }
338 
343  return compression_algorithm_;
344  }
345 
350 
361  void set_initial_metadata_corked(bool corked) {
362  initial_metadata_corked_ = corked;
363  }
364 
373  std::string peer() const;
374 
379  void set_census_context(struct census_context* ccp) { census_context_ = ccp; }
380 
382  struct census_context* census_context() const {
383  return census_context_;
384  }
385 
395  void TryCancel();
396 
402  public:
403  virtual ~GlobalCallbacks() {}
404  virtual void DefaultConstructor(ClientContext* context) = 0;
405  virtual void Destructor(ClientContext* context) = 0;
406  };
407  static void SetGlobalCallbacks(GlobalCallbacks* callbacks);
408 
411  grpc_call* c_call() { return call_; }
412 
418  std::string debug_error_string() const { return debug_error_string_; }
419 
420  private:
421  // Disallow copy and assign.
423  ClientContext& operator=(const ClientContext&);
424 
425  friend class ::grpc::testing::InteropClientContextInspector;
426  friend class ::grpc::internal::CallOpClientRecvStatus;
427  friend class ::grpc::internal::CallOpRecvInitialMetadata;
428  friend class ::grpc::Channel;
429  template <class R>
430  friend class ::grpc::ClientReader;
431  template <class W>
432  friend class ::grpc::ClientWriter;
433  template <class W, class R>
434  friend class ::grpc::ClientReaderWriter;
435  template <class R>
436  friend class ::grpc::ClientAsyncReader;
437  template <class W>
438  friend class ::grpc::ClientAsyncWriter;
439  template <class W, class R>
440  friend class ::grpc::ClientAsyncReaderWriter;
441  template <class R>
442  friend class ::grpc::ClientAsyncResponseReader;
443  friend class ::grpc::internal::ClientAsyncResponseReaderHelper;
444  template <class InputMessage, class OutputMessage>
445  friend class ::grpc::internal::BlockingUnaryCallImpl;
446  template <class InputMessage, class OutputMessage>
447  friend class ::grpc::internal::CallbackUnaryCallImpl;
448  template <class Request, class Response>
449  friend class ::grpc::internal::ClientCallbackReaderWriterImpl;
450  template <class Response>
451  friend class ::grpc::internal::ClientCallbackReaderImpl;
452  template <class Request>
453  friend class ::grpc::internal::ClientCallbackWriterImpl;
454  friend class ::grpc::internal::ClientCallbackUnaryImpl;
455  friend class ::grpc::internal::ClientContextAccessor;
456 
457  // Used by friend class CallOpClientRecvStatus
458  void set_debug_error_string(const std::string& debug_error_string) {
459  debug_error_string_ = debug_error_string;
460  }
461 
462  grpc_call* call() const { return call_; }
463  void set_call(grpc_call* call,
464  const std::shared_ptr<::grpc::Channel>& channel);
465 
466  grpc::experimental::ClientRpcInfo* set_client_rpc_info(
467  const char* method, grpc::internal::RpcMethod::RpcType type,
468  grpc::ChannelInterface* channel,
469  const std::vector<std::unique_ptr<
471  size_t interceptor_pos) {
472  rpc_info_ = grpc::experimental::ClientRpcInfo(this, type, method, channel);
473  rpc_info_.RegisterInterceptors(creators, interceptor_pos);
474  return &rpc_info_;
475  }
476 
477  uint32_t initial_metadata_flags() const {
478  return (idempotent_ ? GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST : 0) |
479  (wait_for_ready_ ? GRPC_INITIAL_METADATA_WAIT_FOR_READY : 0) |
480  (cacheable_ ? GRPC_INITIAL_METADATA_CACHEABLE_REQUEST : 0) |
481  (wait_for_ready_explicitly_set_
483  : 0) |
484  (initial_metadata_corked_ ? GRPC_INITIAL_METADATA_CORKED : 0);
485  }
486 
487  std::string authority() { return authority_; }
488 
489  void SendCancelToInterceptors();
490 
491  static std::unique_ptr<ClientContext> FromInternalServerContext(
492  const grpc::ServerContextBase& server_context,
493  PropagationOptions options);
494 
495  bool initial_metadata_received_;
496  bool wait_for_ready_;
497  bool wait_for_ready_explicitly_set_;
498  bool idempotent_;
499  bool cacheable_;
500  std::shared_ptr<::grpc::Channel> channel_;
502  grpc_call* call_;
503  bool call_canceled_;
504  gpr_timespec deadline_;
505  grpc::string authority_;
506  std::shared_ptr<grpc::CallCredentials> creds_;
507  mutable std::shared_ptr<const grpc::AuthContext> auth_context_;
508  struct census_context* census_context_;
509  std::multimap<std::string, std::string> send_initial_metadata_;
510  mutable grpc::internal::MetadataMap recv_initial_metadata_;
511  mutable grpc::internal::MetadataMap trailing_metadata_;
512 
513  grpc_call* propagate_from_call_;
514  PropagationOptions propagation_options_;
515 
516  grpc_compression_algorithm compression_algorithm_;
517  bool initial_metadata_corked_;
518 
519  std::string debug_error_string_;
520 
522 };
523 
524 } // namespace grpc
525 
526 #endif // GRPCPP_IMPL_CODEGEN_CLIENT_CONTEXT_H
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::ClientContext::compression_algorithm
grpc_compression_algorithm compression_algorithm() const
Return the compression algorithm the client call will request be used.
Definition: client_context.h:342
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::ServerContext
A ServerContext or CallbackServerContext allows the code implementing a service handler to:
Definition: server_context.h:546
time.h
grpc::ClientContext::GlobalCallbacks::Destructor
virtual void Destructor(ClientContext *context)=0
GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST
#define GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST
Initial metadata flags.
Definition: grpc_types.h:484
grpc
An Alarm posts the user-provided tag to its associated completion queue or invokes the user-provided ...
Definition: alarm.h:33
status.h
grpc::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.h:289
grpc::CallbackServerContext
Definition: server_context.h:586
grpc::ClientContext::set_deadline
void set_deadline(const T &deadline)
Set the deadline for the client call.
Definition: client_context.h:266
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::ClientContext::GlobalCallbacks::~GlobalCallbacks
virtual ~GlobalCallbacks()
Definition: client_context.h:403
grpc::PropagationOptions
Options for ClientContext::FromServerContext specifying which traits from the ServerContext to propag...
Definition: client_context.h:126
propagation_bits.h
auth_context.h
grpc::ServerContextBase
Base class of ServerContext. Experimental until callback API is final.
Definition: server_context.h:131
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:242
core_codegen_interface.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:303
grpc::experimental::ServerContextBase
::grpc::ServerContextBase ServerContextBase
Definition: server_context.h:108
grpc::PropagationOptions::PropagationOptions
PropagationOptions()
Definition: client_context.h:128
grpc::ClientContext::set_idempotent
void set_idempotent(bool idempotent)
EXPERIMENTAL: Indicate that this request is idempotent.
Definition: client_context.h:276
grpc::PropagationOptions::disable_census_stats_propagation
PropagationOptions & disable_census_stats_propagation()
Definition: client_context.h:145
grpc::ClientContext::set_census_context
void set_census_context(struct census_context *ccp)
Sets the census context.
Definition: client_context.h:379
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.
grpc::PropagationOptions::disable_census_tracing_propagation
PropagationOptions & disable_census_tracing_propagation()
Definition: client_context.h:155
grpc::internal::MetadataMap
Definition: metadata_map.h:33
metadata_map.h
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:254
grpc::experimental::ClientRpcInfo
Definition: client_interceptor.h:66
grpc::internal::MetadataMap::map
std::multimap< grpc::string_ref, grpc::string_ref > * map()
Definition: metadata_map.h:66
grpc::experimental::CallbackServerContext
::grpc::CallbackServerContext CallbackServerContext
Definition: server_context.h:109
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:486
grpc::CreateAuthContext
std::shared_ptr< const AuthContext > CreateAuthContext(grpc_call *call)
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:491
grpc::ClientContext
A ClientContext allows the person implementing a service client to:
Definition: client_context.h:192
GRPC_INITIAL_METADATA_CACHEABLE_REQUEST
#define GRPC_INITIAL_METADATA_CACHEABLE_REQUEST
Signal that the call is cacheable.
Definition: grpc_types.h:488
grpc::PropagationOptions::disable_deadline_propagation
PropagationOptions & disable_deadline_propagation()
Definition: client_context.h:135
GRPC_INITIAL_METADATA_CORKED
#define GRPC_INITIAL_METADATA_CORKED
Signal that the initial metadata should be corked.
Definition: grpc_types.h:493
grpc_call
struct grpc_call grpc_call
A Call represents an RPC.
Definition: grpc_types.h:70
grpc::ClientContext::TryCancel
void TryCancel()
Send a best-effort out-of-band cancel on the call associated with this client context.
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::ChannelInterface
Codegen interface for grpc::Channel.
Definition: channel_interface.h:71
grpc::ClientContext::SetGlobalCallbacks
static void SetGlobalCallbacks(GlobalCallbacks *callbacks)
grpc::ClientContext::FromServerContext
static std::unique_ptr< ClientContext > FromServerContext(const grpc::ServerContext &server_context, PropagationOptions options=PropagationOptions())
Create a new ClientContext as a child of an incoming server call, according to options (.
sync.h
rpc_method.h
grpc::ClientContext::deadline
std::chrono::system_clock::time_point deadline() const
Return the deadline for the client call.
Definition: client_context.h:298
grpc::internal::RpcMethod::RpcType
RpcType
Definition: rpc_method.h:31
grpc::PropagationOptions::enable_cancellation_propagation
PropagationOptions & enable_cancellation_propagation()
Definition: client_context.h:160
client_interceptor.h
grpc::PropagationOptions::enable_deadline_propagation
PropagationOptions & enable_deadline_propagation()
Definition: client_context.h:130
compression_types.h
string_ref.h
grpc::ClientContext::set_fail_fast
void set_fail_fast(bool fail_fast)
DEPRECATED: Use set_wait_for_ready() instead.
Definition: client_context.h:295
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::PropagationOptions::c_bitmask
uint32_t c_bitmask() const
Definition: client_context.h:170
grpc::PropagationOptions::enable_census_tracing_propagation
PropagationOptions & enable_census_tracing_propagation()
Definition: client_context.h:150
grpc::experimental::ClientInterceptorFactoryInterface
Definition: client_interceptor.h:46
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:361
GRPC_PROPAGATE_CENSUS_TRACING_CONTEXT
#define GRPC_PROPAGATE_CENSUS_TRACING_CONTEXT
Definition: propagation_bits.h:34
grpc::ClientContext::GlobalCallbacks
Global Callbacks.
Definition: client_context.h:401
grpc::ClientContext::FromCallbackServerContext
static std::unique_ptr< ClientContext > FromCallbackServerContext(const grpc::CallbackServerContext &server_context, PropagationOptions options=PropagationOptions())
create_auth_context.h
grpc::ClientContext::set_cacheable
void set_cacheable(bool cacheable)
EXPERIMENTAL: Set this request to be cacheable.
Definition: client_context.h:281
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:313
GPR_CODEGEN_ASSERT
#define GPR_CODEGEN_ASSERT(x)
Codegen specific version of GPR_ASSERT.
Definition: core_codegen_interface.h:146
grpc::PropagationOptions::enable_census_stats_propagation
PropagationOptions & enable_census_stats_propagation()
Definition: client_context.h:140
slice.h
grpc::internal::Mutex
Definition: sync.h:47
grpc::PropagationOptions::disable_cancellation_propagation
PropagationOptions & disable_cancellation_propagation()
Definition: client_context.h:165
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
gpr_timespec
Analogous to struct timespec.
Definition: gpr_types.h:47
grpc::ClientContext::ClientContext
ClientContext()
grpc::ClientContext::credentials
std::shared_ptr< grpc::CallCredentials > credentials()
EXPERIMENTAL debugging API.
Definition: client_context.h:337
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:307
grpc::ClientContext::debug_error_string
std::string debug_error_string() const
EXPERIMENTAL debugging API.
Definition: client_context.h:418
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:382
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::ClientContext::c_call
grpc_call * c_call()
Should be used for framework-level extensions only.
Definition: client_context.h:411
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:36