GRPC C++  1.23.0
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 <mutex>
40 #include <string>
41 
56 
57 struct census_context;
58 struct grpc_call;
59 
60 namespace grpc {
61 
62 class ChannelInterface;
63 
64 namespace internal {
65 class RpcMethod;
66 template <class InputMessage, class OutputMessage>
67 class BlockingUnaryCallImpl;
68 class CallOpClientRecvStatus;
69 class CallOpRecvInitialMetadata;
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 } // namespace internal
89 
90 class CallCredentials;
91 class Channel;
92 class CompletionQueue;
93 class ServerContext;
94 template <class R>
95 class ClientReader;
96 template <class W>
97 class ClientWriter;
98 template <class W, class R>
99 class ClientReaderWriter;
100 template <class R>
101 class ClientAsyncReader;
102 template <class W>
103 class ClientAsyncWriter;
104 template <class W, class R>
106 template <class R>
108 
115  public:
117 
119  propagate_ |= GRPC_PROPAGATE_DEADLINE;
120  return *this;
121  }
122 
124  propagate_ &= ~GRPC_PROPAGATE_DEADLINE;
125  return *this;
126  }
127 
130  return *this;
131  }
132 
135  return *this;
136  }
137 
140  return *this;
141  }
142 
145  return *this;
146  }
147 
149  propagate_ |= GRPC_PROPAGATE_CANCELLATION;
150  return *this;
151  }
152 
154  propagate_ &= ~GRPC_PROPAGATE_CANCELLATION;
155  return *this;
156  }
157 
158  uint32_t c_bitmask() const { return propagate_; }
159 
160  private:
161  uint32_t propagate_;
162 };
163 
181  public:
182  ClientContext();
183  ~ClientContext();
184 
195  static std::unique_ptr<ClientContext> FromServerContext(
196  const grpc_impl::ServerContext& server_context,
198 
216  void AddMetadata(const grpc::string& meta_key,
217  const grpc::string& meta_value);
218 
227  const std::multimap<grpc::string_ref, grpc::string_ref>&
229  GPR_CODEGEN_ASSERT(initial_metadata_received_);
230  return *recv_initial_metadata_.map();
231  }
232 
239  const std::multimap<grpc::string_ref, grpc::string_ref>&
241  // TODO(yangg) check finished
242  return *trailing_metadata_.map();
243  }
244 
251  template <typename T>
252  void set_deadline(const T& deadline) {
253  grpc::TimePoint<T> deadline_tp(deadline);
254  deadline_ = deadline_tp.raw_time();
255  }
256 
262  void set_idempotent(bool idempotent) { idempotent_ = idempotent; }
263 
267  void set_cacheable(bool cacheable) { cacheable_ = cacheable; }
268 
275  void set_wait_for_ready(bool wait_for_ready) {
276  wait_for_ready_ = wait_for_ready;
277  wait_for_ready_explicitly_set_ = true;
278  }
279 
281  void set_fail_fast(bool fail_fast) { set_wait_for_ready(!fail_fast); }
282 
284  std::chrono::system_clock::time_point deadline() const {
285  return grpc::Timespec2Timepoint(deadline_);
286  }
287 
289  gpr_timespec raw_deadline() const { return deadline_; }
290 
293  void set_authority(const grpc::string& authority) { authority_ = authority; }
294 
298  std::shared_ptr<const grpc::AuthContext> auth_context() const {
299  if (auth_context_.get() == nullptr) {
300  auth_context_ = grpc::CreateAuthContext(call_);
301  }
302  return auth_context_;
303  }
304 
314  const std::shared_ptr<grpc_impl::CallCredentials>& creds) {
315  creds_ = creds;
316  }
317 
322  return compression_algorithm_;
323  }
324 
328  void set_compression_algorithm(grpc_compression_algorithm algorithm);
329 
340  void set_initial_metadata_corked(bool corked) {
341  initial_metadata_corked_ = corked;
342  }
343 
351  grpc::string peer() const;
352 
354  void set_census_context(struct census_context* ccp) { census_context_ = ccp; }
355  struct census_context* census_context() const {
356  return census_context_;
357  }
358 
368  void TryCancel();
369 
375  public:
376  virtual ~GlobalCallbacks() {}
377  virtual void DefaultConstructor(ClientContext* context) = 0;
378  virtual void Destructor(ClientContext* context) = 0;
379  };
380  static void SetGlobalCallbacks(GlobalCallbacks* callbacks);
381 
384  grpc_call* c_call() { return call_; }
385 
391  grpc::string debug_error_string() const { return debug_error_string_; }
392 
393  private:
394  // Disallow copy and assign.
396  ClientContext& operator=(const ClientContext&);
397 
398  friend class ::grpc::testing::InteropClientContextInspector;
399  friend class ::grpc::internal::CallOpClientRecvStatus;
400  friend class ::grpc::internal::CallOpRecvInitialMetadata;
402  template <class R>
404  template <class W>
406  template <class W, class R>
408  template <class R>
410  template <class W>
412  template <class W, class R>
414  template <class R>
416  template <class InputMessage, class OutputMessage>
417  friend class ::grpc::internal::BlockingUnaryCallImpl;
418  template <class InputMessage, class OutputMessage>
419  friend class ::grpc_impl::internal::CallbackUnaryCallImpl;
420  template <class Request, class Response>
421  friend class ::grpc_impl::internal::ClientCallbackReaderWriterImpl;
422  template <class Response>
423  friend class ::grpc_impl::internal::ClientCallbackReaderImpl;
424  template <class Request>
425  friend class ::grpc_impl::internal::ClientCallbackWriterImpl;
426  friend class ::grpc_impl::internal::ClientCallbackUnaryImpl;
427 
428  // Used by friend class CallOpClientRecvStatus
429  void set_debug_error_string(const grpc::string& debug_error_string) {
430  debug_error_string_ = debug_error_string;
431  }
432 
433  grpc_call* call() const { return call_; }
434  void set_call(grpc_call* call,
435  const std::shared_ptr<::grpc_impl::Channel>& channel);
436 
437  grpc::experimental::ClientRpcInfo* set_client_rpc_info(
438  const char* method, grpc::internal::RpcMethod::RpcType type,
439  grpc::ChannelInterface* channel,
440  const std::vector<std::unique_ptr<
442  size_t interceptor_pos) {
443  rpc_info_ = grpc::experimental::ClientRpcInfo(this, type, method, channel);
444  rpc_info_.RegisterInterceptors(creators, interceptor_pos);
445  return &rpc_info_;
446  }
447 
448  uint32_t initial_metadata_flags() const {
449  return (idempotent_ ? GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST : 0) |
450  (wait_for_ready_ ? GRPC_INITIAL_METADATA_WAIT_FOR_READY : 0) |
451  (cacheable_ ? GRPC_INITIAL_METADATA_CACHEABLE_REQUEST : 0) |
452  (wait_for_ready_explicitly_set_
454  : 0) |
455  (initial_metadata_corked_ ? GRPC_INITIAL_METADATA_CORKED : 0);
456  }
457 
458  grpc::string authority() { return authority_; }
459 
460  void SendCancelToInterceptors();
461 
462  bool initial_metadata_received_;
463  bool wait_for_ready_;
464  bool wait_for_ready_explicitly_set_;
465  bool idempotent_;
466  bool cacheable_;
467  std::shared_ptr<::grpc_impl::Channel> channel_;
469  grpc_call* call_;
470  bool call_canceled_;
471  gpr_timespec deadline_;
472  grpc::string authority_;
473  std::shared_ptr<grpc_impl::CallCredentials> creds_;
474  mutable std::shared_ptr<const grpc::AuthContext> auth_context_;
475  struct census_context* census_context_;
476  std::multimap<grpc::string, grpc::string> send_initial_metadata_;
477  mutable grpc::internal::MetadataMap recv_initial_metadata_;
478  mutable grpc::internal::MetadataMap trailing_metadata_;
479 
480  grpc_call* propagate_from_call_;
481  PropagationOptions propagation_options_;
482 
483  grpc_compression_algorithm compression_algorithm_;
484  bool initial_metadata_corked_;
485 
486  grpc::string debug_error_string_;
487 
489 };
490 
491 } // namespace grpc_impl
492 
493 #endif // GRPCPP_IMPL_CODEGEN_CLIENT_CONTEXT_IMPL_H
void set_idempotent(bool idempotent)
EXPERIMENTAL: Indicate that this request is idempotent.
Definition: client_context_impl.h:262
struct census_context * census_context() const
Definition: client_context_impl.h:355
struct grpc_call grpc_call
A Call represents an RPC.
Definition: grpc_types.h:70
std::shared_ptr< const AuthContext > CreateAuthContext(grpc_call *call)
::grpc_impl::ClientAsyncReader< R > ClientAsyncReader
Definition: async_stream.h:43
::grpc_impl::ClientContext ClientContext
Definition: client_context.h:26
std::shared_ptr< const grpc::AuthContext > auth_context() const
Return the authentication context for this client call.
Definition: client_context_impl.h:298
PropagationOptions & disable_deadline_propagation()
Definition: client_context_impl.h:123
grpc_call * c_call()
Should be used for framework-level extensions only.
Definition: client_context_impl.h:384
PropagationOptions & enable_deadline_propagation()
Definition: client_context_impl.h:118
#define GPR_CODEGEN_ASSERT(x)
Codegen specific version of GPR_ASSERT.
Definition: core_codegen_interface.h:145
Synchronous (blocking) client-side API for bi-directional streaming RPCs, where the outgoing message ...
Definition: channel_interface.h:35
void set_cacheable(bool cacheable)
EXPERIMENTAL: Set this request to be cacheable.
Definition: client_context_impl.h:267
std::string string
Definition: config.h:35
PropagationOptions & enable_cancellation_propagation()
Definition: client_context_impl.h:148
grpc::string debug_error_string() const
EXPERIMENTAL debugging API.
Definition: client_context_impl.h:391
A call credentials object encapsulates the state needed by a client to authenticate with a server for...
Definition: credentials_impl.h:110
Synchronous (blocking) client-side API for doing server-streaming RPCs, where the stream of messages ...
Definition: channel_interface.h:31
Definition: metadata_map.h:33
PropagationOptions()
Definition: client_context_impl.h:116
PropagationOptions & enable_census_tracing_propagation()
Definition: client_context_impl.h:138
PropagationOptions & enable_census_stats_propagation()
Definition: client_context_impl.h:128
::grpc_impl::PropagationOptions PropagationOptions
Definition: client_context.h:27
Async client-side interface for bi-directional streaming, where the outgoing message stream going to ...
Definition: async_stream_impl.h:513
::grpc_impl::ClientAsyncReaderWriter< W, R > ClientAsyncReaderWriter
Definition: async_stream.h:56
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:228
Options for ClientContext::FromServerContext specifying which traits from the ServerContext to propag...
Definition: client_context_impl.h:114
grpc_compression_algorithm compression_algorithm() const
Return the compression algorithm the client call will request be used.
Definition: client_context_impl.h:321
#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:456
gpr_timespec raw_time()
Definition: time.h:43
RpcType
Definition: rpc_method.h:31
If you are trying to use CompletionQueue::AsyncNext with a time class that isn&#39;t either gpr_timespec ...
Definition: time.h:40
A ServerContext allows the person implementing a service handler to:
Definition: server_context_impl.h:118
#define GRPC_INITIAL_METADATA_CORKED
Signal that the initial metadata should be corked.
Definition: grpc_types.h:458
PropagationOptions & disable_cancellation_propagation()
Definition: client_context_impl.h:153
void set_initial_metadata_corked(bool corked)
Flag whether the initial metadata should be corked.
Definition: client_context_impl.h:340
Async client-side API for doing server-streaming RPCs, where the incoming message stream coming from ...
Definition: async_stream_impl.h:197
gpr_timespec raw_deadline() const
Return a gpr_timespec representation of the client call&#39;s deadline.
Definition: client_context_impl.h:289
PropagationOptions & disable_census_tracing_propagation()
Definition: client_context_impl.h:143
::grpc_impl::Channel Channel
Definition: channel.h:26
#define GRPC_PROPAGATE_CENSUS_TRACING_CONTEXT
Definition: propagation_bits.h:34
std::chrono::system_clock::time_point deadline() const
Return the deadline for the client call.
Definition: client_context_impl.h:284
grpc_compression_algorithm
The various compression algorithms supported by gRPC (not sorted by compression level) ...
Definition: compression_types.h:57
Definition: sync.h:47
Async API for client-side unary RPCs, where the message response received from the server is of type ...
Definition: async_unary_call_impl.h:95
::grpc_impl::ClientReaderWriter< W, R > ClientReaderWriter
Definition: sync_stream.h:69
This header provides an object that reads bytes directly from a grpc::ByteBuffer, via the ZeroCopyInp...
Definition: alarm.h:24
Codegen interface for grpc::Channel.
Definition: channel_interface.h:70
#define GRPC_INITIAL_METADATA_CACHEABLE_REQUEST
Signal that the call is cacheable.
Definition: grpc_types.h:453
void set_authority(const grpc::string &authority)
Set the per call authority header (see https://tools.ietf.org/html/rfc7540#section-8.1.2.3).
Definition: client_context_impl.h:293
::grpc_impl::ClientWriter< W > ClientWriter
Definition: sync_stream.h:62
An Alarm posts the user-provided tag to its associated completion queue or invokes the user-provided ...
Definition: alarm_impl.h:33
#define GRPC_INITIAL_METADATA_WAIT_FOR_READY
Signal that the call should not return UNAVAILABLE before it has started.
Definition: grpc_types.h:451
void set_deadline(const T &deadline)
Set the deadline for the client call.
Definition: client_context_impl.h:252
void set_fail_fast(bool fail_fast)
DEPRECATED: Use set_wait_for_ready() instead.
Definition: client_context_impl.h:281
void set_credentials(const std::shared_ptr< grpc_impl::CallCredentials > &creds)
Set credentials for the client call.
Definition: client_context_impl.h:313
#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
Async API on the client side for doing client-streaming RPCs, where the outgoing message stream going...
Definition: async_stream_impl.h:346
Global Callbacks.
Definition: client_context_impl.h:374
#define GRPC_PROPAGATE_CENSUS_STATS_CONTEXT
Propagate census context.
Definition: propagation_bits.h:33
void set_census_context(struct census_context *ccp)
Get and set census context.
Definition: client_context_impl.h:354
A thin wrapper around grpc_completion_queue (see src/core/lib/surface/completion_queue.h).
Definition: completion_queue_impl.h:101
A ClientContext allows the person implementing a service client to:
Definition: client_context_impl.h:180
Channels represent a connection to an endpoint. Created by CreateChannel.
Definition: channel_impl.h:54
::grpc_impl::ClientAsyncWriter< W > ClientAsyncWriter
Definition: async_stream.h:49
Analogous to struct timespec.
Definition: gpr_types.h:47
std::chrono::system_clock::time_point Timespec2Timepoint(gpr_timespec t)
#define GRPC_PROPAGATE_DEADLINE
Propagation bits: this can be bitwise or-ed to form propagation_mask for grpc_call.
Definition: propagation_bits.h:31
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:240
Synchronous (blocking) client-side API for doing client-streaming RPCs, where the outgoing message st...
Definition: channel_interface.h:33
PropagationOptions & disable_census_stats_propagation()
Definition: client_context_impl.h:133
virtual ~GlobalCallbacks()
Definition: client_context_impl.h:376
::grpc_impl::ClientReader< R > ClientReader
Definition: sync_stream.h:56
uint32_t c_bitmask() const
Definition: client_context_impl.h:158
Definition: client_interceptor.h:69
grpc_impl::ClientAsyncResponseReader< R > ClientAsyncResponseReader
Definition: async_unary_call.h:31
void set_wait_for_ready(bool wait_for_ready)
EXPERIMENTAL: Trigger wait-for-ready or not on this request.
Definition: client_context_impl.h:275
#define GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST
Initial metadata flags.
Definition: grpc_types.h:449
#define GRPC_PROPAGATE_CANCELLATION
Propagate cancellation.
Definition: propagation_bits.h:36