GRPC C++  1.32.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_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_impl {
60 
61 namespace internal {
62 template <class InputMessage, class OutputMessage>
63 class CallbackUnaryCallImpl;
64 template <class Request, class Response>
65 class ClientCallbackReaderWriterImpl;
66 template <class Response>
67 class ClientCallbackReaderImpl;
68 template <class Request>
69 class ClientCallbackWriterImpl;
70 class ClientCallbackUnaryImpl;
71 class ClientContextAccessor;
72 } // namespace internal
73 
74 class ServerContext;
75 template <class R>
76 class ClientReader;
77 template <class W>
78 class ClientWriter;
79 template <class W, class R>
80 class ClientReaderWriter;
81 template <class R>
82 class ClientAsyncReader;
83 template <class W>
84 class ClientAsyncWriter;
85 template <class W, class R>
87 template <class R>
89 
90 class ServerContextBase;
92 } // namespace grpc_impl
93 
94 namespace grpc {
95 
96 namespace testing {
97 class InteropClientContextInspector;
98 } // namespace testing
99 
100 namespace internal {
101 class RpcMethod;
102 template <class InputMessage, class OutputMessage>
103 class BlockingUnaryCallImpl;
104 class CallOpClientRecvStatus;
105 class CallOpRecvInitialMetadata;
106 class ServerContextImpl;
107 template <class InputMessage, class OutputMessage>
109 template <class Request, class Response>
111 template <class Response>
113 template <class Request>
115 class ClientCallbackUnaryImpl;
116 class ClientContextAccessor;
117 } // namespace internal
118 
119 class CallCredentials;
120 class Channel;
121 class ChannelInterface;
122 class CompletionQueue;
123 
130  public:
132 
134  propagate_ |= GRPC_PROPAGATE_DEADLINE;
135  return *this;
136  }
137 
139  propagate_ &= ~GRPC_PROPAGATE_DEADLINE;
140  return *this;
141  }
142 
145  return *this;
146  }
147 
150  return *this;
151  }
152 
155  return *this;
156  }
157 
160  return *this;
161  }
162 
164  propagate_ |= GRPC_PROPAGATE_CANCELLATION;
165  return *this;
166  }
167 
169  propagate_ &= ~GRPC_PROPAGATE_CANCELLATION;
170  return *this;
171  }
172 
173  uint32_t c_bitmask() const { return propagate_; }
174 
175  private:
176  uint32_t propagate_;
177 };
178 
196  public:
197  ClientContext();
198  ~ClientContext();
199 
210  static std::unique_ptr<ClientContext> FromServerContext(
211  const grpc_impl::ServerContext& server_context,
213  static std::unique_ptr<ClientContext> FromCallbackServerContext(
214  const grpc_impl::CallbackServerContext& server_context,
216 
234  void AddMetadata(const std::string& meta_key, const std::string& meta_value);
235 
244  const std::multimap<grpc::string_ref, grpc::string_ref>&
246  GPR_CODEGEN_ASSERT(initial_metadata_received_);
247  return *recv_initial_metadata_.map();
248  }
249 
256  const std::multimap<grpc::string_ref, grpc::string_ref>&
258  // TODO(yangg) check finished
259  return *trailing_metadata_.map();
260  }
261 
268  template <typename T>
269  void set_deadline(const T& deadline) {
270  grpc::TimePoint<T> deadline_tp(deadline);
271  deadline_ = deadline_tp.raw_time();
272  }
273 
279  void set_idempotent(bool idempotent) { idempotent_ = idempotent; }
280 
284  void set_cacheable(bool cacheable) { cacheable_ = cacheable; }
285 
292  void set_wait_for_ready(bool wait_for_ready) {
293  wait_for_ready_ = wait_for_ready;
294  wait_for_ready_explicitly_set_ = true;
295  }
296 
298  void set_fail_fast(bool fail_fast) { set_wait_for_ready(!fail_fast); }
299 
301  std::chrono::system_clock::time_point deadline() const {
302  return grpc::Timespec2Timepoint(deadline_);
303  }
304 
306  gpr_timespec raw_deadline() const { return deadline_; }
307 
310  void set_authority(const std::string& authority) { authority_ = authority; }
311 
316  std::shared_ptr<const grpc::AuthContext> auth_context() const {
317  if (auth_context_.get() == nullptr) {
318  auth_context_ = grpc::CreateAuthContext(call_);
319  }
320  return auth_context_;
321  }
322 
333  void set_credentials(const std::shared_ptr<grpc::CallCredentials>& creds);
334 
340  std::shared_ptr<grpc::CallCredentials> credentials() { return creds_; }
341 
346  return compression_algorithm_;
347  }
348 
353 
364  void set_initial_metadata_corked(bool corked) {
365  initial_metadata_corked_ = corked;
366  }
367 
376  std::string peer() const;
377 
382  void set_census_context(struct census_context* ccp) { census_context_ = ccp; }
383 
385  struct census_context* census_context() const {
386  return census_context_;
387  }
388 
398  void TryCancel();
399 
405  public:
406  virtual ~GlobalCallbacks() {}
407  virtual void DefaultConstructor(ClientContext* context) = 0;
408  virtual void Destructor(ClientContext* context) = 0;
409  };
410  static void SetGlobalCallbacks(GlobalCallbacks* callbacks);
411 
414  grpc_call* c_call() { return call_; }
415 
421  std::string debug_error_string() const { return debug_error_string_; }
422 
423  private:
424  // Disallow copy and assign.
426  ClientContext& operator=(const ClientContext&);
427 
428  friend class ::grpc::testing::InteropClientContextInspector;
429  friend class ::grpc::internal::CallOpClientRecvStatus;
430  friend class ::grpc::internal::CallOpRecvInitialMetadata;
431  friend class ::grpc::Channel;
432  template <class R>
434  template <class W>
436  template <class W, class R>
438  template <class R>
440  template <class W>
442  template <class W, class R>
444  template <class R>
446  template <class InputMessage, class OutputMessage>
447  friend class ::grpc::internal::BlockingUnaryCallImpl;
448  template <class InputMessage, class OutputMessage>
449  friend class ::grpc_impl::internal::CallbackUnaryCallImpl;
450  template <class Request, class Response>
451  friend class ::grpc_impl::internal::ClientCallbackReaderWriterImpl;
452  template <class Response>
453  friend class ::grpc_impl::internal::ClientCallbackReaderImpl;
454  template <class Request>
455  friend class ::grpc_impl::internal::ClientCallbackWriterImpl;
456  friend class ::grpc_impl::internal::ClientCallbackUnaryImpl;
457  friend class ::grpc_impl::internal::ClientContextAccessor;
458 
459  // Used by friend class CallOpClientRecvStatus
460  void set_debug_error_string(const std::string& debug_error_string) {
461  debug_error_string_ = debug_error_string;
462  }
463 
464  grpc_call* call() const { return call_; }
465  void set_call(grpc_call* call,
466  const std::shared_ptr<::grpc::Channel>& channel);
467 
468  grpc::experimental::ClientRpcInfo* set_client_rpc_info(
469  const char* method, grpc::internal::RpcMethod::RpcType type,
470  grpc::ChannelInterface* channel,
471  const std::vector<std::unique_ptr<
473  size_t interceptor_pos) {
474  rpc_info_ = grpc::experimental::ClientRpcInfo(this, type, method, channel);
475  rpc_info_.RegisterInterceptors(creators, interceptor_pos);
476  return &rpc_info_;
477  }
478 
479  uint32_t initial_metadata_flags() const {
480  return (idempotent_ ? GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST : 0) |
481  (wait_for_ready_ ? GRPC_INITIAL_METADATA_WAIT_FOR_READY : 0) |
482  (cacheable_ ? GRPC_INITIAL_METADATA_CACHEABLE_REQUEST : 0) |
483  (wait_for_ready_explicitly_set_
485  : 0) |
486  (initial_metadata_corked_ ? GRPC_INITIAL_METADATA_CORKED : 0);
487  }
488 
489  std::string authority() { return authority_; }
490 
491  void SendCancelToInterceptors();
492 
493  static std::unique_ptr<ClientContext> FromInternalServerContext(
494  const grpc_impl::ServerContextBase& server_context,
495  PropagationOptions options);
496 
497  bool initial_metadata_received_;
498  bool wait_for_ready_;
499  bool wait_for_ready_explicitly_set_;
500  bool idempotent_;
501  bool cacheable_;
502  std::shared_ptr<::grpc::Channel> channel_;
504  grpc_call* call_;
505  bool call_canceled_;
506  gpr_timespec deadline_;
507  grpc::string authority_;
508  std::shared_ptr<grpc::CallCredentials> creds_;
509  mutable std::shared_ptr<const grpc::AuthContext> auth_context_;
510  struct census_context* census_context_;
511  std::multimap<std::string, std::string> send_initial_metadata_;
512  mutable grpc::internal::MetadataMap recv_initial_metadata_;
513  mutable grpc::internal::MetadataMap trailing_metadata_;
514 
515  grpc_call* propagate_from_call_;
516  PropagationOptions propagation_options_;
517 
518  grpc_compression_algorithm compression_algorithm_;
519  bool initial_metadata_corked_;
520 
521  std::string debug_error_string_;
522 
524 };
525 
526 } // namespace grpc
527 
528 #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:345
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::ClientContext::FromCallbackServerContext
static std::unique_ptr< ClientContext > FromCallbackServerContext(const grpc_impl::CallbackServerContext &server_context, PropagationOptions options=PropagationOptions())
grpc::internal::ClientCallbackReaderWriterImpl
Definition: client_context.h:110
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: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::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:292
grpc::ClientAsyncWriter
::grpc_impl::ClientAsyncWriter< W > ClientAsyncWriter
Definition: async_stream.h:49
grpc::ClientContext::set_deadline
void set_deadline(const T &deadline)
Set the deadline for the client call.
Definition: client_context.h:269
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:406
grpc::PropagationOptions
Options for ClientContext::FromServerContext specifying which traits from the ServerContext to propag...
Definition: client_context.h:129
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
auth_context.h
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:245
core_codegen_interface.h
grpc_impl::ServerContextBase
Base class of ServerContext. Experimental until callback API is final.
Definition: server_context_impl.h:123
grpc::ClientContext::raw_deadline
gpr_timespec raw_deadline() const
Return a gpr_timespec representation of the client call's deadline.
Definition: client_context.h:306
grpc_impl::CallbackServerContext
Definition: server_context_impl.h:560
grpc::PropagationOptions::PropagationOptions
PropagationOptions()
Definition: client_context.h:131
grpc::ClientContext::set_idempotent
void set_idempotent(bool idempotent)
EXPERIMENTAL: Indicate that this request is idempotent.
Definition: client_context.h:279
grpc::PropagationOptions::disable_census_stats_propagation
PropagationOptions & disable_census_stats_propagation()
Definition: client_context.h:148
grpc::ClientContext::set_census_context
void set_census_context(struct census_context *ccp)
Sets the census context.
Definition: client_context.h:382
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:158
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:257
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_impl::ServerContext
A ServerContext or CallbackServerContext allows the code implementing a service handler to:
Definition: server_context_impl.h:520
grpc::CallCredentials
A call credentials object encapsulates the state needed by a client to authenticate with a server for...
Definition: credentials.h:110
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::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:489
grpc::ClientContext
A ClientContext allows the person implementing a service client to:
Definition: client_context.h:195
GRPC_INITIAL_METADATA_CACHEABLE_REQUEST
#define GRPC_INITIAL_METADATA_CACHEABLE_REQUEST
Signal that the call is cacheable.
Definition: grpc_types.h:486
grpc::PropagationOptions::disable_deadline_propagation
PropagationOptions & disable_deadline_propagation()
Definition: client_context.h:138
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::ClientContext::TryCancel
void TryCancel()
Send a best-effort out-of-band cancel on the call associated with this client context.
grpc::internal::CallbackUnaryCallImpl
Definition: client_context.h:108
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
grpc::ClientContext::SetGlobalCallbacks
static void SetGlobalCallbacks(GlobalCallbacks *callbacks)
sync.h
grpc::internal::ClientCallbackWriterImpl
Definition: client_context.h:114
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:301
grpc::internal::RpcMethod::RpcType
RpcType
Definition: rpc_method.h:31
grpc::PropagationOptions::enable_cancellation_propagation
PropagationOptions & enable_cancellation_propagation()
Definition: client_context.h:163
grpc::experimental::ServerContextBase
::grpc_impl::ServerContextBase ServerContextBase
Definition: server_context.h:36
client_interceptor.h
grpc::PropagationOptions::enable_deadline_propagation
PropagationOptions & enable_deadline_propagation()
Definition: client_context.h:133
compression_types.h
grpc::ClientAsyncResponseReader
grpc_impl::ClientAsyncResponseReader< R > ClientAsyncResponseReader
Definition: async_unary_call.h:31
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:298
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:173
grpc::PropagationOptions::enable_census_tracing_propagation
PropagationOptions & enable_census_tracing_propagation()
Definition: client_context.h:153
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:364
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:404
create_auth_context.h
grpc::ClientReader
::grpc_impl::ClientReader< R > ClientReader
Definition: sync_stream.h:56
grpc::ClientContext::set_cacheable
void set_cacheable(bool cacheable)
EXPERIMENTAL: Set this request to be cacheable.
Definition: client_context.h:284
grpc::ClientAsyncReaderWriter
::grpc_impl::ClientAsyncReaderWriter< W, R > ClientAsyncReaderWriter
Definition: async_stream.h:56
grpc::CompletionQueue
A thin wrapper around grpc_completion_queue (see src/core/lib/surface/completion_queue....
Definition: completion_queue.h:99
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::Channel
Channels represent a connection to an endpoint. Created by CreateChannel.
Definition: channel.h:54
grpc::internal::ClientCallbackReaderImpl
Definition: client_context.h:112
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:316
grpc::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::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
grpc::PropagationOptions::enable_census_stats_propagation
PropagationOptions & enable_census_stats_propagation()
Definition: client_context.h:143
slice.h
grpc::internal::Mutex
Definition: sync.h:47
grpc::PropagationOptions::disable_cancellation_propagation
PropagationOptions & disable_cancellation_propagation()
Definition: client_context.h:168
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:340
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:310
grpc::ClientContext::debug_error_string
std::string debug_error_string() const
EXPERIMENTAL debugging API.
Definition: client_context.h:421
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:385
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:414
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