GRPC C++  1.33.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 } // namespace internal
76 
77 template <class R>
78 class ClientReader;
79 template <class W>
80 class ClientWriter;
81 template <class W, class R>
82 class ClientReaderWriter;
83 template <class R>
84 class ClientAsyncReader;
85 template <class W>
86 class ClientAsyncWriter;
87 template <class W, class R>
88 class ClientAsyncReaderWriter;
89 template <class R>
90 class ClientAsyncResponseReader;
91 
92 namespace testing {
93 class InteropClientContextInspector;
94 } // namespace testing
95 
96 namespace internal {
97 class RpcMethod;
98 template <class InputMessage, class OutputMessage>
99 class BlockingUnaryCallImpl;
100 class CallOpClientRecvStatus;
101 class CallOpRecvInitialMetadata;
102 class ServerContextImpl;
103 template <class InputMessage, class OutputMessage>
104 class CallbackUnaryCallImpl;
105 template <class Request, class Response>
106 class ClientCallbackReaderWriterImpl;
107 template <class Response>
108 class ClientCallbackReaderImpl;
109 template <class Request>
110 class ClientCallbackWriterImpl;
111 class ClientCallbackUnaryImpl;
112 class ClientContextAccessor;
113 } // namespace internal
114 
115 class CallCredentials;
116 class Channel;
117 class ChannelInterface;
118 class CompletionQueue;
119 
126  public:
128 
130  propagate_ |= GRPC_PROPAGATE_DEADLINE;
131  return *this;
132  }
133 
135  propagate_ &= ~GRPC_PROPAGATE_DEADLINE;
136  return *this;
137  }
138 
141  return *this;
142  }
143 
146  return *this;
147  }
148 
151  return *this;
152  }
153 
156  return *this;
157  }
158 
160  propagate_ |= GRPC_PROPAGATE_CANCELLATION;
161  return *this;
162  }
163 
165  propagate_ &= ~GRPC_PROPAGATE_CANCELLATION;
166  return *this;
167  }
168 
169  uint32_t c_bitmask() const { return propagate_; }
170 
171  private:
172  uint32_t propagate_;
173 };
174 
192  public:
193  ClientContext();
194  ~ClientContext();
195 
206  static std::unique_ptr<ClientContext> FromServerContext(
207  const grpc::ServerContext& server_context,
209  static std::unique_ptr<ClientContext> FromCallbackServerContext(
210  const grpc::CallbackServerContext& server_context,
212 
230  void AddMetadata(const std::string& meta_key, const std::string& meta_value);
231 
240  const std::multimap<grpc::string_ref, grpc::string_ref>&
242  GPR_CODEGEN_ASSERT(initial_metadata_received_);
243  return *recv_initial_metadata_.map();
244  }
245 
252  const std::multimap<grpc::string_ref, grpc::string_ref>&
254  // TODO(yangg) check finished
255  return *trailing_metadata_.map();
256  }
257 
264  template <typename T>
265  void set_deadline(const T& deadline) {
266  grpc::TimePoint<T> deadline_tp(deadline);
267  deadline_ = deadline_tp.raw_time();
268  }
269 
275  void set_idempotent(bool idempotent) { idempotent_ = idempotent; }
276 
280  void set_cacheable(bool cacheable) { cacheable_ = cacheable; }
281 
288  void set_wait_for_ready(bool wait_for_ready) {
289  wait_for_ready_ = wait_for_ready;
290  wait_for_ready_explicitly_set_ = true;
291  }
292 
294  void set_fail_fast(bool fail_fast) { set_wait_for_ready(!fail_fast); }
295 
297  std::chrono::system_clock::time_point deadline() const {
298  return grpc::Timespec2Timepoint(deadline_);
299  }
300 
302  gpr_timespec raw_deadline() const { return deadline_; }
303 
306  void set_authority(const std::string& authority) { authority_ = authority; }
307 
312  std::shared_ptr<const grpc::AuthContext> auth_context() const {
313  if (auth_context_.get() == nullptr) {
314  auth_context_ = grpc::CreateAuthContext(call_);
315  }
316  return auth_context_;
317  }
318 
329  void set_credentials(const std::shared_ptr<grpc::CallCredentials>& creds);
330 
336  std::shared_ptr<grpc::CallCredentials> credentials() { return creds_; }
337 
342  return compression_algorithm_;
343  }
344 
349 
360  void set_initial_metadata_corked(bool corked) {
361  initial_metadata_corked_ = corked;
362  }
363 
372  std::string peer() const;
373 
378  void set_census_context(struct census_context* ccp) { census_context_ = ccp; }
379 
381  struct census_context* census_context() const {
382  return census_context_;
383  }
384 
394  void TryCancel();
395 
401  public:
402  virtual ~GlobalCallbacks() {}
403  virtual void DefaultConstructor(ClientContext* context) = 0;
404  virtual void Destructor(ClientContext* context) = 0;
405  };
406  static void SetGlobalCallbacks(GlobalCallbacks* callbacks);
407 
410  grpc_call* c_call() { return call_; }
411 
417  std::string debug_error_string() const { return debug_error_string_; }
418 
419  private:
420  // Disallow copy and assign.
422  ClientContext& operator=(const ClientContext&);
423 
424  friend class ::grpc::testing::InteropClientContextInspector;
425  friend class ::grpc::internal::CallOpClientRecvStatus;
426  friend class ::grpc::internal::CallOpRecvInitialMetadata;
427  friend class ::grpc::Channel;
428  template <class R>
429  friend class ::grpc::ClientReader;
430  template <class W>
431  friend class ::grpc::ClientWriter;
432  template <class W, class R>
433  friend class ::grpc::ClientReaderWriter;
434  template <class R>
435  friend class ::grpc::ClientAsyncReader;
436  template <class W>
437  friend class ::grpc::ClientAsyncWriter;
438  template <class W, class R>
439  friend class ::grpc::ClientAsyncReaderWriter;
440  template <class R>
441  friend class ::grpc::ClientAsyncResponseReader;
442  template <class InputMessage, class OutputMessage>
443  friend class ::grpc::internal::BlockingUnaryCallImpl;
444  template <class InputMessage, class OutputMessage>
445  friend class ::grpc::internal::CallbackUnaryCallImpl;
446  template <class Request, class Response>
447  friend class ::grpc::internal::ClientCallbackReaderWriterImpl;
448  template <class Response>
449  friend class ::grpc::internal::ClientCallbackReaderImpl;
450  template <class Request>
451  friend class ::grpc::internal::ClientCallbackWriterImpl;
452  friend class ::grpc::internal::ClientCallbackUnaryImpl;
453  friend class ::grpc::internal::ClientContextAccessor;
454 
455  // Used by friend class CallOpClientRecvStatus
456  void set_debug_error_string(const std::string& debug_error_string) {
457  debug_error_string_ = debug_error_string;
458  }
459 
460  grpc_call* call() const { return call_; }
461  void set_call(grpc_call* call,
462  const std::shared_ptr<::grpc::Channel>& channel);
463 
464  grpc::experimental::ClientRpcInfo* set_client_rpc_info(
465  const char* method, grpc::internal::RpcMethod::RpcType type,
466  grpc::ChannelInterface* channel,
467  const std::vector<std::unique_ptr<
469  size_t interceptor_pos) {
470  rpc_info_ = grpc::experimental::ClientRpcInfo(this, type, method, channel);
471  rpc_info_.RegisterInterceptors(creators, interceptor_pos);
472  return &rpc_info_;
473  }
474 
475  uint32_t initial_metadata_flags() const {
476  return (idempotent_ ? GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST : 0) |
477  (wait_for_ready_ ? GRPC_INITIAL_METADATA_WAIT_FOR_READY : 0) |
478  (cacheable_ ? GRPC_INITIAL_METADATA_CACHEABLE_REQUEST : 0) |
479  (wait_for_ready_explicitly_set_
481  : 0) |
482  (initial_metadata_corked_ ? GRPC_INITIAL_METADATA_CORKED : 0);
483  }
484 
485  std::string authority() { return authority_; }
486 
487  void SendCancelToInterceptors();
488 
489  static std::unique_ptr<ClientContext> FromInternalServerContext(
490  const grpc::ServerContextBase& server_context,
491  PropagationOptions options);
492 
493  bool initial_metadata_received_;
494  bool wait_for_ready_;
495  bool wait_for_ready_explicitly_set_;
496  bool idempotent_;
497  bool cacheable_;
498  std::shared_ptr<::grpc::Channel> channel_;
500  grpc_call* call_;
501  bool call_canceled_;
502  gpr_timespec deadline_;
503  grpc::string authority_;
504  std::shared_ptr<grpc::CallCredentials> creds_;
505  mutable std::shared_ptr<const grpc::AuthContext> auth_context_;
506  struct census_context* census_context_;
507  std::multimap<std::string, std::string> send_initial_metadata_;
508  mutable grpc::internal::MetadataMap recv_initial_metadata_;
509  mutable grpc::internal::MetadataMap trailing_metadata_;
510 
511  grpc_call* propagate_from_call_;
512  PropagationOptions propagation_options_;
513 
514  grpc_compression_algorithm compression_algorithm_;
515  bool initial_metadata_corked_;
516 
517  std::string debug_error_string_;
518 
520 };
521 
522 } // namespace grpc
523 
524 #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:341
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:527
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:477
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:288
grpc::CallbackServerContext
Definition: server_context.h:567
grpc::ClientContext::set_deadline
void set_deadline(const T &deadline)
Set the deadline for the client call.
Definition: client_context.h:265
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:402
grpc::PropagationOptions
Options for ClientContext::FromServerContext specifying which traits from the ServerContext to propag...
Definition: client_context.h:125
propagation_bits.h
auth_context.h
grpc::ServerContextBase
Base class of ServerContext. Experimental until callback API is final.
Definition: server_context.h:125
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:241
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:302
grpc::experimental::ServerContextBase
::grpc::ServerContextBase ServerContextBase
Definition: server_context.h:102
grpc::PropagationOptions::PropagationOptions
PropagationOptions()
Definition: client_context.h:127
grpc::ClientContext::set_idempotent
void set_idempotent(bool idempotent)
EXPERIMENTAL: Indicate that this request is idempotent.
Definition: client_context.h:275
grpc::PropagationOptions::disable_census_stats_propagation
PropagationOptions & disable_census_stats_propagation()
Definition: client_context.h:144
grpc::ClientContext::set_census_context
void set_census_context(struct census_context *ccp)
Sets the census context.
Definition: client_context.h:378
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:154
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:253
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:103
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:479
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:484
grpc::ClientContext
A ClientContext allows the person implementing a service client to:
Definition: client_context.h:191
GRPC_INITIAL_METADATA_CACHEABLE_REQUEST
#define GRPC_INITIAL_METADATA_CACHEABLE_REQUEST
Signal that the call is cacheable.
Definition: grpc_types.h:481
grpc::PropagationOptions::disable_deadline_propagation
PropagationOptions & disable_deadline_propagation()
Definition: client_context.h:134
GRPC_INITIAL_METADATA_CORKED
#define GRPC_INITIAL_METADATA_CORKED
Signal that the initial metadata should be corked.
Definition: grpc_types.h:486
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:72
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:297
grpc::internal::RpcMethod::RpcType
RpcType
Definition: rpc_method.h:31
grpc::PropagationOptions::enable_cancellation_propagation
PropagationOptions & enable_cancellation_propagation()
Definition: client_context.h:159
client_interceptor.h
grpc::PropagationOptions::enable_deadline_propagation
PropagationOptions & enable_deadline_propagation()
Definition: client_context.h:129
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:294
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:169
grpc::PropagationOptions::enable_census_tracing_propagation
PropagationOptions & enable_census_tracing_propagation()
Definition: client_context.h:149
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:360
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:400
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:280
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:312
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:139
slice.h
grpc::internal::Mutex
Definition: sync.h:47
grpc::PropagationOptions::disable_cancellation_propagation
PropagationOptions & disable_cancellation_propagation()
Definition: client_context.h:164
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:336
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:306
grpc::ClientContext::debug_error_string
std::string debug_error_string() const
EXPERIMENTAL debugging API.
Definition: client_context.h:417
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:381
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:410
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