GRPC C++  1.16.0-dev
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 
53 
54 struct census_context;
55 struct grpc_call;
56 
57 namespace grpc {
58 
59 class Channel;
60 class ChannelInterface;
61 class CompletionQueue;
62 class CallCredentials;
63 class ClientContext;
64 
65 namespace internal {
66 class RpcMethod;
67 class CallOpClientRecvStatus;
68 class CallOpRecvInitialMetadata;
69 template <class InputMessage, class OutputMessage>
70 class BlockingUnaryCallImpl;
71 template <class InputMessage, class OutputMessage>
72 class CallbackUnaryCallImpl;
73 } // namespace internal
74 
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>
86 class ClientAsyncReaderWriter;
87 template <class R>
88 class ClientAsyncResponseReader;
89 class ServerContext;
90 
97  public:
99 
101  propagate_ |= GRPC_PROPAGATE_DEADLINE;
102  return *this;
103  }
104 
106  propagate_ &= ~GRPC_PROPAGATE_DEADLINE;
107  return *this;
108  }
109 
112  return *this;
113  }
114 
117  return *this;
118  }
119 
122  return *this;
123  }
124 
127  return *this;
128  }
129 
131  propagate_ |= GRPC_PROPAGATE_CANCELLATION;
132  return *this;
133  }
134 
136  propagate_ &= ~GRPC_PROPAGATE_CANCELLATION;
137  return *this;
138  }
139 
140  uint32_t c_bitmask() const { return propagate_; }
141 
142  private:
143  uint32_t propagate_;
144 };
145 
146 namespace testing {
147 class InteropClientContextInspector;
148 } // namespace testing
149 
165  public:
166  ClientContext();
167  ~ClientContext();
168 
179  static std::unique_ptr<ClientContext> FromServerContext(
180  const ServerContext& server_context,
182 
193  void AddMetadata(const grpc::string& meta_key,
194  const grpc::string& meta_value);
195 
204  const std::multimap<grpc::string_ref, grpc::string_ref>&
206  GPR_CODEGEN_ASSERT(initial_metadata_received_);
207  return *recv_initial_metadata_.map();
208  }
209 
216  const std::multimap<grpc::string_ref, grpc::string_ref>&
218  // TODO(yangg) check finished
219  return *trailing_metadata_.map();
220  }
221 
228  template <typename T>
229  void set_deadline(const T& deadline) {
230  TimePoint<T> deadline_tp(deadline);
231  deadline_ = deadline_tp.raw_time();
232  }
233 
239  void set_idempotent(bool idempotent) { idempotent_ = idempotent; }
240 
244  void set_cacheable(bool cacheable) { cacheable_ = cacheable; }
245 
252  void set_wait_for_ready(bool wait_for_ready) {
253  wait_for_ready_ = wait_for_ready;
254  wait_for_ready_explicitly_set_ = true;
255  }
256 
258  void set_fail_fast(bool fail_fast) { set_wait_for_ready(!fail_fast); }
259 
261  std::chrono::system_clock::time_point deadline() const {
262  return Timespec2Timepoint(deadline_);
263  }
264 
266  gpr_timespec raw_deadline() const { return deadline_; }
267 
270  void set_authority(const grpc::string& authority) { authority_ = authority; }
271 
275  std::shared_ptr<const AuthContext> auth_context() const {
276  if (auth_context_.get() == nullptr) {
277  auth_context_ = CreateAuthContext(call_);
278  }
279  return auth_context_;
280  }
281 
290  void set_credentials(const std::shared_ptr<CallCredentials>& creds) {
291  creds_ = creds;
292  }
293 
298  return compression_algorithm_;
299  }
300 
304  void set_compression_algorithm(grpc_compression_algorithm algorithm);
305 
316  void set_initial_metadata_corked(bool corked) {
317  initial_metadata_corked_ = corked;
318  }
319 
327  grpc::string peer() const;
328 
330  void set_census_context(struct census_context* ccp) { census_context_ = ccp; }
331  struct census_context* census_context() const {
332  return census_context_;
333  }
334 
344  void TryCancel();
345 
351  public:
352  virtual ~GlobalCallbacks() {}
353  virtual void DefaultConstructor(ClientContext* context) = 0;
354  virtual void Destructor(ClientContext* context) = 0;
355  };
356  static void SetGlobalCallbacks(GlobalCallbacks* callbacks);
357 
360  grpc_call* c_call() { return call_; }
361 
367  grpc::string debug_error_string() const { return debug_error_string_; }
368 
369  private:
370  // Disallow copy and assign.
372  ClientContext& operator=(const ClientContext&);
373 
374  friend class ::grpc::testing::InteropClientContextInspector;
375  friend class ::grpc::internal::CallOpClientRecvStatus;
376  friend class ::grpc::internal::CallOpRecvInitialMetadata;
377  friend class Channel;
378  template <class R>
379  friend class ::grpc::ClientReader;
380  template <class W>
381  friend class ::grpc::ClientWriter;
382  template <class W, class R>
383  friend class ::grpc::ClientReaderWriter;
384  template <class R>
385  friend class ::grpc::ClientAsyncReader;
386  template <class W>
387  friend class ::grpc::ClientAsyncWriter;
388  template <class W, class R>
389  friend class ::grpc::ClientAsyncReaderWriter;
390  template <class R>
391  friend class ::grpc::ClientAsyncResponseReader;
392  template <class InputMessage, class OutputMessage>
393  friend class ::grpc::internal::BlockingUnaryCallImpl;
394  template <class InputMessage, class OutputMessage>
395  friend class ::grpc::internal::CallbackUnaryCallImpl;
396 
397  // Used by friend class CallOpClientRecvStatus
398  void set_debug_error_string(const grpc::string& debug_error_string) {
399  debug_error_string_ = debug_error_string;
400  }
401 
402  grpc_call* call() const { return call_; }
403  void set_call(grpc_call* call, const std::shared_ptr<Channel>& channel);
404 
405  uint32_t initial_metadata_flags() const {
406  return (idempotent_ ? GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST : 0) |
407  (wait_for_ready_ ? GRPC_INITIAL_METADATA_WAIT_FOR_READY : 0) |
408  (cacheable_ ? GRPC_INITIAL_METADATA_CACHEABLE_REQUEST : 0) |
409  (wait_for_ready_explicitly_set_
411  : 0) |
412  (initial_metadata_corked_ ? GRPC_INITIAL_METADATA_CORKED : 0);
413  }
414 
415  grpc::string authority() { return authority_; }
416 
417  bool initial_metadata_received_;
418  bool wait_for_ready_;
419  bool wait_for_ready_explicitly_set_;
420  bool idempotent_;
421  bool cacheable_;
422  std::shared_ptr<Channel> channel_;
423  std::mutex mu_;
424  grpc_call* call_;
425  bool call_canceled_;
426  gpr_timespec deadline_;
427  grpc::string authority_;
428  std::shared_ptr<CallCredentials> creds_;
429  mutable std::shared_ptr<const AuthContext> auth_context_;
430  struct census_context* census_context_;
431  std::multimap<grpc::string, grpc::string> send_initial_metadata_;
432  mutable internal::MetadataMap recv_initial_metadata_;
433  mutable internal::MetadataMap trailing_metadata_;
434 
435  grpc_call* propagate_from_call_;
436  PropagationOptions propagation_options_;
437 
438  grpc_compression_algorithm compression_algorithm_;
439  bool initial_metadata_corked_;
440 
441  grpc::string debug_error_string_;
442 };
443 
444 } // namespace grpc
445 
446 #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:205
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:367
struct census_context * census_context() const
Definition: client_context.h:331
void set_idempotent(bool idempotent)
EXPERIMENTAL: Indicate that this request is idempotent.
Definition: client_context.h:239
PropagationOptions & enable_census_tracing_propagation()
Definition: client_context.h:120
PropagationOptions & enable_deadline_propagation()
Definition: client_context.h:100
#define GPR_CODEGEN_ASSERT(x)
Codegen specific version of GPR_ASSERT.
Definition: core_codegen_interface.h:138
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:217
void set_wait_for_ready(bool wait_for_ready)
EXPERIMENTAL: Trigger wait-for-ready or not on this request.
Definition: client_context.h:252
Definition: metadata_map.h:31
grpc_call * c_call()
Should be used for framework-level extensions only.
Definition: client_context.h:360
void set_deadline(const T &deadline)
Set the deadline for the client call.
Definition: client_context.h:229
void set_initial_metadata_corked(bool corked)
Flag whether the initial metadata should be corked.
Definition: client_context.h:316
PropagationOptions & disable_cancellation_propagation()
Definition: client_context.h:135
PropagationOptions & enable_census_stats_propagation()
Definition: client_context.h:110
#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:419
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:130
#define GRPC_INITIAL_METADATA_CORKED
Signal that the initial metadata should be corked.
Definition: grpc_types.h:421
A ClientContext allows the person implementing a service client to:
Definition: client_context.h:164
uint32_t c_bitmask() const
Definition: client_context.h:140
Options for ClientContext::FromServerContext specifying which traits from the ServerContext to propag...
Definition: client_context.h:96
void set_fail_fast(bool fail_fast)
DEPRECATED: Use set_wait_for_ready() instead.
Definition: client_context.h:258
void set_census_context(struct census_context *ccp)
Get and set census context.
Definition: client_context.h:330
std::shared_ptr< const AuthContext > auth_context() const
Return the authentication context for this client call.
Definition: client_context.h:275
#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:244
Global Callbacks.
Definition: client_context.h:350
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:297
An Alarm posts the user provided tag to its associated completion queue upon expiry or cancellation...
Definition: alarm.h:31
#define GRPC_INITIAL_METADATA_CACHEABLE_REQUEST
Signal that the call is cacheable.
Definition: grpc_types.h:416
A ServerContext allows the person implementing a service handler to:
Definition: server_context.h:97
PropagationOptions & disable_census_tracing_propagation()
Definition: client_context.h:125
#define GRPC_INITIAL_METADATA_WAIT_FOR_READY
Signal that the call should not return UNAVAILABLE before it has started.
Definition: grpc_types.h:414
#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:270
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:98
gpr_timespec raw_deadline() const
Return a gpr_timespec representation of the client call&#39;s deadline.
Definition: client_context.h:266
void set_credentials(const std::shared_ptr< CallCredentials > &creds)
Set credentials for the client call.
Definition: client_context.h:290
PropagationOptions & disable_deadline_propagation()
Definition: client_context.h:105
std::chrono::system_clock::time_point deadline() const
Return the deadline for the client call.
Definition: client_context.h:261
PropagationOptions & disable_census_stats_propagation()
Definition: client_context.h:115
virtual ~GlobalCallbacks()
Definition: client_context.h:352
#define GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST
Initial metadata flags.
Definition: grpc_types.h:412
#define GRPC_PROPAGATE_CANCELLATION
Propagate cancellation.
Definition: propagation_bits.h:36
Channels represent a connection to an endpoint. Created by CreateChannel.
Definition: channel.h:42