GRPC C++  1.6.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 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 #ifndef GRPCXX_IMPL_CODEGEN_SERVER_CONTEXT_H
20 #define GRPCXX_IMPL_CODEGEN_SERVER_CONTEXT_H
21 
22 #include <map>
23 #include <memory>
24 #include <vector>
25 
27 
36 
37 struct grpc_metadata;
38 struct grpc_call;
39 struct census_context;
40 
41 namespace grpc {
42 class ClientContext;
43 template <class W, class R>
44 class ServerAsyncReader;
45 template <class W>
46 class ServerAsyncWriter;
47 template <class W>
48 class ServerAsyncResponseWriter;
49 template <class W, class R>
50 class ServerAsyncReaderWriter;
51 template <class R>
52 class ServerReader;
53 template <class W>
54 class ServerWriter;
55 namespace internal {
56 template <class W, class R>
57 class ServerReaderWriterBody;
58 }
59 template <class ServiceType, class RequestType, class ResponseType>
60 class RpcMethodHandler;
61 template <class ServiceType, class RequestType, class ResponseType>
62 class ClientStreamingHandler;
63 template <class ServiceType, class RequestType, class ResponseType>
64 class ServerStreamingHandler;
65 template <class ServiceType, class RequestType, class ResponseType>
66 class BidiStreamingHandler;
67 class UnknownMethodHandler;
68 
69 class Call;
70 class CallOpBuffer;
71 class CompletionQueue;
72 class Server;
73 class ServerInterface;
74 
75 namespace testing {
76 class InteropServerContextInspector;
77 class ServerContextTestSpouse;
78 } // namespace testing
79 
96  public:
97  ServerContext(); // for async calls
99 
101  std::chrono::system_clock::time_point deadline() const {
102  return Timespec2Timepoint(deadline_);
103  }
104 
106  gpr_timespec raw_deadline() const { return deadline_; }
107 
120  void AddInitialMetadata(const grpc::string& key, const grpc::string& value);
121 
134  void AddTrailingMetadata(const grpc::string& key, const grpc::string& value);
135 
139  bool IsCancelled() const;
140 
153  void TryCancel() const;
154 
164  const std::multimap<grpc::string_ref, grpc::string_ref>& client_metadata()
165  const {
166  return *client_metadata_.map();
167  }
168 
171  return compression_level_;
172  }
173 
178  compression_level_set_ = true;
179  compression_level_ = level;
180  }
181 
185  bool compression_level_set() const { return compression_level_set_; }
186 
189  return compression_algorithm_;
190  }
195 
197  void SetLoadReportingCosts(const std::vector<grpc::string>& cost_data);
198 
202  std::shared_ptr<const AuthContext> auth_context() const {
203  if (auth_context_.get() == nullptr) {
204  auth_context_ = CreateAuthContext(call_);
205  }
206  return auth_context_;
207  }
208 
213  grpc::string peer() const;
214 
216  const struct census_context* census_context() const;
217 
221  void AsyncNotifyWhenDone(void* tag) {
222  has_notify_when_done_tag_ = true;
223  async_notify_when_done_tag_ = tag;
224  }
225 
228  grpc_call* c_call() { return call_; }
229 
230  private:
231  friend class ::grpc::testing::InteropServerContextInspector;
232  friend class ::grpc::testing::ServerContextTestSpouse;
233  friend class ::grpc::ServerInterface;
234  friend class ::grpc::Server;
235  template <class W, class R>
236  friend class ::grpc::ServerAsyncReader;
237  template <class W>
238  friend class ::grpc::ServerAsyncWriter;
239  template <class W>
240  friend class ::grpc::ServerAsyncResponseWriter;
241  template <class W, class R>
242  friend class ::grpc::ServerAsyncReaderWriter;
243  template <class R>
244  friend class ::grpc::ServerReader;
245  template <class W>
246  friend class ::grpc::ServerWriter;
247  template <class W, class R>
248  friend class ::grpc::internal::ServerReaderWriterBody;
249  template <class ServiceType, class RequestType, class ResponseType>
250  friend class RpcMethodHandler;
251  template <class ServiceType, class RequestType, class ResponseType>
253  template <class ServiceType, class RequestType, class ResponseType>
255  template <class Streamer, bool WriteNeeded>
257  friend class UnknownMethodHandler;
258  friend class ::grpc::ClientContext;
259 
262  ServerContext& operator=(const ServerContext&);
263 
264  class CompletionOp;
265 
266  void BeginCompletionOp(Call* call);
268  CompletionQueueTag* GetCompletionOpTag();
269 
271 
272  void set_call(grpc_call* call) { call_ = call; }
273 
274  uint32_t initial_metadata_flags() const { return 0; }
275 
276  CompletionOp* completion_op_;
277  bool has_notify_when_done_tag_;
278  void* async_notify_when_done_tag_;
279 
280  gpr_timespec deadline_;
281  grpc_call* call_;
282  CompletionQueue* cq_;
283  bool sent_initial_metadata_;
284  mutable std::shared_ptr<const AuthContext> auth_context_;
285  MetadataMap client_metadata_;
286  std::multimap<grpc::string, grpc::string> initial_metadata_;
287  std::multimap<grpc::string, grpc::string> trailing_metadata_;
288 
289  bool compression_level_set_;
290  grpc_compression_level compression_level_;
291  grpc_compression_algorithm compression_algorithm_;
292 
293  CallOpSet<CallOpSendInitialMetadata, CallOpSendMessage> pending_ops_;
294  bool has_pending_ops_;
295 };
296 
297 } // namespace grpc
298 
299 #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:188
struct grpc_call grpc_call
A Call represents an RPC.
Definition: grpc_types.h:71
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:25
grpc_compression_level compression_level() const
Return the compression algorithm to be used by the server call.
Definition: server_context.h:170
std::string string
Definition: config.h:35
gpr_timespec raw_deadline() const
Return a gpr_timespec representation of the server call's deadline.
Definition: server_context.h:106
void set_compression_level(grpc_compression_level level)
Set algorithm to be the compression algorithm used for the server call.
Definition: server_context.h:177
void AsyncNotifyWhenDone(void *tag)
Async only.
Definition: server_context.h:221
Definition: grpc_types.h:426
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:68
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:244
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:165
A single metadata element.
Definition: grpc_types.h:383
grpc_compression_algorithm
The various compression algorithms supported by gRPC.
Definition: compression_types.h:56
Straightforward wrapping of the C call object.
Definition: call.h:647
A wrapper class of an application provided rpc method handler.
Definition: completion_queue.h:61
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:43
A ServerContext allows the person implementing a service handler to:
Definition: server_context.h:95
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:164
A wrapper class of an application provided client streaming handler.
Definition: completion_queue.h:63
A wrapper class of an application provided server streaming handler.
Definition: completion_queue.h:65
Analogous to struct timespec.
Definition: gpr_types.h:47
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:228
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:202
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:185
std::chrono::system_clock::time_point deadline() const
Return the deadline for the server call.
Definition: server_context.h:101