GRPC C++  1.6.0
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 GRPCXX_IMPL_CODEGEN_CLIENT_CONTEXT_H
35 #define GRPCXX_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 RpcMethod;
64 template <class R>
65 class ClientReader;
66 template <class W>
67 class ClientWriter;
68 template <class W, class R>
69 class ClientReaderWriter;
70 template <class R>
71 class ClientAsyncReader;
72 template <class W>
73 class ClientAsyncWriter;
74 template <class W, class R>
75 class ClientAsyncReaderWriter;
76 template <class R>
77 class ClientAsyncResponseReader;
78 class ServerContext;
79 
86  public:
88 
90  propagate_ |= GRPC_PROPAGATE_DEADLINE;
91  return *this;
92  }
93 
95  propagate_ &= ~GRPC_PROPAGATE_DEADLINE;
96  return *this;
97  }
98 
101  return *this;
102  }
103 
106  return *this;
107  }
108 
111  return *this;
112  }
113 
116  return *this;
117  }
118 
120  propagate_ |= GRPC_PROPAGATE_CANCELLATION;
121  return *this;
122  }
123 
125  propagate_ &= ~GRPC_PROPAGATE_CANCELLATION;
126  return *this;
127  }
128 
129  uint32_t c_bitmask() const { return propagate_; }
130 
131  private:
132  uint32_t propagate_;
133 };
134 
135 namespace testing {
136 class InteropClientContextInspector;
137 } // namespace testing
138 
154  public:
155  ClientContext();
156  ~ClientContext();
157 
168  static std::unique_ptr<ClientContext> FromServerContext(
169  const ServerContext& server_context,
171 
182  void AddMetadata(const grpc::string& meta_key,
183  const grpc::string& meta_value);
184 
193  const std::multimap<grpc::string_ref, grpc::string_ref>&
195  GPR_CODEGEN_ASSERT(initial_metadata_received_);
196  return *recv_initial_metadata_.map();
197  }
198 
205  const std::multimap<grpc::string_ref, grpc::string_ref>&
207  // TODO(yangg) check finished
208  return *trailing_metadata_.map();
209  }
210 
217  template <typename T>
218  void set_deadline(const T& deadline) {
219  TimePoint<T> deadline_tp(deadline);
220  deadline_ = deadline_tp.raw_time();
221  }
222 
228  void set_idempotent(bool idempotent) { idempotent_ = idempotent; }
229 
233  void set_cacheable(bool cacheable) { cacheable_ = cacheable; }
234 
241  void set_wait_for_ready(bool wait_for_ready) {
242  wait_for_ready_ = wait_for_ready;
243  wait_for_ready_explicitly_set_ = true;
244  }
245 
247  void set_fail_fast(bool fail_fast) { set_wait_for_ready(!fail_fast); }
248 
250  std::chrono::system_clock::time_point deadline() const {
251  return Timespec2Timepoint(deadline_);
252  }
253 
255  gpr_timespec raw_deadline() const { return deadline_; }
256 
259  void set_authority(const grpc::string& authority) { authority_ = authority; }
260 
264  std::shared_ptr<const AuthContext> auth_context() const {
265  if (auth_context_.get() == nullptr) {
266  auth_context_ = CreateAuthContext(call_);
267  }
268  return auth_context_;
269  }
270 
279  void set_credentials(const std::shared_ptr<CallCredentials>& creds) {
280  creds_ = creds;
281  }
282 
285  return compression_algorithm_;
286  }
287 
292 
300  void set_initial_metadata_corked(bool corked) {
301  initial_metadata_corked_ = corked;
302  }
303 
311  grpc::string peer() const;
312 
314  void set_census_context(struct census_context* ccp) { census_context_ = ccp; }
315  struct census_context* census_context() const {
316  return census_context_;
317  }
318 
324  void TryCancel();
325 
331  public:
332  virtual ~GlobalCallbacks() {}
333  virtual void DefaultConstructor(ClientContext* context) = 0;
334  virtual void Destructor(ClientContext* context) = 0;
335  };
336  static void SetGlobalCallbacks(GlobalCallbacks* callbacks);
337 
340  grpc_call* c_call() { return call_; }
341 
342  private:
343  // Disallow copy and assign.
345  ClientContext& operator=(const ClientContext&);
346 
347  friend class ::grpc::testing::InteropClientContextInspector;
350  friend class Channel;
351  template <class R>
352  friend class ::grpc::ClientReader;
353  template <class W>
354  friend class ::grpc::ClientWriter;
355  template <class W, class R>
356  friend class ::grpc::ClientReaderWriter;
357  template <class R>
358  friend class ::grpc::ClientAsyncReader;
359  template <class W>
360  friend class ::grpc::ClientAsyncWriter;
361  template <class W, class R>
362  friend class ::grpc::ClientAsyncReaderWriter;
363  template <class R>
364  friend class ::grpc::ClientAsyncResponseReader;
365  template <class InputMessage, class OutputMessage>
366  friend Status BlockingUnaryCall(ChannelInterface* channel,
367  const RpcMethod& method,
368  ClientContext* context,
369  const InputMessage& request,
370  OutputMessage* result);
371 
372  grpc_call* call() const { return call_; }
373  void set_call(grpc_call* call, const std::shared_ptr<Channel>& channel);
374 
375  uint32_t initial_metadata_flags() const {
376  return (idempotent_ ? GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST : 0) |
377  (wait_for_ready_ ? GRPC_INITIAL_METADATA_WAIT_FOR_READY : 0) |
378  (cacheable_ ? GRPC_INITIAL_METADATA_CACHEABLE_REQUEST : 0) |
379  (wait_for_ready_explicitly_set_
381  : 0) |
382  (initial_metadata_corked_ ? GRPC_INITIAL_METADATA_CORKED : 0);
383  }
384 
385  grpc::string authority() { return authority_; }
386 
387  bool initial_metadata_received_;
388  bool wait_for_ready_;
389  bool wait_for_ready_explicitly_set_;
390  bool idempotent_;
391  bool cacheable_;
392  std::shared_ptr<Channel> channel_;
393  std::mutex mu_;
394  grpc_call* call_;
395  bool call_canceled_;
396  gpr_timespec deadline_;
397  grpc::string authority_;
398  std::shared_ptr<CallCredentials> creds_;
399  mutable std::shared_ptr<const AuthContext> auth_context_;
400  struct census_context* census_context_;
401  std::multimap<grpc::string, grpc::string> send_initial_metadata_;
402  MetadataMap recv_initial_metadata_;
403  MetadataMap trailing_metadata_;
404 
405  grpc_call* propagate_from_call_;
406  PropagationOptions propagation_options_;
407 
408  grpc_compression_algorithm compression_algorithm_;
409  bool initial_metadata_corked_;
410 };
411 
412 } // namespace grpc
413 
414 #endif // GRPCXX_IMPL_CODEGEN_CLIENT_CONTEXT_H
struct grpc_call grpc_call
A Call represents an RPC.
Definition: grpc_types.h:71
std::shared_ptr< const AuthContext > CreateAuthContext(grpc_call *call)
void set_idempotent(bool idempotent)
EXPERIMENTAL: Indicate that this request is idempotent.
Definition: client_context.h:228
PropagationOptions & enable_census_tracing_propagation()
Definition: client_context.h:109
PropagationOptions & enable_deadline_propagation()
Definition: client_context.h:89
std::string string
Definition: config.h:35
void set_wait_for_ready(bool wait_for_ready)
EXPERIMENTAL: Trigger wait-for-ready or not on this request.
Definition: client_context.h:241
#define GPR_CODEGEN_ASSERT(x)
Codegen specific version of GPR_ASSERT.
Definition: core_codegen_interface.h:126
grpc_call * c_call()
Should be used for framework-level extensions only.
Definition: client_context.h:340
Definition: call.h:524
void set_deadline(const T &deadline)
Set the deadline for the client call.
Definition: client_context.h:218
grpc::string peer() const
Return the peer uri in a string.
void set_initial_metadata_corked(bool corked)
Flag whether the initial metadata should be corked.
Definition: client_context.h:300
PropagationOptions & disable_cancellation_propagation()
Definition: client_context.h:124
PropagationOptions & enable_census_stats_propagation()
Definition: client_context.h:99
#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:370
gpr_timespec raw_time()
Definition: time.h:41
If you are trying to use CompletionQueue::AsyncNext with a time class that isn't either gpr_timespec ...
Definition: time.h:38
PropagationOptions & enable_cancellation_propagation()
Definition: client_context.h:119
#define GRPC_INITIAL_METADATA_CORKED
Signal that the initial metadata should be corked.
Definition: grpc_types.h:372
virtual void Destructor(ClientContext *context)=0
A ClientContext allows the person implementing a service client to:
Definition: client_context.h:153
std::shared_ptr< const AuthContext > auth_context() const
Return the authentication context for this client call.
Definition: client_context.h:264
struct census_context * census_context() const
Definition: client_context.h:315
void TryCancel()
Send a best-effort out-of-band cancel on the call associated with this client context.
Options for ClientContext::FromServerContext specifying which traits from the ServerContext to propag...
Definition: client_context.h:85
void set_fail_fast(bool fail_fast)
DEPRECATED: Use set_wait_for_ready() instead.
Definition: client_context.h:247
void set_census_context(struct census_context *ccp)
Get and set census context.
Definition: client_context.h:314
#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:233
Global Callbacks.
Definition: client_context.h:330
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 to be used by the client call.
Definition: client_context.h:284
std::multimap< grpc::string_ref, grpc::string_ref > * map()
Definition: metadata_map.h:43
friend Status BlockingUnaryCall(ChannelInterface *channel, const RpcMethod &method, ClientContext *context, const InputMessage &request, OutputMessage *result)
Wrapper that performs a blocking unary call.
Definition: client_unary_call.h:37
Codegen interface for grpc::Channel.
Definition: channel_interface.h:49
#define GRPC_INITIAL_METADATA_CACHEABLE_REQUEST
Signal that the call is cacheable.
Definition: grpc_types.h:367
A ServerContext allows the person implementing a service handler to:
Definition: server_context.h:95
const std::multimap< grpc::string_ref, grpc::string_ref > & GetServerTrailingMetadata() const
Return a collection of trailing metadata key-value pairs.
Definition: client_context.h:206
static void SetGlobalCallbacks(GlobalCallbacks *callbacks)
PropagationOptions & disable_census_tracing_propagation()
Definition: client_context.h:114
#define GRPC_INITIAL_METADATA_WAIT_FOR_READY
Signal that the call should not return UNAVAILABLE before it has started.
Definition: grpc_types.h:365
Descriptor of an RPC method.
Definition: rpc_method.h:29
gpr_timespec raw_deadline() const
Return a gpr_timespec representation of the client call's deadline.
Definition: client_context.h:255
#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:259
std::chrono::system_clock::time_point deadline() const
Return the deadline for the client call.
Definition: client_context.h:250
Did it work? If it didn't, why?
Definition: status.h:30
Analogous to struct timespec.
Definition: gpr_types.h:47
std::chrono::system_clock::time_point Timespec2Timepoint(gpr_timespec t)
virtual void DefaultConstructor(ClientContext *context)=0
const std::multimap< grpc::string_ref, grpc::string_ref > & GetServerInitialMetadata() const
Return a collection of initial metadata key-value pairs.
Definition: client_context.h:194
#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:87
uint32_t c_bitmask() const
Definition: client_context.h:129
void set_credentials(const std::shared_ptr< CallCredentials > &creds)
Set credentials for the client call.
Definition: client_context.h:279
void set_compression_algorithm(grpc_compression_algorithm algorithm)
Set algorithm to be the compression algorithm used for the client call.
Definition: call.h:495
PropagationOptions & disable_deadline_propagation()
Definition: client_context.h:94
static std::unique_ptr< ClientContext > FromServerContext(const ServerContext &server_context, PropagationOptions options=PropagationOptions())
Create a new ClientContext as a child of an incoming server call, according to options (...
void AddMetadata(const grpc::string &meta_key, const grpc::string &meta_value)
Add the (meta_key, meta_value) pair to the metadata associated with a client call.
PropagationOptions & disable_census_stats_propagation()
Definition: client_context.h:104
virtual ~GlobalCallbacks()
Definition: client_context.h:332
#define GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST
Initial metadata flags.
Definition: grpc_types.h:363
#define GRPC_PROPAGATE_CANCELLATION
Propagate cancellation.
Definition: propagation_bits.h:36
Channels represent a connection to an endpoint. Created by CreateChannel.
Definition: channel.h:34