GRPC C++  1.39.1
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 {
60 class ServerContext;
61 class ServerContextBase;
63 
64 namespace internal {
65 template <class InputMessage, class OutputMessage>
66 class CallbackUnaryCallImpl;
67 template <class Request, class Response>
68 class ClientCallbackReaderWriterImpl;
69 template <class Response>
70 class ClientCallbackReaderImpl;
71 template <class Request>
72 class ClientCallbackWriterImpl;
73 class ClientCallbackUnaryImpl;
74 class ClientContextAccessor;
75 class ClientAsyncResponseReaderHelper;
76 } // namespace internal
77 
78 template <class R>
79 class ClientReader;
80 template <class W>
81 class ClientWriter;
82 template <class W, class R>
83 class ClientReaderWriter;
84 template <class R>
85 class ClientAsyncReader;
86 template <class W>
87 class ClientAsyncWriter;
88 template <class W, class R>
89 class ClientAsyncReaderWriter;
90 template <class R>
91 class ClientAsyncResponseReader;
92 
93 namespace testing {
94 class InteropClientContextInspector;
95 class ClientContextTestPeer;
96 } // namespace testing
97 
98 namespace internal {
99 class RpcMethod;
100 template <class InputMessage, class OutputMessage>
101 class BlockingUnaryCallImpl;
102 class CallOpClientRecvStatus;
103 class CallOpRecvInitialMetadata;
104 class ServerContextImpl;
105 template <class InputMessage, class OutputMessage>
106 class CallbackUnaryCallImpl;
107 template <class Request, class Response>
108 class ClientCallbackReaderWriterImpl;
109 template <class Response>
110 class ClientCallbackReaderImpl;
111 template <class Request>
112 class ClientCallbackWriterImpl;
113 class ClientCallbackUnaryImpl;
114 class ClientContextAccessor;
115 } // namespace internal
116 
117 class CallCredentials;
118 class Channel;
119 class ChannelInterface;
120 class CompletionQueue;
121 
128  public:
130 
132  propagate_ |= GRPC_PROPAGATE_DEADLINE;
133  return *this;
134  }
135 
137  propagate_ &= ~GRPC_PROPAGATE_DEADLINE;
138  return *this;
139  }
140 
143  return *this;
144  }
145 
148  return *this;
149  }
150 
153  return *this;
154  }
155 
158  return *this;
159  }
160 
162  propagate_ |= GRPC_PROPAGATE_CANCELLATION;
163  return *this;
164  }
165 
167  propagate_ &= ~GRPC_PROPAGATE_CANCELLATION;
168  return *this;
169  }
170 
171  uint32_t c_bitmask() const { return propagate_; }
172 
173  private:
174  uint32_t propagate_;
175 };
176 
194  public:
197 
208  static std::unique_ptr<ClientContext> FromServerContext(
209  const grpc::ServerContextBase& server_context,
211  static std::unique_ptr<ClientContext> FromCallbackServerContext(
212  const grpc::CallbackServerContext& server_context,
214 
232  void AddMetadata(const std::string& meta_key, const std::string& meta_value);
233 
242  const std::multimap<grpc::string_ref, grpc::string_ref>&
244  GPR_CODEGEN_ASSERT(initial_metadata_received_);
245  return *recv_initial_metadata_.map();
246  }
247 
254  const std::multimap<grpc::string_ref, grpc::string_ref>&
256  // TODO(yangg) check finished
257  return *trailing_metadata_.map();
258  }
259 
266  template <typename T>
267  void set_deadline(const T& deadline) {
268  grpc::TimePoint<T> deadline_tp(deadline);
269  deadline_ = deadline_tp.raw_time();
270  }
271 
277  void set_idempotent(bool idempotent) { idempotent_ = idempotent; }
278 
282  void set_cacheable(bool cacheable) { cacheable_ = cacheable; }
283 
290  void set_wait_for_ready(bool wait_for_ready) {
291  wait_for_ready_ = wait_for_ready;
292  wait_for_ready_explicitly_set_ = true;
293  }
294 
296  void set_fail_fast(bool fail_fast) { set_wait_for_ready(!fail_fast); }
297 
299  std::chrono::system_clock::time_point deadline() const {
300  return grpc::Timespec2Timepoint(deadline_);
301  }
302 
304  gpr_timespec raw_deadline() const { return deadline_; }
305 
308  void set_authority(const std::string& authority) { authority_ = authority; }
309 
314  std::shared_ptr<const grpc::AuthContext> auth_context() const {
315  if (auth_context_ == nullptr) {
316  auth_context_ = grpc::CreateAuthContext(call_);
317  }
318  return auth_context_;
319  }
320 
331  void set_credentials(const std::shared_ptr<grpc::CallCredentials>& creds);
332 
338  std::shared_ptr<grpc::CallCredentials> credentials() { return creds_; }
339 
344  return compression_algorithm_;
345  }
346 
351 
362  void set_initial_metadata_corked(bool corked) {
363  initial_metadata_corked_ = corked;
364  }
365 
374  std::string peer() const;
375 
380  void set_census_context(struct census_context* ccp) { census_context_ = ccp; }
381 
383  struct census_context* census_context() const {
384  return census_context_;
385  }
386 
396  void TryCancel();
397 
403  public:
404  virtual ~GlobalCallbacks() {}
405  virtual void DefaultConstructor(ClientContext* context) = 0;
406  virtual void Destructor(ClientContext* context) = 0;
407  };
408  static void SetGlobalCallbacks(GlobalCallbacks* callbacks);
409 
412  grpc_call* c_call() { return call_; }
413 
419  std::string debug_error_string() const { return debug_error_string_; }
420 
421  private:
422  // Disallow copy and assign.
424  ClientContext& operator=(const ClientContext&);
425 
426  friend class ::grpc::testing::InteropClientContextInspector;
427  friend class ::grpc::testing::ClientContextTestPeer;
428  friend class ::grpc::internal::CallOpClientRecvStatus;
429  friend class ::grpc::internal::CallOpRecvInitialMetadata;
430  friend class ::grpc::Channel;
431  template <class R>
432  friend class ::grpc::ClientReader;
433  template <class W>
434  friend class ::grpc::ClientWriter;
435  template <class W, class R>
436  friend class ::grpc::ClientReaderWriter;
437  template <class R>
438  friend class ::grpc::ClientAsyncReader;
439  template <class W>
440  friend class ::grpc::ClientAsyncWriter;
441  template <class W, class R>
442  friend class ::grpc::ClientAsyncReaderWriter;
443  template <class R>
444  friend class ::grpc::ClientAsyncResponseReader;
445  friend class ::grpc::internal::ClientAsyncResponseReaderHelper;
446  template <class InputMessage, class OutputMessage>
447  friend class ::grpc::internal::BlockingUnaryCallImpl;
448  template <class InputMessage, class OutputMessage>
449  friend class ::grpc::internal::CallbackUnaryCallImpl;
450  template <class Request, class Response>
451  friend class ::grpc::internal::ClientCallbackReaderWriterImpl;
452  template <class Response>
453  friend class ::grpc::internal::ClientCallbackReaderImpl;
454  template <class Request>
455  friend class ::grpc::internal::ClientCallbackWriterImpl;
456  friend class ::grpc::internal::ClientCallbackUnaryImpl;
457  friend class ::grpc::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, const char* suffix_for_stats,
471  const std::vector<std::unique_ptr<
473  size_t interceptor_pos) {
474  rpc_info_ = grpc::experimental::ClientRpcInfo(this, type, method,
475  suffix_for_stats, channel);
476  rpc_info_.RegisterInterceptors(creators, interceptor_pos);
477  return &rpc_info_;
478  }
479 
480  uint32_t initial_metadata_flags() const {
481  return (idempotent_ ? GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST : 0) |
482  (wait_for_ready_ ? GRPC_INITIAL_METADATA_WAIT_FOR_READY : 0) |
483  (cacheable_ ? GRPC_INITIAL_METADATA_CACHEABLE_REQUEST : 0) |
484  (wait_for_ready_explicitly_set_
486  : 0) |
487  (initial_metadata_corked_ ? GRPC_INITIAL_METADATA_CORKED : 0);
488  }
489 
490  std::string authority() { return authority_; }
491 
492  void SendCancelToInterceptors();
493 
494  static std::unique_ptr<ClientContext> FromInternalServerContext(
495  const grpc::ServerContextBase& server_context,
496  PropagationOptions options);
497 
498  bool initial_metadata_received_;
499  bool wait_for_ready_;
500  bool wait_for_ready_explicitly_set_;
501  bool idempotent_;
502  bool cacheable_;
503  std::shared_ptr<::grpc::Channel> channel_;
505  grpc_call* call_;
506  bool call_canceled_;
507  gpr_timespec deadline_;
508  grpc::string authority_;
509  std::shared_ptr<grpc::CallCredentials> creds_;
510  mutable std::shared_ptr<const grpc::AuthContext> auth_context_;
511  struct census_context* census_context_;
512  std::multimap<std::string, std::string> send_initial_metadata_;
513  mutable grpc::internal::MetadataMap recv_initial_metadata_;
514  mutable grpc::internal::MetadataMap trailing_metadata_;
515 
516  grpc_call* propagate_from_call_;
517  PropagationOptions propagation_options_;
518 
519  grpc_compression_algorithm compression_algorithm_;
520  bool initial_metadata_corked_;
521 
522  std::string debug_error_string_;
523 
525 };
526 
527 } // namespace grpc
528 
529 #endif // GRPCPP_IMPL_CODEGEN_CLIENT_CONTEXT_H
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
Definition: server_context.h:578
Codegen interface for grpc::Channel.
Definition: channel_interface.h:71
Global Callbacks.
Definition: client_context.h:402
virtual void Destructor(ClientContext *context)=0
virtual ~GlobalCallbacks()
Definition: client_context.h:404
virtual void DefaultConstructor(ClientContext *context)=0
A ClientContext allows the person implementing a service client to:
Definition: client_context.h:193
const std::multimap< grpc::string_ref, grpc::string_ref > & GetServerInitialMetadata() const
Return a collection of initial metadata key-value pairs.
Definition: client_context.h:243
void set_credentials(const std::shared_ptr< grpc::CallCredentials > &creds)
Set credentials for the client call.
void set_cacheable(bool cacheable)
EXPERIMENTAL: Set this request to be cacheable.
Definition: client_context.h:282
void set_fail_fast(bool fail_fast)
DEPRECATED: Use set_wait_for_ready() instead.
Definition: client_context.h:296
gpr_timespec raw_deadline() const
Return a gpr_timespec representation of the client call's deadline.
Definition: client_context.h:304
static std::unique_ptr< ClientContext > FromServerContext(const grpc::ServerContextBase &server_context, PropagationOptions options=PropagationOptions())
Create a new ClientContext as a child of an incoming server call, according to options (.
void set_census_context(struct census_context *ccp)
Sets the census context.
Definition: client_context.h:380
const std::multimap< grpc::string_ref, grpc::string_ref > & GetServerTrailingMetadata() const
Return a collection of trailing metadata key-value pairs.
Definition: client_context.h:255
std::string peer() const
Return the peer uri in a string.
void set_compression_algorithm(grpc_compression_algorithm algorithm)
Set algorithm to be the compression algorithm used for the client call.
std::string debug_error_string() const
EXPERIMENTAL debugging API.
Definition: client_context.h:419
grpc_compression_algorithm compression_algorithm() const
Return the compression algorithm the client call will request be used.
Definition: client_context.h:343
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.
std::shared_ptr< const grpc::AuthContext > auth_context() const
Return the authentication context for the associated client call.
Definition: client_context.h:314
std::chrono::system_clock::time_point deadline() const
Return the deadline for the client call.
Definition: client_context.h:299
grpc_call * c_call()
Should be used for framework-level extensions only.
Definition: client_context.h:412
struct census_context * census_context() const
Returns the census context that has been set, or nullptr if not set.
Definition: client_context.h:383
void set_wait_for_ready(bool wait_for_ready)
EXPERIMENTAL: Trigger wait-for-ready or not on this request.
Definition: client_context.h:290
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:308
void TryCancel()
Send a best-effort out-of-band cancel on the call associated with this client context.
static void SetGlobalCallbacks(GlobalCallbacks *callbacks)
void set_deadline(const T &deadline)
Set the deadline for the client call.
Definition: client_context.h:267
std::shared_ptr< grpc::CallCredentials > credentials()
EXPERIMENTAL debugging API.
Definition: client_context.h:338
void set_initial_metadata_corked(bool corked)
Flag whether the initial metadata should be corked.
Definition: client_context.h:362
void set_idempotent(bool idempotent)
EXPERIMENTAL: Indicate that this request is idempotent.
Definition: client_context.h:277
static std::unique_ptr< ClientContext > FromCallbackServerContext(const grpc::CallbackServerContext &server_context, PropagationOptions options=PropagationOptions())
Options for ClientContext::FromServerContext specifying which traits from the ServerContext to propag...
Definition: client_context.h:127
PropagationOptions & disable_census_stats_propagation()
Definition: client_context.h:146
PropagationOptions & enable_census_tracing_propagation()
Definition: client_context.h:151
PropagationOptions & disable_cancellation_propagation()
Definition: client_context.h:166
PropagationOptions()
Definition: client_context.h:129
uint32_t c_bitmask() const
Definition: client_context.h:171
PropagationOptions & enable_deadline_propagation()
Definition: client_context.h:131
PropagationOptions & enable_cancellation_propagation()
Definition: client_context.h:161
PropagationOptions & disable_census_tracing_propagation()
Definition: client_context.h:156
PropagationOptions & enable_census_stats_propagation()
Definition: client_context.h:141
PropagationOptions & disable_deadline_propagation()
Definition: client_context.h:136
Base class of ServerContext. Experimental until callback API is final.
Definition: server_context.h:126
If you are trying to use CompletionQueue::AsyncNext with a time class that isn't either gpr_timespec ...
Definition: time.h:40
gpr_timespec raw_time()=delete
Definition: client_interceptor.h:66
Definition: metadata_map.h:33
std::multimap< grpc::string_ref, grpc::string_ref > * map()
Definition: metadata_map.h:66
Definition: sync.h:58
RpcType
Definition: rpc_method.h:31
grpc_compression_algorithm
The various compression algorithms supported by gRPC (not sorted by compression level)
Definition: compression_types.h:57
#define GRPC_INITIAL_METADATA_CORKED
Signal that the initial metadata should be corked.
Definition: grpc_types.h:508
struct grpc_call grpc_call
A Call represents an RPC.
Definition: grpc_types.h:70
#define GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST
Initial metadata flags.
Definition: grpc_types.h:499
#define GRPC_INITIAL_METADATA_CACHEABLE_REQUEST
Signal that the call is cacheable.
Definition: grpc_types.h:503
#define GRPC_INITIAL_METADATA_WAIT_FOR_READY
Signal that the call should not return UNAVAILABLE before it has started.
Definition: grpc_types.h:501
#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:506
#define GPR_CODEGEN_ASSERT(x)
Codegen specific version of GPR_ASSERT.
Definition: core_codegen_interface.h:146
::grpc::ServerContextBase ServerContextBase
Definition: server_context.h:108
::grpc::CallbackServerContext CallbackServerContext
Definition: server_context.h:109
An Alarm posts the user-provided tag to its associated completion queue or invokes the user-provided ...
Definition: alarm.h:33
std::chrono::system_clock::time_point Timespec2Timepoint(gpr_timespec t)
std::shared_ptr< const AuthContext > CreateAuthContext(grpc_call *call)
#define GRPC_PROPAGATE_CENSUS_STATS_CONTEXT
Propagate census context.
Definition: propagation_bits.h:33
#define GRPC_PROPAGATE_DEADLINE
Propagation bits: this can be bitwise or-ed to form propagation_mask for grpc_call.
Definition: propagation_bits.h:31
#define GRPC_PROPAGATE_CANCELLATION
Propagate cancellation.
Definition: propagation_bits.h:36
#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_TRACING_CONTEXT
Definition: propagation_bits.h:34
Analogous to struct timespec.
Definition: gpr_types.h:47