Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(441)

Side by Side Diff: third_party/grpc/include/grpc++/impl/codegen/client_context.h

Issue 1932353002: Initial checkin of gRPC to third_party/ Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 *
3 * Copyright 2015-2016, 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
34 /// A ClientContext allows the person implementing a service client to:
35 ///
36 /// - Add custom metadata key-value pairs that will propagated to the server
37 /// side.
38 /// - Control call settings such as compression and authentication.
39 /// - Initial and trailing metadata coming from the server.
40 /// - Get performance metrics (ie, census).
41 ///
42 /// Context settings are only relevant to the call they are invoked with, that
43 /// is to say, they aren't sticky. Some of these settings, such as the
44 /// compression options, can be made persistant at channel construction time
45 /// (see \a grpc::CreateCustomChannel).
46 ///
47 /// \warning ClientContext instances should \em not be reused across rpcs.
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 <string>
55
56 #include <grpc++/impl/codegen/config.h>
57 #include <grpc++/impl/codegen/security/auth_context.h>
58 #include <grpc++/impl/codegen/status.h>
59 #include <grpc++/impl/codegen/string_ref.h>
60 #include <grpc++/impl/codegen/sync.h>
61 #include <grpc++/impl/codegen/time.h>
62 #include <grpc/impl/codegen/compression_types.h>
63 #include <grpc/impl/codegen/log.h>
64 #include <grpc/impl/codegen/propagation_bits.h>
65 #include <grpc/impl/codegen/time.h>
66
67 struct census_context;
68 struct grpc_call;
69
70 namespace grpc {
71
72 class Channel;
73 class ChannelInterface;
74 class CompletionQueue;
75 class CallCredentials;
76 class RpcMethod;
77 template <class R>
78 class ClientReader;
79 template <class W>
80 class ClientWriter;
81 template <class W, class R>
82 class ClientReaderWriter;
83 template <class R>
84 class ClientAsyncReader;
85 template <class W>
86 class ClientAsyncWriter;
87 template <class W, class R>
88 class ClientAsyncReaderWriter;
89 template <class R>
90 class ClientAsyncResponseReader;
91 class ServerContext;
92
93 /// Options for \a ClientContext::FromServerContext specifying which traits from
94 /// the \a ServerContext to propagate (copy) from it into a new \a
95 /// ClientContext.
96 ///
97 /// \see ClientContext::FromServerContext
98 class PropagationOptions {
99 public:
100 PropagationOptions() : propagate_(GRPC_PROPAGATE_DEFAULTS) {}
101
102 PropagationOptions& enable_deadline_propagation() {
103 propagate_ |= GRPC_PROPAGATE_DEADLINE;
104 return *this;
105 }
106
107 PropagationOptions& disable_deadline_propagation() {
108 propagate_ &= ~GRPC_PROPAGATE_DEADLINE;
109 return *this;
110 }
111
112 PropagationOptions& enable_census_stats_propagation() {
113 propagate_ |= GRPC_PROPAGATE_CENSUS_STATS_CONTEXT;
114 return *this;
115 }
116
117 PropagationOptions& disable_census_stats_propagation() {
118 propagate_ &= ~GRPC_PROPAGATE_CENSUS_STATS_CONTEXT;
119 return *this;
120 }
121
122 PropagationOptions& enable_census_tracing_propagation() {
123 propagate_ |= GRPC_PROPAGATE_CENSUS_TRACING_CONTEXT;
124 return *this;
125 }
126
127 PropagationOptions& disable_census_tracing_propagation() {
128 propagate_ &= ~GRPC_PROPAGATE_CENSUS_TRACING_CONTEXT;
129 return *this;
130 }
131
132 PropagationOptions& enable_cancellation_propagation() {
133 propagate_ |= GRPC_PROPAGATE_CANCELLATION;
134 return *this;
135 }
136
137 PropagationOptions& disable_cancellation_propagation() {
138 propagate_ &= ~GRPC_PROPAGATE_CANCELLATION;
139 return *this;
140 }
141
142 uint32_t c_bitmask() const { return propagate_; }
143
144 private:
145 uint32_t propagate_;
146 };
147
148 namespace testing {
149 class InteropClientContextInspector;
150 } // namespace testing
151
152 class ClientContext {
153 public:
154 ClientContext();
155 ~ClientContext();
156
157 /// Create a new \a ClientContext as a child of an incoming server call,
158 /// according to \a options (\see PropagationOptions).
159 ///
160 /// \param server_context The source server context to use as the basis for
161 /// constructing the client context.
162 /// \param options The options controlling what to copy from the \a
163 /// server_context.
164 ///
165 /// \return A newly constructed \a ClientContext instance based on \a
166 /// server_context, with traits propagated (copied) according to \a options.
167 static std::unique_ptr<ClientContext> FromServerContext(
168 const ServerContext& server_context,
169 PropagationOptions options = PropagationOptions());
170
171 /// Add the (\a meta_key, \a meta_value) pair to the metadata associated with
172 /// a client call. These are made available at the server side by the \a
173 /// grpc::ServerContext::client_metadata() method.
174 ///
175 /// \warning This method should only be called before invoking the rpc.
176 ///
177 /// \param meta_key The metadata key. If \a meta_value is binary data, it must
178 /// end in "-bin".
179 /// \param meta_value The metadata value. If its value is binary, it must be
180 /// base64-encoding (see https://tools.ietf.org/html/rfc4648#section-4) and \a
181 /// meta_key must end in "-bin".
182 void AddMetadata(const grpc::string& meta_key,
183 const grpc::string& meta_value);
184
185 /// Return a collection of initial metadata key-value pairs. Note that keys
186 /// may happen more than once (ie, a \a std::multimap is returned).
187 ///
188 /// \warning This method should only be called after initial metadata has been
189 /// received. For streaming calls, see \a
190 /// ClientReaderInterface::WaitForInitialMetadata().
191 ///
192 /// \return A multimap of initial metadata key-value pairs from the server.
193 const std::multimap<grpc::string_ref, grpc::string_ref>&
194 GetServerInitialMetadata() {
195 GPR_ASSERT(initial_metadata_received_);
196 return recv_initial_metadata_;
197 }
198
199 /// Return a collection of trailing metadata key-value pairs. Note that keys
200 /// may happen more than once (ie, a \a std::multimap is returned).
201 ///
202 /// \warning This method is only callable once the stream has finished.
203 ///
204 /// \return A multimap of metadata trailing key-value pairs from the server.
205 const std::multimap<grpc::string_ref, grpc::string_ref>&
206 GetServerTrailingMetadata() {
207 // TODO(yangg) check finished
208 return trailing_metadata_;
209 }
210
211 /// Set the deadline for the client call.
212 ///
213 /// \warning This method should only be called before invoking the rpc.
214 ///
215 /// \param deadline the deadline for the client call. Units are determined by
216 /// the type used.
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
223 #ifndef GRPC_CXX0X_NO_CHRONO
224 /// Return the deadline for the client call.
225 std::chrono::system_clock::time_point deadline() {
226 return Timespec2Timepoint(deadline_);
227 }
228 #endif // !GRPC_CXX0X_NO_CHRONO
229
230 /// Return a \a gpr_timespec representation of the client call's deadline.
231 gpr_timespec raw_deadline() { return deadline_; }
232
233 /// Set the per call authority header (see
234 /// https://tools.ietf.org/html/rfc7540#section-8.1.2.3).
235 void set_authority(const grpc::string& authority) { authority_ = authority; }
236
237 /// Return the authentication context for this client call.
238 ///
239 /// \see grpc::AuthContext.
240 std::shared_ptr<const AuthContext> auth_context() const;
241
242 /// Set credentials for the client call.
243 ///
244 /// A credentials object encapsulates all the state needed by a client to
245 /// authenticate with a server and make various assertions, e.g., about the
246 /// client’s identity, role, or whether it is authorized to make a particular
247 /// call.
248 ///
249 /// \see http://www.grpc.io/docs/guides/auth.html
250 void set_credentials(const std::shared_ptr<CallCredentials>& creds) {
251 creds_ = creds;
252 }
253
254 /// Return the compression algorithm to be used by the client call.
255 grpc_compression_algorithm compression_algorithm() const {
256 return compression_algorithm_;
257 }
258
259 /// Set \a algorithm to be the compression algorithm used for the client call.
260 ///
261 /// \param algorith The compression algorithm used for the client call.
262 void set_compression_algorithm(grpc_compression_algorithm algorithm);
263
264 /// Return the peer uri in a string.
265 ///
266 /// \warning This value is never authenticated or subject to any security
267 /// related code. It must not be used for any authentication related
268 /// functionality. Instead, use auth_context.
269 ///
270 /// \return The call's peer URI.
271 grpc::string peer() const;
272
273 /// Get and set census context.
274 void set_census_context(struct census_context* ccp) { census_context_ = ccp; }
275 struct census_context* census_context() const {
276 return census_context_;
277 }
278
279 /// Send a best-effort out-of-band cancel. The call could be in any stage.
280 /// e.g. if it is already finished, it may still return success.
281 ///
282 /// There is no guarantee the call will be cancelled.
283 void TryCancel();
284
285 /// Global Callbacks
286 ///
287 /// Can be set exactly once per application to install hooks whenever
288 /// a client context is constructed and destructed.
289 class GlobalCallbacks {
290 public:
291 virtual ~GlobalCallbacks() {}
292 virtual void DefaultConstructor(ClientContext* context) = 0;
293 virtual void Destructor(ClientContext* context) = 0;
294 };
295 static void SetGlobalCallbacks(GlobalCallbacks* callbacks);
296
297 private:
298 // Disallow copy and assign.
299 ClientContext(const ClientContext&);
300 ClientContext& operator=(const ClientContext&);
301
302 friend class ::grpc::testing::InteropClientContextInspector;
303 friend class CallOpClientRecvStatus;
304 friend class CallOpRecvInitialMetadata;
305 friend class Channel;
306 template <class R>
307 friend class ::grpc::ClientReader;
308 template <class W>
309 friend class ::grpc::ClientWriter;
310 template <class W, class R>
311 friend class ::grpc::ClientReaderWriter;
312 template <class R>
313 friend class ::grpc::ClientAsyncReader;
314 template <class W>
315 friend class ::grpc::ClientAsyncWriter;
316 template <class W, class R>
317 friend class ::grpc::ClientAsyncReaderWriter;
318 template <class R>
319 friend class ::grpc::ClientAsyncResponseReader;
320 template <class InputMessage, class OutputMessage>
321 friend Status BlockingUnaryCall(ChannelInterface* channel,
322 const RpcMethod& method,
323 ClientContext* context,
324 const InputMessage& request,
325 OutputMessage* result);
326
327 grpc_call* call() { return call_; }
328 void set_call(grpc_call* call, const std::shared_ptr<Channel>& channel);
329
330 grpc::string authority() { return authority_; }
331
332 bool initial_metadata_received_;
333 std::shared_ptr<Channel> channel_;
334 grpc::mutex mu_;
335 grpc_call* call_;
336 bool call_canceled_;
337 gpr_timespec deadline_;
338 grpc::string authority_;
339 std::shared_ptr<CallCredentials> creds_;
340 mutable std::shared_ptr<const AuthContext> auth_context_;
341 struct census_context* census_context_;
342 std::multimap<grpc::string, grpc::string> send_initial_metadata_;
343 std::multimap<grpc::string_ref, grpc::string_ref> recv_initial_metadata_;
344 std::multimap<grpc::string_ref, grpc::string_ref> trailing_metadata_;
345
346 grpc_call* propagate_from_call_;
347 PropagationOptions propagation_options_;
348
349 grpc_compression_algorithm compression_algorithm_;
350 };
351
352 } // namespace grpc
353
354 #endif // GRPCXX_IMPL_CODEGEN_CLIENT_CONTEXT_H
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698