GRPC C++  0.12.0.0
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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_CLIENT_CONTEXT_H
50 #define GRPCXX_CLIENT_CONTEXT_H
51 
52 #include <map>
53 #include <memory>
54 #include <string>
55 
56 #include <grpc++/impl/sync.h>
58 #include <grpc++/support/config.h>
59 #include <grpc++/support/status.h>
61 #include <grpc++/support/time.h>
62 #include <grpc/compression.h>
63 #include <grpc/grpc.h>
64 #include <grpc/support/log.h>
65 #include <grpc/support/time.h>
66 
67 struct census_context;
68 
69 namespace grpc {
70 
71 class Channel;
72 class CompletionQueue;
73 class CallCredentials;
74 class RpcMethod;
75 template <class R>
76 class ClientReader;
77 template <class W>
78 class ClientWriter;
79 template <class R, class W>
80 class ClientReaderWriter;
81 template <class R>
82 class ClientAsyncReader;
83 template <class W>
84 class ClientAsyncWriter;
85 template <class R, class W>
86 class ClientAsyncReaderWriter;
87 template <class R>
88 class ClientAsyncResponseReader;
89 class ServerContext;
90 
97  public:
98  PropagationOptions() : propagate_(GRPC_PROPAGATE_DEFAULTS) {}
99 
101  propagate_ |= GRPC_PROPAGATE_DEADLINE;
102  return *this;
103  }
104 
106  propagate_ &= ~GRPC_PROPAGATE_DEADLINE;
107  return *this;
108  }
109 
111  propagate_ |= GRPC_PROPAGATE_CENSUS_STATS_CONTEXT;
112  return *this;
113  }
114 
116  propagate_ &= ~GRPC_PROPAGATE_CENSUS_STATS_CONTEXT;
117  return *this;
118  }
119 
121  propagate_ |= GRPC_PROPAGATE_CENSUS_TRACING_CONTEXT;
122  return *this;
123  }
124 
126  propagate_ &= ~GRPC_PROPAGATE_CENSUS_TRACING_CONTEXT;
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  gpr_uint32 c_bitmask() const { return propagate_; }
141 
142  private:
143  gpr_uint32 propagate_;
144 };
145 
146 namespace testing {
147 class InteropClientContextInspector;
148 } // namespace testing
149 
151  public:
152  ClientContext();
153  ~ClientContext();
154 
165  static std::unique_ptr<ClientContext> FromServerContext(
166  const ServerContext& server_context,
168 
180  void AddMetadata(const grpc::string& meta_key,
181  const grpc::string& meta_value);
182 
191  const std::multimap<grpc::string_ref, grpc::string_ref>&
193  GPR_ASSERT(initial_metadata_received_);
194  return recv_initial_metadata_;
195  }
196 
203  const std::multimap<grpc::string_ref, grpc::string_ref>&
205  // TODO(yangg) check finished
206  return trailing_metadata_;
207  }
208 
215  template <typename T>
216  void set_deadline(const T& deadline) {
217  TimePoint<T> deadline_tp(deadline);
218  deadline_ = deadline_tp.raw_time();
219  }
220 
221 #ifndef GRPC_CXX0X_NO_CHRONO
222  std::chrono::system_clock::time_point deadline() {
224  return Timespec2Timepoint(deadline_);
225  }
226 #endif // !GRPC_CXX0X_NO_CHRONO
227 
229  gpr_timespec raw_deadline() { return deadline_; }
230 
233  void set_authority(const grpc::string& authority) { authority_ = authority; }
234 
238  std::shared_ptr<const AuthContext> auth_context() const;
239 
248  void set_credentials(const std::shared_ptr<CallCredentials>& creds) {
249  creds_ = creds;
250  }
251 
253  grpc_compression_algorithm compression_algorithm() const {
254  return compression_algorithm_;
255  }
256 
260  void set_compression_algorithm(grpc_compression_algorithm algorithm);
261 
269  grpc::string peer() const;
270 
272  void set_census_context(struct census_context* ccp) { census_context_ = ccp; }
273  struct census_context* census_context() const {
274  return census_context_;
275  }
276 
281  void TryCancel();
282 
288  public:
289  virtual void DefaultConstructor(ClientContext* context) = 0;
290  virtual void Destructor(ClientContext* context) = 0;
291  };
292  static void SetGlobalCallbacks(GlobalCallbacks* callbacks);
293 
294  private:
295  // Disallow copy and assign.
297  ClientContext& operator=(const ClientContext&);
298 
299  friend class ::grpc::testing::InteropClientContextInspector;
302  friend class Channel;
303  template <class R>
304  friend class ::grpc::ClientReader;
305  template <class W>
306  friend class ::grpc::ClientWriter;
307  template <class R, class W>
308  friend class ::grpc::ClientReaderWriter;
309  template <class R>
310  friend class ::grpc::ClientAsyncReader;
311  template <class W>
312  friend class ::grpc::ClientAsyncWriter;
313  template <class R, class W>
314  friend class ::grpc::ClientAsyncReaderWriter;
315  template <class R>
316  friend class ::grpc::ClientAsyncResponseReader;
317  template <class InputMessage, class OutputMessage>
318  friend Status BlockingUnaryCall(Channel* channel, const RpcMethod& method,
319  ClientContext* context,
320  const InputMessage& request,
321  OutputMessage* result);
322 
323  grpc_call* call() { return call_; }
324  void set_call(grpc_call* call, const std::shared_ptr<Channel>& channel);
325 
326  grpc::string authority() { return authority_; }
327 
328  bool initial_metadata_received_;
329  std::shared_ptr<Channel> channel_;
330  grpc::mutex mu_;
331  grpc_call* call_;
332  bool call_canceled_;
333  gpr_timespec deadline_;
334  grpc::string authority_;
335  std::shared_ptr<CallCredentials> creds_;
336  mutable std::shared_ptr<const AuthContext> auth_context_;
337  struct census_context* census_context_;
338  std::multimap<grpc::string, grpc::string> send_initial_metadata_;
339  std::multimap<grpc::string_ref, grpc::string_ref> recv_initial_metadata_;
340  std::multimap<grpc::string_ref, grpc::string_ref> trailing_metadata_;
341 
342  grpc_call* propagate_from_call_;
343  PropagationOptions propagation_options_;
344 
345  grpc_compression_algorithm compression_algorithm_;
346 };
347 
348 } // namespace grpc
349 
350 #endif // GRPCXX_CLIENT_CONTEXT_H
PropagationOptions & enable_census_tracing_propagation()
Definition: client_context.h:120
PropagationOptions & enable_deadline_propagation()
Definition: client_context.h:100
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:223
const std::multimap< grpc::string_ref, grpc::string_ref > & GetServerTrailingMetadata()
Return a collection of trailing metadata key-value pairs.
Definition: client_context.h:204
Definition: call.h:425
void set_deadline(const T &deadline)
Set the deadline for the client call.
Definition: client_context.h:216
Definition: sync_no_cxx11.h:45
grpc::string peer() const
Return the peer uri in a string.
PropagationOptions & disable_cancellation_propagation()
Definition: client_context.h:135
PropagationOptions & enable_census_stats_propagation()
Definition: client_context.h:110
gpr_timespec raw_time()
Definition: time.h:57
Definition: time.h:54
PropagationOptions & enable_cancellation_propagation()
Definition: client_context.h:130
virtual void Destructor(ClientContext *context)=0
Definition: client_context.h:150
std::shared_ptr< const AuthContext > auth_context() const
Return the authentication context for this client call.
struct census_context * census_context() const
Definition: client_context.h:273
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:96
void set_census_context(struct census_context *ccp)
Get and set census context.
Definition: client_context.h:272
const std::multimap< grpc::string_ref, grpc::string_ref > & GetServerInitialMetadata()
Return a collection of initial metadata key-value pairs.
Definition: client_context.h:192
Global Callbacks.
Definition: client_context.h:287
grpc_compression_algorithm compression_algorithm() const
Return the compression algorithm to be used by the client call.
Definition: client_context.h:253
gpr_uint32 c_bitmask() const
Definition: client_context.h:140
Definition: server_context.h:89
static void SetGlobalCallbacks(GlobalCallbacks *callbacks)
PropagationOptions & disable_census_tracing_propagation()
Definition: client_context.h:125
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
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:233
gpr_timespec raw_deadline()
Return a gpr_timespec representation of the client call's deadline.
Definition: client_context.h:229
Did it work? If it didn't, why?
Definition: status.h:45
std::chrono::system_clock::time_point Timespec2Timepoint(gpr_timespec t)
virtual void DefaultConstructor(ClientContext *context)=0
PropagationOptions()
Definition: client_context.h:98
void set_credentials(const std::shared_ptr< CallCredentials > &creds)
Set credentials for the client call.
Definition: client_context.h:248
void set_compression_algorithm(grpc_compression_algorithm algorithm)
Set algorithm to be the compression algorithm used for the client call.
Definition: call.h:395
PropagationOptions & disable_deadline_propagation()
Definition: client_context.h:105
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:115
Channels represent a connection to an endpoint. Created by CreateChannel.
Definition: channel.h:69