GRPC C++  1.17.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 <mutex>
40 #include <string>
41 
54 
55 struct census_context;
56 struct grpc_call;
57 
58 namespace grpc {
59 
60 class Channel;
61 class ChannelInterface;
62 class CompletionQueue;
63 class CallCredentials;
64 class ClientContext;
65 
66 namespace internal {
67 class RpcMethod;
68 class CallOpClientRecvStatus;
69 class CallOpRecvInitialMetadata;
70 template <class InputMessage, class OutputMessage>
71 class BlockingUnaryCallImpl;
72 template <class InputMessage, class OutputMessage>
73 class CallbackUnaryCallImpl;
74 } // namespace internal
75 
76 template <class R>
77 class ClientReader;
78 template <class W>
79 class ClientWriter;
80 template <class W, class R>
81 class ClientReaderWriter;
82 template <class R>
83 class ClientAsyncReader;
84 template <class W>
85 class ClientAsyncWriter;
86 template <class W, class R>
87 class ClientAsyncReaderWriter;
88 template <class R>
89 class ClientAsyncResponseReader;
90 class ServerContext;
91 
98  public:
100 
102  propagate_ |= GRPC_PROPAGATE_DEADLINE;
103  return *this;
104  }
105 
107  propagate_ &= ~GRPC_PROPAGATE_DEADLINE;
108  return *this;
109  }
110 
113  return *this;
114  }
115 
118  return *this;
119  }
120 
123  return *this;
124  }
125 
128  return *this;
129  }
130 
132  propagate_ |= GRPC_PROPAGATE_CANCELLATION;
133  return *this;
134  }
135 
137  propagate_ &= ~GRPC_PROPAGATE_CANCELLATION;
138  return *this;
139  }
140 
141  uint32_t c_bitmask() const { return propagate_; }
142 
143  private:
144  uint32_t propagate_;
145 };
146 
147 namespace testing {
148 class InteropClientContextInspector;
149 } // namespace testing
150 
166  public:
167  ClientContext();
168  ~ClientContext();
169 
180  static std::unique_ptr<ClientContext> FromServerContext(
181  const ServerContext& server_context,
183 
194  void AddMetadata(const grpc::string& meta_key,
195  const grpc::string& meta_value);
196 
205  const std::multimap<grpc::string_ref, grpc::string_ref>&
207  GPR_CODEGEN_ASSERT(initial_metadata_received_);
208  return *recv_initial_metadata_.map();
209  }
210 
217  const std::multimap<grpc::string_ref, grpc::string_ref>&
219  // TODO(yangg) check finished
220  return *trailing_metadata_.map();
221  }
222 
229  template <typename T>
230  void set_deadline(const T& deadline) {
231  TimePoint<T> deadline_tp(deadline);
232  deadline_ = deadline_tp.raw_time();
233  }
234 
240  void set_idempotent(bool idempotent) { idempotent_ = idempotent; }
241 
245  void set_cacheable(bool cacheable) { cacheable_ = cacheable; }
246 
253  void set_wait_for_ready(bool wait_for_ready) {
254  wait_for_ready_ = wait_for_ready;
255  wait_for_ready_explicitly_set_ = true;
256  }
257 
259  void set_fail_fast(bool fail_fast) { set_wait_for_ready(!fail_fast); }
260 
262  std::chrono::system_clock::time_point deadline() const {
263  return Timespec2Timepoint(deadline_);
264  }
265 
267  gpr_timespec raw_deadline() const { return deadline_; }
268 
271  void set_authority(const grpc::string& authority) { authority_ = authority; }
272 
276  std::shared_ptr<const AuthContext> auth_context() const {
277  if (auth_context_.get() == nullptr) {
278  auth_context_ = CreateAuthContext(call_);
279  }
280  return auth_context_;
281  }
282 
291  void set_credentials(const std::shared_ptr<CallCredentials>& creds) {
292  creds_ = creds;
293  }
294 
299  return compression_algorithm_;
300  }
301 
305  void set_compression_algorithm(grpc_compression_algorithm algorithm);
306 
317  void set_initial_metadata_corked(bool corked) {
318  initial_metadata_corked_ = corked;
319  }
320 
328  grpc::string peer() const;
329 
331  void set_census_context(struct census_context* ccp) { census_context_ = ccp; }
332  struct census_context* census_context() const {
333  return census_context_;
334  }
335 
345  void TryCancel();
346 
352  public:
353  virtual ~GlobalCallbacks() {}
354  virtual void DefaultConstructor(ClientContext* context) = 0;
355  virtual void Destructor(ClientContext* context) = 0;
356  };
357  static void SetGlobalCallbacks(GlobalCallbacks* callbacks);
358 
361  grpc_call* c_call() { return call_; }
362 
368  grpc::string debug_error_string() const { return debug_error_string_; }
369 
370  private:
371  // Disallow copy and assign.
373  ClientContext& operator=(const ClientContext&);
374 
375  friend class ::grpc::testing::InteropClientContextInspector;
376  friend class ::grpc::internal::CallOpClientRecvStatus;
377  friend class ::grpc::internal::CallOpRecvInitialMetadata;
378  friend class Channel;
379  template <class R>
380  friend class ::grpc::ClientReader;
381  template <class W>
382  friend class ::grpc::ClientWriter;
383  template <class W, class R>
384  friend class ::grpc::ClientReaderWriter;
385  template <class R>
386  friend class ::grpc::ClientAsyncReader;
387  template <class W>
388  friend class ::grpc::ClientAsyncWriter;
389  template <class W, class R>
390  friend class ::grpc::ClientAsyncReaderWriter;
391  template <class R>
392  friend class ::grpc::ClientAsyncResponseReader;
393  template <class InputMessage, class OutputMessage>
394  friend class ::grpc::internal::BlockingUnaryCallImpl;
395  template <class InputMessage, class OutputMessage>
396  friend class ::grpc::internal::CallbackUnaryCallImpl;
397 
398  // Used by friend class CallOpClientRecvStatus
399  void set_debug_error_string(const grpc::string& debug_error_string) {
400  debug_error_string_ = debug_error_string;
401  }
402 
403  grpc_call* call() const { return call_; }
404  void set_call(grpc_call* call, const std::shared_ptr<Channel>& channel);
405 
406  experimental::ClientRpcInfo* set_client_rpc_info(
407  const char* method, grpc::ChannelInterface* channel,
408  const std::vector<
409  std::unique_ptr<experimental::ClientInterceptorFactoryInterface>>&
410  creators,
411  size_t interceptor_pos) {
412  rpc_info_ = experimental::ClientRpcInfo(this, method, channel);
413  rpc_info_.RegisterInterceptors(creators, interceptor_pos);
414  return &rpc_info_;
415  }
416 
417  uint32_t initial_metadata_flags() const {
418  return (idempotent_ ? GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST : 0) |
419  (wait_for_ready_ ? GRPC_INITIAL_METADATA_WAIT_FOR_READY : 0) |
420  (cacheable_ ? GRPC_INITIAL_METADATA_CACHEABLE_REQUEST : 0) |
421  (wait_for_ready_explicitly_set_
423  : 0) |
424  (initial_metadata_corked_ ? GRPC_INITIAL_METADATA_CORKED : 0);
425  }
426 
427  grpc::string authority() { return authority_; }
428 
429  void SendCancelToInterceptors();
430 
431  bool initial_metadata_received_;
432  bool wait_for_ready_;
433  bool wait_for_ready_explicitly_set_;
434  bool idempotent_;
435  bool cacheable_;
436  std::shared_ptr<Channel> channel_;
437  std::mutex mu_;
438  grpc_call* call_;
439  bool call_canceled_;
440  gpr_timespec deadline_;
441  grpc::string authority_;
442  std::shared_ptr<CallCredentials> creds_;
443  mutable std::shared_ptr<const AuthContext> auth_context_;
444  struct census_context* census_context_;
445  std::multimap<grpc::string, grpc::string> send_initial_metadata_;
446  mutable internal::MetadataMap recv_initial_metadata_;
447  mutable internal::MetadataMap trailing_metadata_;
448 
449  grpc_call* propagate_from_call_;
450  PropagationOptions propagation_options_;
451 
452  grpc_compression_algorithm compression_algorithm_;
453  bool initial_metadata_corked_;
454 
455  grpc::string debug_error_string_;
456 
457  experimental::ClientRpcInfo rpc_info_;
458 };
459 
460 } // namespace grpc
461 
462 #endif // GRPCPP_IMPL_CODEGEN_CLIENT_CONTEXT_H
const std::multimap< grpc::string_ref, grpc::string_ref > & GetServerInitialMetadata() const
Return a collection of initial metadata key-value pairs.
Definition: client_context.h:206
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::string debug_error_string() const
EXPERIMENTAL debugging API.
Definition: client_context.h:368
struct census_context * census_context() const
Definition: client_context.h:332
void set_idempotent(bool idempotent)
EXPERIMENTAL: Indicate that this request is idempotent.
Definition: client_context.h:240
PropagationOptions & enable_census_tracing_propagation()
Definition: client_context.h:121
PropagationOptions & enable_deadline_propagation()
Definition: client_context.h:101
#define GPR_CODEGEN_ASSERT(x)
Codegen specific version of GPR_ASSERT.
Definition: core_codegen_interface.h:141
std::string string
Definition: config.h:35
const std::multimap< grpc::string_ref, grpc::string_ref > & GetServerTrailingMetadata() const
Return a collection of trailing metadata key-value pairs.
Definition: client_context.h:218
void set_wait_for_ready(bool wait_for_ready)
EXPERIMENTAL: Trigger wait-for-ready or not on this request.
Definition: client_context.h:253
Definition: metadata_map.h:33
grpc_call * c_call()
Should be used for framework-level extensions only.
Definition: client_context.h:361
void set_deadline(const T &deadline)
Set the deadline for the client call.
Definition: client_context.h:230
void set_initial_metadata_corked(bool corked)
Flag whether the initial metadata should be corked.
Definition: client_context.h:317
PropagationOptions & disable_cancellation_propagation()
Definition: client_context.h:136
PropagationOptions & enable_census_stats_propagation()
Definition: client_context.h:111
#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:424
gpr_timespec raw_time()
Definition: time.h:43
If you are trying to use CompletionQueue::AsyncNext with a time class that isn&#39;t either gpr_timespec ...
Definition: time.h:40
PropagationOptions & enable_cancellation_propagation()
Definition: client_context.h:131
#define GRPC_INITIAL_METADATA_CORKED
Signal that the initial metadata should be corked.
Definition: grpc_types.h:426
A ClientContext allows the person implementing a service client to:
Definition: client_context.h:165
uint32_t c_bitmask() const
Definition: client_context.h:141
Options for ClientContext::FromServerContext specifying which traits from the ServerContext to propag...
Definition: client_context.h:97
void set_fail_fast(bool fail_fast)
DEPRECATED: Use set_wait_for_ready() instead.
Definition: client_context.h:259
void set_census_context(struct census_context *ccp)
Get and set census context.
Definition: client_context.h:331
std::shared_ptr< const AuthContext > auth_context() const
Return the authentication context for this client call.
Definition: client_context.h:276
#define GRPC_PROPAGATE_CENSUS_TRACING_CONTEXT
Definition: propagation_bits.h:34
void set_cacheable(bool cacheable)
EXPERIMENTAL: Set this request to be cacheable.
Definition: client_context.h:245
Global Callbacks.
Definition: client_context.h:351
grpc_compression_algorithm
The various compression algorithms supported by gRPC.
Definition: compression_types.h:56
grpc_compression_algorithm compression_algorithm() const
Return the compression algorithm the client call will request be used.
Definition: client_context.h:298
An Alarm posts the user provided tag to its associated completion queue upon expiry or cancellation...
Definition: alarm.h:33
Codegen interface for grpc::Channel.
Definition: channel_interface.h:60
#define GRPC_INITIAL_METADATA_CACHEABLE_REQUEST
Signal that the call is cacheable.
Definition: grpc_types.h:421
A ServerContext allows the person implementing a service handler to:
Definition: server_context.h:102
PropagationOptions & disable_census_tracing_propagation()
Definition: client_context.h:126
#define GRPC_INITIAL_METADATA_WAIT_FOR_READY
Signal that the call should not return UNAVAILABLE before it has started.
Definition: grpc_types.h:419
#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
#define GRPC_PROPAGATE_CENSUS_STATS_CONTEXT
Propagate census context.
Definition: propagation_bits.h:33
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.h:271
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
PropagationOptions()
Definition: client_context.h:99
gpr_timespec raw_deadline() const
Return a gpr_timespec representation of the client call&#39;s deadline.
Definition: client_context.h:267
void set_credentials(const std::shared_ptr< CallCredentials > &creds)
Set credentials for the client call.
Definition: client_context.h:291
PropagationOptions & disable_deadline_propagation()
Definition: client_context.h:106
Definition: client_interceptor.h:53
std::chrono::system_clock::time_point deadline() const
Return the deadline for the client call.
Definition: client_context.h:262
PropagationOptions & disable_census_stats_propagation()
Definition: client_context.h:116
virtual ~GlobalCallbacks()
Definition: client_context.h:353
#define GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST
Initial metadata flags.
Definition: grpc_types.h:417
#define GRPC_PROPAGATE_CANCELLATION
Propagate cancellation.
Definition: propagation_bits.h:36
Channels represent a connection to an endpoint. Created by CreateChannel.
Definition: channel.h:44