GRPC C++  1.2.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, Google Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above
13  * copyright notice, this list of conditions and the following disclaimer
14  * in the documentation and/or other materials provided with the
15  * distribution.
16  * * Neither the name of Google Inc. nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  */
33 
48 
49 #ifndef GRPCXX_IMPL_CODEGEN_CLIENT_CONTEXT_H
50 #define GRPCXX_IMPL_CODEGEN_CLIENT_CONTEXT_H
51 
52 #include <map>
53 #include <memory>
54 #include <mutex>
55 #include <string>
56 
68 
69 struct census_context;
70 struct grpc_call;
71 
72 namespace grpc {
73 
74 class Channel;
75 class ChannelInterface;
76 class CompletionQueue;
77 class CallCredentials;
78 class RpcMethod;
79 template <class R>
80 class ClientReader;
81 template <class W>
82 class ClientWriter;
83 template <class W, class R>
84 class ClientReaderWriter;
85 template <class R>
86 class ClientAsyncReader;
87 template <class W>
88 class ClientAsyncWriter;
89 template <class W, class R>
90 class ClientAsyncReaderWriter;
91 template <class R>
92 class ClientAsyncResponseReader;
93 class ServerContext;
94 
101  public:
103 
105  propagate_ |= GRPC_PROPAGATE_DEADLINE;
106  return *this;
107  }
108 
110  propagate_ &= ~GRPC_PROPAGATE_DEADLINE;
111  return *this;
112  }
113 
116  return *this;
117  }
118 
121  return *this;
122  }
123 
126  return *this;
127  }
128 
131  return *this;
132  }
133 
135  propagate_ |= GRPC_PROPAGATE_CANCELLATION;
136  return *this;
137  }
138 
140  propagate_ &= ~GRPC_PROPAGATE_CANCELLATION;
141  return *this;
142  }
143 
144  uint32_t c_bitmask() const { return propagate_; }
145 
146  private:
147  uint32_t propagate_;
148 };
149 
150 namespace testing {
151 class InteropClientContextInspector;
152 } // namespace testing
153 
155  public:
156  ClientContext();
157  ~ClientContext();
158 
169  static std::unique_ptr<ClientContext> FromServerContext(
170  const ServerContext& server_context,
172 
184  void AddMetadata(const grpc::string& meta_key,
185  const grpc::string& meta_value);
186 
195  const std::multimap<grpc::string_ref, grpc::string_ref>&
197  GPR_CODEGEN_ASSERT(initial_metadata_received_);
198  return *recv_initial_metadata_.map();
199  }
200 
207  const std::multimap<grpc::string_ref, grpc::string_ref>&
209  // TODO(yangg) check finished
210  return *trailing_metadata_.map();
211  }
212 
219  template <typename T>
220  void set_deadline(const T& deadline) {
221  TimePoint<T> deadline_tp(deadline);
222  deadline_ = deadline_tp.raw_time();
223  }
224 
226  void set_idempotent(bool idempotent) { idempotent_ = idempotent; }
227 
229  void set_cacheable(bool cacheable) { cacheable_ = cacheable; }
230 
232  void set_wait_for_ready(bool wait_for_ready) {
233  wait_for_ready_ = wait_for_ready;
234  wait_for_ready_explicitly_set_ = true;
235  }
236 
238  void set_fail_fast(bool fail_fast) { set_wait_for_ready(!fail_fast); }
239 
241  std::chrono::system_clock::time_point deadline() const {
242  return Timespec2Timepoint(deadline_);
243  }
244 
246  gpr_timespec raw_deadline() const { return deadline_; }
247 
250  void set_authority(const grpc::string& authority) { authority_ = authority; }
251 
255  std::shared_ptr<const AuthContext> auth_context() const {
256  if (auth_context_.get() == nullptr) {
257  auth_context_ = CreateAuthContext(call_);
258  }
259  return auth_context_;
260  }
261 
270  void set_credentials(const std::shared_ptr<CallCredentials>& creds) {
271  creds_ = creds;
272  }
273 
276  return compression_algorithm_;
277  }
278 
283 
291  grpc::string peer() const;
292 
294  void set_census_context(struct census_context* ccp) { census_context_ = ccp; }
295  struct census_context* census_context() const {
296  return census_context_;
297  }
298 
303  void TryCancel();
304 
310  public:
311  virtual ~GlobalCallbacks() {}
312  virtual void DefaultConstructor(ClientContext* context) = 0;
313  virtual void Destructor(ClientContext* context) = 0;
314  };
315  static void SetGlobalCallbacks(GlobalCallbacks* callbacks);
316 
317  // Should be used for framework-level extensions only.
318  // Applications never need to call this method.
319  grpc_call* c_call() { return call_; }
320 
321  private:
322  // Disallow copy and assign.
324  ClientContext& operator=(const ClientContext&);
325 
326  friend class ::grpc::testing::InteropClientContextInspector;
329  friend class Channel;
330  template <class R>
331  friend class ::grpc::ClientReader;
332  template <class W>
333  friend class ::grpc::ClientWriter;
334  template <class W, class R>
335  friend class ::grpc::ClientReaderWriter;
336  template <class R>
337  friend class ::grpc::ClientAsyncReader;
338  template <class W>
339  friend class ::grpc::ClientAsyncWriter;
340  template <class W, class R>
341  friend class ::grpc::ClientAsyncReaderWriter;
342  template <class R>
343  friend class ::grpc::ClientAsyncResponseReader;
344  template <class InputMessage, class OutputMessage>
345  friend Status BlockingUnaryCall(ChannelInterface* channel,
346  const RpcMethod& method,
347  ClientContext* context,
348  const InputMessage& request,
349  OutputMessage* result);
350 
351  grpc_call* call() const { return call_; }
352  void set_call(grpc_call* call, const std::shared_ptr<Channel>& channel);
353 
354  uint32_t initial_metadata_flags() const {
355  return (idempotent_ ? GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST : 0) |
356  (wait_for_ready_ ? GRPC_INITIAL_METADATA_WAIT_FOR_READY : 0) |
357  (cacheable_ ? GRPC_INITIAL_METADATA_CACHEABLE_REQUEST : 0) |
358  (wait_for_ready_explicitly_set_
360  : 0);
361  }
362 
363  grpc::string authority() { return authority_; }
364 
365  bool initial_metadata_received_;
366  bool wait_for_ready_;
367  bool wait_for_ready_explicitly_set_;
368  bool idempotent_;
369  bool cacheable_;
370  std::shared_ptr<Channel> channel_;
371  std::mutex mu_;
372  grpc_call* call_;
373  bool call_canceled_;
374  gpr_timespec deadline_;
375  grpc::string authority_;
376  std::shared_ptr<CallCredentials> creds_;
377  mutable std::shared_ptr<const AuthContext> auth_context_;
378  struct census_context* census_context_;
379  std::multimap<grpc::string, grpc::string> send_initial_metadata_;
380  MetadataMap recv_initial_metadata_;
381  MetadataMap trailing_metadata_;
382 
383  grpc_call* propagate_from_call_;
384  PropagationOptions propagation_options_;
385 
386  grpc_compression_algorithm compression_algorithm_;
387 };
388 
389 } // namespace grpc
390 
391 #endif // GRPCXX_IMPL_CODEGEN_CLIENT_CONTEXT_H
struct grpc_call grpc_call
A Call represents an RPC.
Definition: grpc_types.h:85
std::shared_ptr< const AuthContext > CreateAuthContext(grpc_call *call)
void set_idempotent(bool idempotent)
EXPERIMENTAL: Set this request to be idempotent.
Definition: client_context.h:226
PropagationOptions & enable_census_tracing_propagation()
Definition: client_context.h:124
PropagationOptions & enable_deadline_propagation()
Definition: client_context.h:104
std::string string
Definition: config.h:50
void set_wait_for_ready(bool wait_for_ready)
EXPERIMENTAL: Trigger wait-for-ready or not on this request.
Definition: client_context.h:232
#define GPR_CODEGEN_ASSERT(x)
Codegen specific version of GPR_ASSERT.
Definition: core_codegen_interface.h:121
grpc_call * c_call()
Definition: client_context.h:319
Definition: call.h:486
void set_deadline(const T &deadline)
Set the deadline for the client call.
Definition: client_context.h:220
grpc::string peer() const
Return the peer uri in a string.
PropagationOptions & disable_cancellation_propagation()
Definition: client_context.h:139
PropagationOptions & enable_census_stats_propagation()
Definition: client_context.h:114
#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:313
gpr_timespec raw_time()
Definition: time.h:57
Definition: time.h:54
PropagationOptions & enable_cancellation_propagation()
Definition: client_context.h:134
virtual void Destructor(ClientContext *context)=0
Definition: client_context.h:154
std::shared_ptr< const AuthContext > auth_context() const
Return the authentication context for this client call.
Definition: client_context.h:255
struct census_context * census_context() const
Definition: client_context.h:295
void TryCancel()
Send a best-effort out-of-band cancel.
Options for ClientContext::FromServerContext specifying which traits from the ServerContext to propag...
Definition: client_context.h:100
void set_fail_fast(bool fail_fast)
DEPRECATED: Use set_wait_for_ready() instead.
Definition: client_context.h:238
void set_census_context(struct census_context *ccp)
Get and set census context.
Definition: client_context.h:294
#define GRPC_PROPAGATE_CENSUS_TRACING_CONTEXT
Definition: propagation_bits.h:49
void set_cacheable(bool cacheable)
EXPERIMENTAL: Set this request to be cacheable.
Definition: client_context.h:229
Global Callbacks.
Definition: client_context.h:309
grpc_compression_algorithm
Definition: compression_types.h:72
grpc_compression_algorithm compression_algorithm() const
Return the compression algorithm to be used by the client call.
Definition: client_context.h:275
std::multimap< grpc::string_ref, grpc::string_ref > * map()
Definition: metadata_map.h:58
friend Status BlockingUnaryCall(ChannelInterface *channel, const RpcMethod &method, ClientContext *context, const InputMessage &request, OutputMessage *result)
Definition: client_unary_call.h:52
Codegen interface for grpc::Channel.
Definition: channel_interface.h:64
#define GRPC_INITIAL_METADATA_CACHEABLE_REQUEST
Signal that the call is cacheable.
Definition: grpc_types.h:310
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:208
static void SetGlobalCallbacks(GlobalCallbacks *callbacks)
PropagationOptions & disable_census_tracing_propagation()
Definition: client_context.h:129
#define GRPC_INITIAL_METADATA_WAIT_FOR_READY
Signal that the call should not return UNAVAILABLE before it has started.
Definition: grpc_types.h:308
Definition: rpc_method.h:43
gpr_timespec raw_deadline() const
Return a gpr_timespec representation of the client call's deadline.
Definition: client_context.h:246
#define GRPC_PROPAGATE_DEFAULTS
Definition: propagation_bits.h:58
#define GRPC_PROPAGATE_CENSUS_STATS_CONTEXT
Propagate census context.
Definition: propagation_bits.h:48
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:250
std::chrono::system_clock::time_point deadline() const
Return the deadline for the client call.
Definition: client_context.h:241
Did it work? If it didn't, why?
Definition: status.h:45
Definition: gpr_types.h:63
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:196
#define GRPC_PROPAGATE_DEADLINE
Propagate deadline.
Definition: propagation_bits.h:46
PropagationOptions()
Definition: client_context.h:102
uint32_t c_bitmask() const
Definition: client_context.h:144
void set_credentials(const std::shared_ptr< CallCredentials > &creds)
Set credentials for the client call.
Definition: client_context.h:270
void set_compression_algorithm(grpc_compression_algorithm algorithm)
Set algorithm to be the compression algorithm used for the client call.
Definition: call.h:457
PropagationOptions & disable_deadline_propagation()
Definition: client_context.h:109
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:119
virtual ~GlobalCallbacks()
Definition: client_context.h:311
#define GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST
Signal that the call is idempotent.
Definition: grpc_types.h:306
#define GRPC_PROPAGATE_CANCELLATION
Propagate cancellation.
Definition: propagation_bits.h:51
Channels represent a connection to an endpoint. Created by CreateChannel.
Definition: channel.h:49