GRPC C++  1.4.0
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
server_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 
34 #ifndef GRPCXX_IMPL_CODEGEN_SERVER_CONTEXT_H
35 #define GRPCXX_IMPL_CODEGEN_SERVER_CONTEXT_H
36 
37 #include <map>
38 #include <memory>
39 #include <vector>
40 
42 
50 
51 struct grpc_metadata;
52 struct grpc_call;
53 struct census_context;
54 
55 namespace grpc {
56 class ClientContext;
57 template <class W, class R>
58 class ServerAsyncReader;
59 template <class W>
60 class ServerAsyncWriter;
61 template <class W>
62 class ServerAsyncResponseWriter;
63 template <class W, class R>
64 class ServerAsyncReaderWriter;
65 template <class R>
66 class ServerReader;
67 template <class W>
68 class ServerWriter;
69 namespace internal {
70 template <class W, class R>
71 class ServerReaderWriterBody;
72 }
73 template <class ServiceType, class RequestType, class ResponseType>
74 class RpcMethodHandler;
75 template <class ServiceType, class RequestType, class ResponseType>
76 class ClientStreamingHandler;
77 template <class ServiceType, class RequestType, class ResponseType>
78 class ServerStreamingHandler;
79 template <class ServiceType, class RequestType, class ResponseType>
80 class BidiStreamingHandler;
81 class UnknownMethodHandler;
82 
83 class Call;
84 class CallOpBuffer;
85 class CompletionQueue;
86 class Server;
87 class ServerInterface;
88 
89 namespace testing {
90 class InteropServerContextInspector;
91 class ServerContextTestSpouse;
92 } // namespace testing
93 
110  public:
111  ServerContext(); // for async calls
112  ~ServerContext();
113 
115  std::chrono::system_clock::time_point deadline() const {
116  return Timespec2Timepoint(deadline_);
117  }
118 
120  gpr_timespec raw_deadline() const { return deadline_; }
121 
134  void AddInitialMetadata(const grpc::string& key, const grpc::string& value);
135 
148  void AddTrailingMetadata(const grpc::string& key, const grpc::string& value);
149 
153  bool IsCancelled() const;
154 
167  void TryCancel() const;
168 
178  const std::multimap<grpc::string_ref, grpc::string_ref>& client_metadata()
179  const {
180  return *client_metadata_.map();
181  }
182 
185  return compression_level_;
186  }
187 
192  compression_level_set_ = true;
193  compression_level_ = level;
194  }
195 
199  bool compression_level_set() const { return compression_level_set_; }
200 
203  return compression_algorithm_;
204  }
209 
211  void SetLoadReportingCosts(const std::vector<grpc::string>& cost_data);
212 
216  std::shared_ptr<const AuthContext> auth_context() const {
217  if (auth_context_.get() == nullptr) {
218  auth_context_ = CreateAuthContext(call_);
219  }
220  return auth_context_;
221  }
222 
227  grpc::string peer() const;
228 
230  const struct census_context* census_context() const;
231 
235  void AsyncNotifyWhenDone(void* tag) {
236  has_notify_when_done_tag_ = true;
237  async_notify_when_done_tag_ = tag;
238  }
239 
242  grpc_call* c_call() { return call_; }
243 
244  private:
245  friend class ::grpc::testing::InteropServerContextInspector;
246  friend class ::grpc::testing::ServerContextTestSpouse;
247  friend class ::grpc::ServerInterface;
248  friend class ::grpc::Server;
249  template <class W, class R>
250  friend class ::grpc::ServerAsyncReader;
251  template <class W>
252  friend class ::grpc::ServerAsyncWriter;
253  template <class W>
254  friend class ::grpc::ServerAsyncResponseWriter;
255  template <class W, class R>
256  friend class ::grpc::ServerAsyncReaderWriter;
257  template <class R>
258  friend class ::grpc::ServerReader;
259  template <class W>
260  friend class ::grpc::ServerWriter;
261  template <class W, class R>
262  friend class ::grpc::internal::ServerReaderWriterBody;
263  template <class ServiceType, class RequestType, class ResponseType>
264  friend class RpcMethodHandler;
265  template <class ServiceType, class RequestType, class ResponseType>
267  template <class ServiceType, class RequestType, class ResponseType>
269  template <class Streamer, bool WriteNeeded>
271  friend class UnknownMethodHandler;
272  friend class ::grpc::ClientContext;
273 
276  ServerContext& operator=(const ServerContext&);
277 
278  class CompletionOp;
279 
280  void BeginCompletionOp(Call* call);
282  CompletionQueueTag* GetCompletionOpTag();
283 
285 
286  void set_call(grpc_call* call) { call_ = call; }
287 
288  uint32_t initial_metadata_flags() const { return 0; }
289 
290  CompletionOp* completion_op_;
291  bool has_notify_when_done_tag_;
292  void* async_notify_when_done_tag_;
293 
294  gpr_timespec deadline_;
295  grpc_call* call_;
296  CompletionQueue* cq_;
297  bool sent_initial_metadata_;
298  mutable std::shared_ptr<const AuthContext> auth_context_;
299  MetadataMap client_metadata_;
300  std::multimap<grpc::string, grpc::string> initial_metadata_;
301  std::multimap<grpc::string, grpc::string> trailing_metadata_;
302 
303  bool compression_level_set_;
304  grpc_compression_level compression_level_;
305  grpc_compression_algorithm compression_algorithm_;
306 };
307 
308 } // namespace grpc
309 
310 #endif // GRPCXX_IMPL_CODEGEN_SERVER_CONTEXT_H
grpc_compression_algorithm compression_algorithm() const
Return the compression algorithm to be used by the server call.
Definition: server_context.h:202
struct grpc_call grpc_call
A Call represents an RPC.
Definition: grpc_types.h:86
std::shared_ptr< const AuthContext > CreateAuthContext(grpc_call *call)
void SetLoadReportingCosts(const std::vector< grpc::string > &cost_data)
Set the load reporting costs in cost_data for the call.
void set_compression_algorithm(grpc_compression_algorithm algorithm)
Set algorithm to be the compression algorithm used for the server call.
An interface allowing implementors to process and filter event tags.
Definition: completion_queue_tag.h:40
grpc_compression_level compression_level() const
Return the compression algorithm to be used by the server call.
Definition: server_context.h:184
std::string string
Definition: config.h:50
gpr_timespec raw_deadline() const
Return a gpr_timespec representation of the server call's deadline.
Definition: server_context.h:120
void set_compression_level(grpc_compression_level level)
Set algorithm to be the compression algorithm used for the server call.
Definition: server_context.h:191
void AsyncNotifyWhenDone(void *tag)
Async only.
Definition: server_context.h:235
Definition: grpc_types.h:427
void TryCancel() const
Cancel the Call from the server.
grpc_compression_level
Compression levels allow a party with knowledge of its peer's accepted encodings to request compressi...
Definition: compression_types.h:83
void AddInitialMetadata(const grpc::string &key, const grpc::string &value)
Add the (meta_key, meta_value) pair to the initial metadata associated with a server call...
Handle unknown method by returning UNIMPLEMENTED error.
Definition: method_handler_impl.h:253
const struct census_context * census_context() const
Get the census context associated with this server call.
A wrapper class of an application provided bidi-streaming handler.
Definition: method_handler_impl.h:177
A single metadata element.
Definition: grpc_types.h:384
grpc_compression_algorithm
The various compression algorithms supported by gRPC.
Definition: compression_types.h:71
Straightforward wrapping of the C call object.
Definition: call.h:638
A wrapper class of an application provided rpc method handler.
Definition: completion_queue.h:76
bool IsCancelled() const
IsCancelled is always safe to call when using sync API.
std::multimap< grpc::string_ref, grpc::string_ref > * map()
Definition: metadata_map.h:58
A ServerContext allows the person implementing a service handler to:
Definition: server_context.h:109
grpc::string peer() const
Return the peer uri in a string.
const std::multimap< grpc::string_ref, grpc::string_ref > & client_metadata() const
Return a collection of initial metadata key-value pairs sent from the client.
Definition: server_context.h:178
A wrapper class of an application provided client streaming handler.
Definition: completion_queue.h:78
A wrapper class of an application provided server streaming handler.
Definition: completion_queue.h:80
Analogous to struct timespec.
Definition: gpr_types.h:62
std::chrono::system_clock::time_point Timespec2Timepoint(gpr_timespec t)
grpc_call * c_call()
Should be used for framework-level extensions only.
Definition: server_context.h:242
void AddTrailingMetadata(const grpc::string &key, const grpc::string &value)
Add the (meta_key, meta_value) pair to the initial metadata associated with a server call...
std::shared_ptr< const AuthContext > auth_context() const
Return the authentication context for this server call.
Definition: server_context.h:216
bool compression_level_set() const
Return a bool indicating whether the compression level for this call has been set (either implicitly ...
Definition: server_context.h:199
std::chrono::system_clock::time_point deadline() const
Return the deadline for the server call.
Definition: server_context.h:115