GRPC C++  0.11.0.0
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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_CLIENT_CONTEXT_H
50 #define GRPCXX_CLIENT_CONTEXT_H
51 
52 #include <map>
53 #include <memory>
54 #include <string>
55 
56 #include <grpc/compression.h>
57 #include <grpc/grpc.h>
58 #include <grpc/support/log.h>
59 #include <grpc/support/time.h>
61 #include <grpc++/support/config.h>
62 #include <grpc++/support/status.h>
64 #include <grpc++/support/time.h>
65 
66 struct census_context;
67 
68 namespace grpc {
69 
70 class Channel;
71 class CompletionQueue;
72 class Credentials;
73 class RpcMethod;
74 template <class R>
75 class ClientReader;
76 template <class W>
77 class ClientWriter;
78 template <class R, class W>
79 class ClientReaderWriter;
80 template <class R>
81 class ClientAsyncReader;
82 template <class W>
83 class ClientAsyncWriter;
84 template <class R, class W>
85 class ClientAsyncReaderWriter;
86 template <class R>
87 class ClientAsyncResponseReader;
88 class ServerContext;
89 
96  public:
97  PropagationOptions() : propagate_(GRPC_PROPAGATE_DEFAULTS) {}
98 
100  propagate_ |= GRPC_PROPAGATE_DEADLINE;
101  return *this;
102  }
103 
105  propagate_ &= ~GRPC_PROPAGATE_DEADLINE;
106  return *this;
107  }
108 
110  propagate_ |= GRPC_PROPAGATE_CENSUS_STATS_CONTEXT;
111  return *this;
112  }
113 
115  propagate_ &= ~GRPC_PROPAGATE_CENSUS_STATS_CONTEXT;
116  return *this;
117  }
118 
120  propagate_ |= GRPC_PROPAGATE_CENSUS_TRACING_CONTEXT;
121  return *this;
122  }
123 
125  propagate_ &= ~GRPC_PROPAGATE_CENSUS_TRACING_CONTEXT;
126  return *this;
127  }
128 
130  propagate_ |= GRPC_PROPAGATE_CANCELLATION;
131  return *this;
132  }
133 
135  propagate_ &= ~GRPC_PROPAGATE_CANCELLATION;
136  return *this;
137  }
138 
139  gpr_uint32 c_bitmask() const { return propagate_; }
140 
141  private:
142  gpr_uint32 propagate_;
143 };
144 
145 namespace testing {
146 class InteropClientContextInspector;
147 } // namespace testing
148 
150  public:
151  ClientContext();
152  ~ClientContext();
153 
164  static std::unique_ptr<ClientContext> FromServerContext(
165  const ServerContext& server_context,
167 
179  void AddMetadata(const grpc::string& meta_key,
180  const grpc::string& meta_value);
181 
190  const std::multimap<grpc::string_ref, grpc::string_ref>&
192  GPR_ASSERT(initial_metadata_received_);
193  return recv_initial_metadata_;
194  }
195 
202  const std::multimap<grpc::string_ref, grpc::string_ref>&
204  // TODO(yangg) check finished
205  return trailing_metadata_;
206  }
207 
214  template <typename T>
215  void set_deadline(const T& deadline) {
216  TimePoint<T> deadline_tp(deadline);
217  deadline_ = deadline_tp.raw_time();
218  }
219 
220 #ifndef GRPC_CXX0X_NO_CHRONO
221  std::chrono::system_clock::time_point deadline() {
223  return Timespec2Timepoint(deadline_);
224  }
225 #endif // !GRPC_CXX0X_NO_CHRONO
226 
228  gpr_timespec raw_deadline() { return deadline_; }
229 
232  void set_authority(const grpc::string& authority) { authority_ = authority; }
233 
237  std::shared_ptr<const AuthContext> auth_context() const;
238 
247  void set_credentials(const std::shared_ptr<Credentials>& creds) {
248  creds_ = creds;
249  }
250 
252  grpc_compression_algorithm compression_algorithm() const {
253  return compression_algorithm_;
254  }
255 
259  void set_compression_algorithm(grpc_compression_algorithm algorithm);
260 
268  grpc::string peer() const;
269 
271  void set_census_context(struct census_context* ccp) { census_context_ = ccp; }
272  struct census_context* census_context() const {
273  return census_context_;
274  }
275 
280  void TryCancel();
281 
282  private:
283  // Disallow copy and assign.
285  ClientContext& operator=(const ClientContext&);
286 
287  friend class ::grpc::testing::InteropClientContextInspector;
290  friend class Channel;
291  template <class R>
292  friend class ::grpc::ClientReader;
293  template <class W>
294  friend class ::grpc::ClientWriter;
295  template <class R, class W>
296  friend class ::grpc::ClientReaderWriter;
297  template <class R>
298  friend class ::grpc::ClientAsyncReader;
299  template <class W>
300  friend class ::grpc::ClientAsyncWriter;
301  template <class R, class W>
302  friend class ::grpc::ClientAsyncReaderWriter;
303  template <class R>
304  friend class ::grpc::ClientAsyncResponseReader;
305  template <class InputMessage, class OutputMessage>
306  friend Status BlockingUnaryCall(Channel* channel, const RpcMethod& method,
307  ClientContext* context,
308  const InputMessage& request,
309  OutputMessage* result);
310 
311  grpc_call* call() { return call_; }
312  void set_call(grpc_call* call, const std::shared_ptr<Channel>& channel);
313 
314  grpc::string authority() { return authority_; }
315 
316  bool initial_metadata_received_;
317  std::shared_ptr<Channel> channel_;
318  grpc_call* call_;
319  gpr_timespec deadline_;
320  grpc::string authority_;
321  std::shared_ptr<Credentials> creds_;
322  mutable std::shared_ptr<const AuthContext> auth_context_;
323  struct census_context* census_context_;
324  std::multimap<grpc::string, grpc::string> send_initial_metadata_;
325  std::multimap<grpc::string_ref, grpc::string_ref> recv_initial_metadata_;
326  std::multimap<grpc::string_ref, grpc::string_ref> trailing_metadata_;
327 
328  grpc_call* propagate_from_call_;
329  PropagationOptions propagation_options_;
330 
331  grpc_compression_algorithm compression_algorithm_;
332 };
333 
334 } // namespace grpc
335 
336 #endif // GRPCXX_CLIENT_CONTEXT_H
PropagationOptions & enable_census_tracing_propagation()
Definition: client_context.h:119
PropagationOptions & enable_deadline_propagation()
Definition: client_context.h:99
std::string string
Definition: config.h:112
std::chrono::system_clock::time_point deadline()
Return the deadline for the client call.
Definition: client_context.h:222
const std::multimap< grpc::string_ref, grpc::string_ref > & GetServerTrailingMetadata()
Return a collection of trailing metadata key-value pairs.
Definition: client_context.h:203
Definition: call.h:426
void set_deadline(const T &deadline)
Set the deadline for the client call.
Definition: client_context.h:215
grpc::string peer() const
Return the peer uri in a string.
Definition: client_context.cc:109
PropagationOptions & disable_cancellation_propagation()
Definition: client_context.h:134
std::shared_ptr< const AuthContext > auth_context() const
Return the authentication context for this client call.
Definition: client_context.cc:96
PropagationOptions & enable_census_stats_propagation()
Definition: client_context.h:109
gpr_timespec raw_time()
Definition: time.h:56
Definition: time.h:53
PropagationOptions & enable_cancellation_propagation()
Definition: client_context.h:129
Definition: client_context.h:149
ClientContext()
Definition: client_context.cc:48
struct census_context * census_context() const
Definition: client_context.h:272
void TryCancel()
Send a best-effort out-of-band cancel.
Definition: client_context.cc:103
Options for ClientContext::FromServerContext specifying which traits from the ServerContext to propag...
Definition: client_context.h:95
void set_census_context(struct census_context *ccp)
Get and set census context.
Definition: client_context.h:271
const std::multimap< grpc::string_ref, grpc::string_ref > & GetServerInitialMetadata()
Return a collection of initial metadata key-value pairs.
Definition: client_context.h:191
grpc_compression_algorithm compression_algorithm() const
Return the compression algorithm to be used by the client call.
Definition: client_context.h:252
std::chrono::system_clock::time_point Timespec2Timepoint(gpr_timespec t)
Definition: time.cc:81
gpr_uint32 c_bitmask() const
Definition: client_context.h:139
Definition: server_context.h:89
PropagationOptions & disable_census_tracing_propagation()
Definition: client_context.h:124
friend Status BlockingUnaryCall(Channel *channel, const RpcMethod &method, ClientContext *context, const InputMessage &request, OutputMessage *result)
Definition: client_unary_call.h:50
Definition: rpc_method.h:43
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 (...
Definition: client_context.cc:60
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:232
gpr_timespec raw_deadline()
Return a gpr_timespec representation of the client call's deadline.
Definition: client_context.h:228
Did it work? If it didn't, why?
Definition: status.h:45
PropagationOptions()
Definition: client_context.h:97
void set_compression_algorithm(grpc_compression_algorithm algorithm)
Set algorithm to be the compression algorithm used for the client call.
Definition: client_context.cc:84
Definition: call.h:396
PropagationOptions & disable_deadline_propagation()
Definition: client_context.h:104
void set_credentials(const std::shared_ptr< Credentials > &creds)
Set credentials for the client call.
Definition: client_context.h:247
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.
Definition: client_context.cc:68
PropagationOptions & disable_census_stats_propagation()
Definition: client_context.h:114
Channels represent a connection to an endpoint. Created by CreateChannel.
Definition: channel.h:69
~ClientContext()
Definition: client_context.cc:54