GRPC C++  1.23.0
server_context_impl.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2019 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 GRPCPP_IMPL_CODEGEN_SERVER_CONTEXT_IMPL_H
20 #define GRPCPP_IMPL_CODEGEN_SERVER_CONTEXT_IMPL_H
21 #include <map>
22 #include <memory>
23 #include <vector>
24 
26 
38 
39 struct grpc_metadata;
40 struct grpc_call;
41 struct census_context;
42 
43 namespace grpc_impl {
44 class ClientContext;
45 class CompletionQueue;
46 class Server;
47 template <class W, class R>
48 class ServerAsyncReader;
49 template <class W>
50 class ServerAsyncWriter;
51 template <class W>
53 template <class W, class R>
55 template <class R>
56 class ServerReader;
57 template <class W>
58 class ServerWriter;
59 
60 namespace internal {
61 template <class ServiceType, class RequestType, class ResponseType>
63 template <class RequestType, class ResponseType>
65 template <class RequestType, class ResponseType>
67 template <class RequestType, class ResponseType>
69 template <class RequestType, class ResponseType>
71 template <class W, class R>
73 class ServerReactor;
74 } // namespace internal
75 
76 } // namespace grpc_impl
77 namespace grpc {
78 class GenericServerContext;
79 class ServerInterface;
80 
81 namespace internal {
82 template <class ServiceType, class RequestType, class ResponseType>
83 class ClientStreamingHandler;
84 template <class ServiceType, class RequestType, class ResponseType>
85 class ServerStreamingHandler;
86 template <class ServiceType, class RequestType, class ResponseType>
87 class RpcMethodHandler;
88 template <class Streamer, bool WriteNeeded>
89 class TemplatedBidiStreamingHandler;
90 template <StatusCode code>
91 class ErrorMethodHandler;
92 class Call;
93 } // namespace internal
94 
95 class ServerInterface;
96 namespace testing {
97 class InteropServerContextInspector;
98 class ServerContextTestSpouse;
99 } // namespace testing
100 } // namespace grpc
101 
102 namespace grpc_impl {
119  public:
120  ServerContext(); // for async calls
121  ~ServerContext();
122 
124  std::chrono::system_clock::time_point deadline() const {
126  }
127 
129  gpr_timespec raw_deadline() const { return deadline_; }
130 
150  void AddInitialMetadata(const grpc::string& key, const grpc::string& value);
151 
171  void AddTrailingMetadata(const grpc::string& key, const grpc::string& value);
172 
176  bool IsCancelled() const;
177 
194  void TryCancel() const;
195 
205  const std::multimap<grpc::string_ref, grpc::string_ref>& client_metadata()
206  const {
207  return *client_metadata_.map();
208  }
209 
212  return compression_level_;
213  }
214 
219  compression_level_set_ = true;
220  compression_level_ = level;
221  }
222 
226  bool compression_level_set() const { return compression_level_set_; }
227 
233  return compression_algorithm_;
234  }
238  void set_compression_algorithm(grpc_compression_algorithm algorithm);
239 
241  void SetLoadReportingCosts(const std::vector<grpc::string>& cost_data);
242 
246  std::shared_ptr<const ::grpc::AuthContext> auth_context() const {
247  if (auth_context_.get() == nullptr) {
248  auth_context_ = ::grpc::CreateAuthContext(call_);
249  }
250  return auth_context_;
251  }
252 
257  grpc::string peer() const;
258 
260  const struct census_context* census_context() const;
261 
267  void AsyncNotifyWhenDone(void* tag) {
268  has_notify_when_done_tag_ = true;
269  async_notify_when_done_tag_ = tag;
270  }
271 
274  grpc_call* c_call() { return call_; }
275 
276  private:
277  friend class ::grpc::testing::InteropServerContextInspector;
278  friend class ::grpc::testing::ServerContextTestSpouse;
279  friend class ::grpc::ServerInterface;
281  template <class W, class R>
283  template <class W>
285  template <class W>
287  template <class W, class R>
289  template <class R>
291  template <class W>
293  template <class W, class R>
294  friend class ::grpc_impl::internal::ServerReaderWriterBody;
295  template <class ServiceType, class RequestType, class ResponseType>
296  friend class ::grpc::internal::RpcMethodHandler;
297  template <class ServiceType, class RequestType, class ResponseType>
298  friend class ::grpc::internal::ClientStreamingHandler;
299  template <class ServiceType, class RequestType, class ResponseType>
300  friend class ::grpc::internal::ServerStreamingHandler;
301  template <class Streamer, bool WriteNeeded>
302  friend class ::grpc::internal::TemplatedBidiStreamingHandler;
303  template <class RequestType, class ResponseType>
304  friend class ::grpc_impl::internal::CallbackUnaryHandler;
305  template <class RequestType, class ResponseType>
306  friend class ::grpc_impl::internal::CallbackClientStreamingHandler;
307  template <class RequestType, class ResponseType>
308  friend class ::grpc_impl::internal::CallbackServerStreamingHandler;
309  template <class RequestType, class ResponseType>
310  friend class ::grpc_impl::internal::CallbackBidiHandler;
311  template <::grpc::StatusCode code>
312  friend class ::grpc::internal::ErrorMethodHandler;
314  friend class ::grpc::GenericServerContext;
315 
318  ServerContext& operator=(const ServerContext&);
319 
320  class CompletionOp;
321 
322  void BeginCompletionOp(::grpc::internal::Call* call,
323  std::function<void(bool)> callback,
326  ::grpc::internal::CompletionQueueTag* GetCompletionOpTag();
327 
329 
330  void set_call(grpc_call* call) { call_ = call; }
331 
332  void BindDeadlineAndMetadata(gpr_timespec deadline, grpc_metadata_array* arr);
333 
334  void Clear();
335 
336  void Setup(gpr_timespec deadline);
337 
338  uint32_t initial_metadata_flags() const { return 0; }
339 
340  void SetCancelCallback(std::function<void()> callback);
341  void ClearCancelCallback();
342 
343  ::grpc::experimental::ServerRpcInfo* set_server_rpc_info(
344  const char* method, ::grpc::internal::RpcMethod::RpcType type,
345  const std::vector<std::unique_ptr<
347  if (creators.size() != 0) {
348  rpc_info_ = new ::grpc::experimental::ServerRpcInfo(this, method, type);
349  rpc_info_->RegisterInterceptors(creators);
350  }
351  return rpc_info_;
352  }
353 
354  CompletionOp* completion_op_;
355  bool has_notify_when_done_tag_;
356  void* async_notify_when_done_tag_;
358 
359  gpr_timespec deadline_;
360  grpc_call* call_;
362  bool sent_initial_metadata_;
363  mutable std::shared_ptr<const ::grpc::AuthContext> auth_context_;
364  mutable ::grpc::internal::MetadataMap client_metadata_;
365  std::multimap<grpc::string, grpc::string> initial_metadata_;
366  std::multimap<grpc::string, grpc::string> trailing_metadata_;
367 
368  bool compression_level_set_;
369  grpc_compression_level compression_level_;
370  grpc_compression_algorithm compression_algorithm_;
371 
374  pending_ops_;
375  bool has_pending_ops_;
376 
378 };
379 } // namespace grpc_impl
380 #endif // GRPCPP_IMPL_CODEGEN_SERVER_CONTEXT_IMPL_H
struct grpc_call grpc_call
A Call represents an RPC.
Definition: grpc_types.h:70
std::shared_ptr< const AuthContext > CreateAuthContext(grpc_call *call)
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_impl.h:205
::grpc_impl::ClientContext ClientContext
Definition: client_context.h:26
std::string string
Definition: config.h:35
An interface allowing implementors to process and filter event tags.
Definition: completion_queue_tag.h:26
void AsyncNotifyWhenDone(void *tag)
Async only.
Definition: server_context_impl.h:267
::grpc_impl::Server Server
Definition: server.h:26
Primary implementation of CallOpSetInterface.
Definition: call_op_set.h:821
::grpc_impl::ServerAsyncResponseWriter< W > ServerAsyncResponseWriter
Definition: async_unary_call.h:34
Definition: completion_queue_impl.h:62
Definition: grpc_types.h:513
::grpc_impl::ServerAsyncWriter< W > ServerAsyncWriter
Definition: async_stream.h:69
grpc_compression_level
Compression levels allow a party with knowledge of its peer&#39;s accepted encodings to request compressi...
Definition: compression_types.h:71
RpcType
Definition: rpc_method.h:31
::grpc_impl::ServerContext ServerContext
Definition: server_context.h:25
A ServerContext allows the person implementing a service handler to:
Definition: server_context_impl.h:118
std::shared_ptr< const ::grpc::AuthContext > auth_context() const
Return the authentication context for this server call.
Definition: server_context_impl.h:246
grpc_call * c_call()
Should be used for framework-level extensions only.
Definition: server_context_impl.h:274
Definition: call_op_set.h:218
void set_compression_level(grpc_compression_level level)
Set level to be the compression level used for the server call.
Definition: server_context_impl.h:218
::grpc_impl::ServerReader< R > ServerReader
Definition: sync_stream.h:75
::grpc_impl::CompletionQueue CompletionQueue
Definition: completion_queue.h:26
std::chrono::system_clock::time_point deadline() const
Return the deadline for the server call.
Definition: server_context_impl.h:124
Definition: server_context_impl.h:62
A single metadata element.
Definition: grpc_types.h:469
Definition: call_op_set.h:288
grpc_compression_algorithm
The various compression algorithms supported by gRPC (not sorted by compression level) ...
Definition: compression_types.h:57
ServerRpcInfo represents the state of a particular RPC as it appears to an interceptor.
Definition: server_interceptor.h:60
grpc_compression_level compression_level() const
Return the compression algorithm to be used by the server call.
Definition: server_context_impl.h:211
grpc_compression_algorithm compression_algorithm() const
Return the compression algorithm the server call will request be used.
Definition: server_context_impl.h:232
This header provides an object that reads bytes directly from a grpc::ByteBuffer, via the ZeroCopyInp...
Definition: alarm.h:24
Definition: byte_buffer.h:36
gpr_timespec raw_deadline() const
Return a gpr_timespec representation of the server call&#39;s deadline.
Definition: server_context_impl.h:129
An Alarm posts the user-provided tag to its associated completion queue or invokes the user-provided ...
Definition: alarm_impl.h:33
::grpc_impl::ServerAsyncReaderWriter< W, R > ServerAsyncReaderWriter
Definition: async_stream.h:76
Definition: server_callback_impl.h:46
CallbackWithSuccessTag can be reused multiple times, and will be used in this fashion for streaming o...
Definition: callback_common.h:132
::grpc_impl::ServerAsyncReader< W, R > ServerAsyncReader
Definition: async_stream.h:63
A thin wrapper around grpc_completion_queue (see src/core/lib/surface/completion_queue.h).
Definition: completion_queue_impl.h:101
bool compression_level_set() const
Return a bool indicating whether the compression level for this call has been set (either implicitly ...
Definition: server_context_impl.h:226
Analogous to struct timespec.
Definition: gpr_types.h:47
std::chrono::system_clock::time_point Timespec2Timepoint(gpr_timespec t)
::grpc_impl::ServerWriter< W > ServerWriter
Definition: sync_stream.h:81
Definition: server_callback_impl.h:42
Definition: server_callback_impl.h:48
Straightforward wrapping of the C call object.
Definition: call.h:38