GRPC C++  1.22.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 class CallOpClientRecvStatus;
67 class CallOpRecvInitialMetadata;
68 template <class InputMessage, class OutputMessage>
69 class BlockingUnaryCallImpl;
70 template <class InputMessage, class OutputMessage>
71 class CallbackUnaryCallImpl;
72 template <class Request, class Response>
73 class ClientCallbackReaderWriterImpl;
74 template <class Response>
75 class ClientCallbackReaderImpl;
76 template <class Request>
77 class ClientCallbackWriterImpl;
78 class ClientCallbackUnaryImpl;
79 } // namespace internal
80 
81 template <class R>
82 class ClientReader;
83 template <class W>
84 class ClientWriter;
85 template <class W, class R>
86 class ClientReaderWriter;
87 template <class R>
88 class ClientAsyncReader;
89 template <class W>
90 class ClientAsyncWriter;
91 template <class W, class R>
92 class ClientAsyncReaderWriter;
93 template <class R>
94 class ClientAsyncResponseReader;
95 
96 namespace testing {
97 class InteropClientContextInspector;
98 } // namespace testing
99 } // namespace grpc
100 namespace grpc_impl {
101 
102 class CallCredentials;
103 class Channel;
104 class CompletionQueue;
105 class ServerContext;
106 
113  public:
115 
117  propagate_ |= GRPC_PROPAGATE_DEADLINE;
118  return *this;
119  }
120 
122  propagate_ &= ~GRPC_PROPAGATE_DEADLINE;
123  return *this;
124  }
125 
128  return *this;
129  }
130 
133  return *this;
134  }
135 
138  return *this;
139  }
140 
143  return *this;
144  }
145 
147  propagate_ |= GRPC_PROPAGATE_CANCELLATION;
148  return *this;
149  }
150 
152  propagate_ &= ~GRPC_PROPAGATE_CANCELLATION;
153  return *this;
154  }
155 
156  uint32_t c_bitmask() const { return propagate_; }
157 
158  private:
159  uint32_t propagate_;
160 };
161 
179  public:
180  ClientContext();
181  ~ClientContext();
182 
193  static std::unique_ptr<ClientContext> FromServerContext(
194  const grpc_impl::ServerContext& server_context,
196 
214  void AddMetadata(const grpc::string& meta_key,
215  const grpc::string& meta_value);
216 
225  const std::multimap<grpc::string_ref, grpc::string_ref>&
227  GPR_CODEGEN_ASSERT(initial_metadata_received_);
228  return *recv_initial_metadata_.map();
229  }
230 
237  const std::multimap<grpc::string_ref, grpc::string_ref>&
239  // TODO(yangg) check finished
240  return *trailing_metadata_.map();
241  }
242 
249  template <typename T>
250  void set_deadline(const T& deadline) {
251  grpc::TimePoint<T> deadline_tp(deadline);
252  deadline_ = deadline_tp.raw_time();
253  }
254 
260  void set_idempotent(bool idempotent) { idempotent_ = idempotent; }
261 
265  void set_cacheable(bool cacheable) { cacheable_ = cacheable; }
266 
273  void set_wait_for_ready(bool wait_for_ready) {
274  wait_for_ready_ = wait_for_ready;
275  wait_for_ready_explicitly_set_ = true;
276  }
277 
279  void set_fail_fast(bool fail_fast) { set_wait_for_ready(!fail_fast); }
280 
282  std::chrono::system_clock::time_point deadline() const {
283  return grpc::Timespec2Timepoint(deadline_);
284  }
285 
287  gpr_timespec raw_deadline() const { return deadline_; }
288 
291  void set_authority(const grpc::string& authority) { authority_ = authority; }
292 
296  std::shared_ptr<const grpc::AuthContext> auth_context() const {
297  if (auth_context_.get() == nullptr) {
298  auth_context_ = grpc::CreateAuthContext(call_);
299  }
300  return auth_context_;
301  }
302 
312  const std::shared_ptr<grpc_impl::CallCredentials>& creds) {
313  creds_ = creds;
314  }
315 
320  return compression_algorithm_;
321  }
322 
326  void set_compression_algorithm(grpc_compression_algorithm algorithm);
327 
338  void set_initial_metadata_corked(bool corked) {
339  initial_metadata_corked_ = corked;
340  }
341 
349  grpc::string peer() const;
350 
352  void set_census_context(struct census_context* ccp) { census_context_ = ccp; }
353  struct census_context* census_context() const {
354  return census_context_;
355  }
356 
366  void TryCancel();
367 
373  public:
374  virtual ~GlobalCallbacks() {}
375  virtual void DefaultConstructor(ClientContext* context) = 0;
376  virtual void Destructor(ClientContext* context) = 0;
377  };
378  static void SetGlobalCallbacks(GlobalCallbacks* callbacks);
379 
382  grpc_call* c_call() { return call_; }
383 
389  grpc::string debug_error_string() const { return debug_error_string_; }
390 
391  private:
392  // Disallow copy and assign.
394  ClientContext& operator=(const ClientContext&);
395 
396  friend class ::grpc::testing::InteropClientContextInspector;
397  friend class ::grpc::internal::CallOpClientRecvStatus;
398  friend class ::grpc::internal::CallOpRecvInitialMetadata;
400  template <class R>
401  friend class ::grpc::ClientReader;
402  template <class W>
403  friend class ::grpc::ClientWriter;
404  template <class W, class R>
405  friend class ::grpc::ClientReaderWriter;
406  template <class R>
407  friend class ::grpc::ClientAsyncReader;
408  template <class W>
409  friend class ::grpc::ClientAsyncWriter;
410  template <class W, class R>
411  friend class ::grpc::ClientAsyncReaderWriter;
412  template <class R>
413  friend class ::grpc::ClientAsyncResponseReader;
414  template <class InputMessage, class OutputMessage>
415  friend class ::grpc::internal::BlockingUnaryCallImpl;
416  template <class InputMessage, class OutputMessage>
417  friend class ::grpc::internal::CallbackUnaryCallImpl;
418  template <class Request, class Response>
419  friend class ::grpc::internal::ClientCallbackReaderWriterImpl;
420  template <class Response>
421  friend class ::grpc::internal::ClientCallbackReaderImpl;
422  template <class Request>
423  friend class ::grpc::internal::ClientCallbackWriterImpl;
424  friend class ::grpc::internal::ClientCallbackUnaryImpl;
425 
426  // Used by friend class CallOpClientRecvStatus
427  void set_debug_error_string(const grpc::string& debug_error_string) {
428  debug_error_string_ = debug_error_string;
429  }
430 
431  grpc_call* call() const { return call_; }
432  void set_call(grpc_call* call,
433  const std::shared_ptr<::grpc_impl::Channel>& channel);
434 
435  grpc::experimental::ClientRpcInfo* set_client_rpc_info(
436  const char* method, grpc::internal::RpcMethod::RpcType type,
437  grpc::ChannelInterface* channel,
438  const std::vector<std::unique_ptr<
440  size_t interceptor_pos) {
441  rpc_info_ = grpc::experimental::ClientRpcInfo(this, type, method, channel);
442  rpc_info_.RegisterInterceptors(creators, interceptor_pos);
443  return &rpc_info_;
444  }
445 
446  uint32_t initial_metadata_flags() const {
447  return (idempotent_ ? GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST : 0) |
448  (wait_for_ready_ ? GRPC_INITIAL_METADATA_WAIT_FOR_READY : 0) |
449  (cacheable_ ? GRPC_INITIAL_METADATA_CACHEABLE_REQUEST : 0) |
450  (wait_for_ready_explicitly_set_
452  : 0) |
453  (initial_metadata_corked_ ? GRPC_INITIAL_METADATA_CORKED : 0);
454  }
455 
456  grpc::string authority() { return authority_; }
457 
458  void SendCancelToInterceptors();
459 
460  bool initial_metadata_received_;
461  bool wait_for_ready_;
462  bool wait_for_ready_explicitly_set_;
463  bool idempotent_;
464  bool cacheable_;
465  std::shared_ptr<::grpc_impl::Channel> channel_;
467  grpc_call* call_;
468  bool call_canceled_;
469  gpr_timespec deadline_;
470  grpc::string authority_;
471  std::shared_ptr<grpc_impl::CallCredentials> creds_;
472  mutable std::shared_ptr<const grpc::AuthContext> auth_context_;
473  struct census_context* census_context_;
474  std::multimap<grpc::string, grpc::string> send_initial_metadata_;
475  mutable grpc::internal::MetadataMap recv_initial_metadata_;
476  mutable grpc::internal::MetadataMap trailing_metadata_;
477 
478  grpc_call* propagate_from_call_;
479  PropagationOptions propagation_options_;
480 
481  grpc_compression_algorithm compression_algorithm_;
482  bool initial_metadata_corked_;
483 
484  grpc::string debug_error_string_;
485 
487 };
488 
489 } // namespace grpc_impl
490 
491 #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:260
struct census_context * census_context() const
Definition: client_context_impl.h:353
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::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:296
PropagationOptions & disable_deadline_propagation()
Definition: client_context_impl.h:121
grpc_call * c_call()
Should be used for framework-level extensions only.
Definition: client_context_impl.h:382
PropagationOptions & enable_deadline_propagation()
Definition: client_context_impl.h:116
#define GPR_CODEGEN_ASSERT(x)
Codegen specific version of GPR_ASSERT.
Definition: core_codegen_interface.h:145
void set_cacheable(bool cacheable)
EXPERIMENTAL: Set this request to be cacheable.
Definition: client_context_impl.h:265
std::string string
Definition: config.h:35
PropagationOptions & enable_cancellation_propagation()
Definition: client_context_impl.h:146
grpc::string debug_error_string() const
EXPERIMENTAL debugging API.
Definition: client_context_impl.h:389
A call credentials object encapsulates the state needed by a client to authenticate with a server for...
Definition: credentials_impl.h:110
Definition: metadata_map.h:33
PropagationOptions()
Definition: client_context_impl.h:114
PropagationOptions & enable_census_tracing_propagation()
Definition: client_context_impl.h:136
PropagationOptions & enable_census_stats_propagation()
Definition: client_context_impl.h:126
::grpc_impl::PropagationOptions PropagationOptions
Definition: client_context.h:27
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:226
Options for ClientContext::FromServerContext specifying which traits from the ServerContext to propag...
Definition: client_context_impl.h:112
grpc_compression_algorithm compression_algorithm() const
Return the compression algorithm the client call will request be used.
Definition: client_context_impl.h:319
#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:447
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:114
#define GRPC_INITIAL_METADATA_CORKED
Signal that the initial metadata should be corked.
Definition: grpc_types.h:449
PropagationOptions & disable_cancellation_propagation()
Definition: client_context_impl.h:151
void set_initial_metadata_corked(bool corked)
Flag whether the initial metadata should be corked.
Definition: client_context_impl.h:338
gpr_timespec raw_deadline() const
Return a gpr_timespec representation of the client call&#39;s deadline.
Definition: client_context_impl.h:287
PropagationOptions & disable_census_tracing_propagation()
Definition: client_context_impl.h:141
::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:282
grpc_compression_algorithm
The various compression algorithms supported by gRPC (not sorted by compression level) ...
Definition: compression_types.h:57
Definition: sync.h:47
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:69
#define GRPC_INITIAL_METADATA_CACHEABLE_REQUEST
Signal that the call is cacheable.
Definition: grpc_types.h:444
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:291
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:442
void set_deadline(const T &deadline)
Set the deadline for the client call.
Definition: client_context_impl.h:250
void set_fail_fast(bool fail_fast)
DEPRECATED: Use set_wait_for_ready() instead.
Definition: client_context_impl.h:279
void set_credentials(const std::shared_ptr< grpc_impl::CallCredentials > &creds)
Set credentials for the client call.
Definition: client_context_impl.h:311
#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
Global Callbacks.
Definition: client_context_impl.h:372
#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:352
A thin wrapper around grpc_completion_queue (see src/core/lib/surface/completion_queue.h).
Definition: completion_queue_impl.h:102
A ClientContext allows the person implementing a service client to:
Definition: client_context_impl.h:178
Channels represent a connection to an endpoint. Created by CreateChannel.
Definition: channel_impl.h:54
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:238
PropagationOptions & disable_census_stats_propagation()
Definition: client_context_impl.h:131
virtual ~GlobalCallbacks()
Definition: client_context_impl.h:374
uint32_t c_bitmask() const
Definition: client_context_impl.h:156
Definition: client_interceptor.h:69
void set_wait_for_ready(bool wait_for_ready)
EXPERIMENTAL: Trigger wait-for-ready or not on this request.
Definition: client_context_impl.h:273
#define GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST
Initial metadata flags.
Definition: grpc_types.h:440
#define GRPC_PROPAGATE_CANCELLATION
Propagate cancellation.
Definition: propagation_bits.h:36