GRPC C++  1.17.0
server_interceptor.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2018 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_INTERCEPTOR_H
20 #define GRPCPP_IMPL_CODEGEN_SERVER_INTERCEPTOR_H
21 
22 #include <atomic>
23 #include <vector>
24 
27 
28 namespace grpc {
29 
30 class ServerContext;
31 
32 namespace internal {
33 class InterceptorBatchMethodsImpl;
34 }
35 
36 namespace experimental {
37 class ServerRpcInfo;
38 
40  public:
42  virtual Interceptor* CreateServerInterceptor(ServerRpcInfo* info) = 0;
43 };
44 
46  public:
48 
49  ServerRpcInfo(const ServerRpcInfo&) = delete;
50  ServerRpcInfo(ServerRpcInfo&&) = default;
51  ServerRpcInfo& operator=(ServerRpcInfo&&) = default;
52 
53  // Getter methods
54  const char* method() { return method_; }
55  grpc::ServerContext* server_context() { return ctx_; }
56 
57  private:
58  ServerRpcInfo(grpc::ServerContext* ctx, const char* method)
59  : ctx_(ctx), method_(method) {
60  ref_.store(1);
61  }
62 
63  // Runs interceptor at pos \a pos.
64  void RunInterceptor(
65  experimental::InterceptorBatchMethods* interceptor_methods, size_t pos) {
66  GPR_CODEGEN_ASSERT(pos < interceptors_.size());
67  interceptors_[pos]->Intercept(interceptor_methods);
68  }
69 
70  void RegisterInterceptors(
71  const std::vector<
72  std::unique_ptr<experimental::ServerInterceptorFactoryInterface>>&
73  creators) {
74  for (const auto& creator : creators) {
75  interceptors_.push_back(std::unique_ptr<experimental::Interceptor>(
76  creator->CreateServerInterceptor(this)));
77  }
78  }
79 
80  void Ref() { ref_++; }
81  void Unref() {
82  if (--ref_ == 0) {
83  delete this;
84  }
85  }
86 
87  grpc::ServerContext* ctx_ = nullptr;
88  const char* method_ = nullptr;
89  std::atomic_int ref_;
90  std::vector<std::unique_ptr<experimental::Interceptor>> interceptors_;
91 
93  friend class grpc::ServerContext;
94 };
95 
96 } // namespace experimental
97 } // namespace grpc
98 
99 #endif // GRPCPP_IMPL_CODEGEN_SERVER_INTERCEPTOR_H
#define GPR_CODEGEN_ASSERT(x)
Codegen specific version of GPR_ASSERT.
Definition: core_codegen_interface.h:141
grpc::ServerContext * server_context()
Definition: server_interceptor.h:55
Definition: server_interceptor.h:45
An Alarm posts the user provided tag to its associated completion queue upon expiry or cancellation...
Definition: alarm.h:33
virtual ~ServerInterceptorFactoryInterface()
Definition: server_interceptor.h:41
Definition: interceptor.h:123
Definition: interceptor_common.h:36
A ServerContext allows the person implementing a service handler to:
Definition: server_context.h:102
~ServerRpcInfo()
Definition: server_interceptor.h:47
const char * method()
Definition: server_interceptor.h:54