server_interceptor.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. #ifndef GRPCPP_SUPPORT_SERVER_INTERCEPTOR_H
  19. #define GRPCPP_SUPPORT_SERVER_INTERCEPTOR_H
  20. #include <atomic>
  21. #include <vector>
  22. #include <grpc/support/log.h>
  23. #include <grpcpp/impl/rpc_method.h>
  24. #include <grpcpp/support/interceptor.h>
  25. #include <grpcpp/support/string_ref.h>
  26. namespace grpc {
  27. class ServerContextBase;
  28. namespace internal {
  29. class InterceptorBatchMethodsImpl;
  30. }
  31. namespace experimental {
  32. class ServerRpcInfo;
  33. // A factory interface for creation of server interceptors. A vector of
  34. // factories can be provided to ServerBuilder which will be used to create a new
  35. // vector of server interceptors per RPC. Server interceptor authors should
  36. // create a subclass of ServerInterceptorFactorInterface which creates objects
  37. // of their interceptors.
  38. class ServerInterceptorFactoryInterface {
  39. public:
  40. virtual ~ServerInterceptorFactoryInterface() {}
  41. // Returns a pointer to an Interceptor object on successful creation, nullptr
  42. // otherwise. If nullptr is returned, this server interceptor factory is
  43. // ignored for the purposes of that RPC.
  44. virtual Interceptor* CreateServerInterceptor(ServerRpcInfo* info) = 0;
  45. };
  46. /// ServerRpcInfo represents the state of a particular RPC as it
  47. /// appears to an interceptor. It is created and owned by the library and
  48. /// passed to the CreateServerInterceptor method of the application's
  49. /// ServerInterceptorFactoryInterface implementation
  50. class ServerRpcInfo {
  51. public:
  52. /// Type categorizes RPCs by unary or streaming type
  53. enum class Type { UNARY, CLIENT_STREAMING, SERVER_STREAMING, BIDI_STREAMING };
  54. ~ServerRpcInfo() {}
  55. // Delete all copy and move constructors and assignments
  56. ServerRpcInfo(const ServerRpcInfo&) = delete;
  57. ServerRpcInfo& operator=(const ServerRpcInfo&) = delete;
  58. ServerRpcInfo(ServerRpcInfo&&) = delete;
  59. ServerRpcInfo& operator=(ServerRpcInfo&&) = delete;
  60. // Getter methods
  61. /// Return the fully-specified method name
  62. const char* method() const { return method_; }
  63. /// Return the type of the RPC (unary or a streaming flavor)
  64. Type type() const { return type_; }
  65. /// Return a pointer to the underlying ServerContext structure associated
  66. /// with the RPC to support features that apply to it
  67. ServerContextBase* server_context() { return ctx_; }
  68. private:
  69. static_assert(Type::UNARY ==
  70. static_cast<Type>(internal::RpcMethod::NORMAL_RPC),
  71. "violated expectation about Type enum");
  72. static_assert(Type::CLIENT_STREAMING ==
  73. static_cast<Type>(internal::RpcMethod::CLIENT_STREAMING),
  74. "violated expectation about Type enum");
  75. static_assert(Type::SERVER_STREAMING ==
  76. static_cast<Type>(internal::RpcMethod::SERVER_STREAMING),
  77. "violated expectation about Type enum");
  78. static_assert(Type::BIDI_STREAMING ==
  79. static_cast<Type>(internal::RpcMethod::BIDI_STREAMING),
  80. "violated expectation about Type enum");
  81. ServerRpcInfo(ServerContextBase* ctx, const char* method,
  82. internal::RpcMethod::RpcType type)
  83. : ctx_(ctx), method_(method), type_(static_cast<Type>(type)) {}
  84. // Runs interceptor at pos \a pos.
  85. void RunInterceptor(
  86. experimental::InterceptorBatchMethods* interceptor_methods, size_t pos) {
  87. GPR_ASSERT(pos < interceptors_.size());
  88. interceptors_[pos]->Intercept(interceptor_methods);
  89. }
  90. void RegisterInterceptors(
  91. const std::vector<
  92. std::unique_ptr<experimental::ServerInterceptorFactoryInterface>>&
  93. creators) {
  94. for (const auto& creator : creators) {
  95. auto* interceptor = creator->CreateServerInterceptor(this);
  96. if (interceptor != nullptr) {
  97. interceptors_.push_back(
  98. std::unique_ptr<experimental::Interceptor>(interceptor));
  99. }
  100. }
  101. }
  102. void Ref() { ref_.fetch_add(1, std::memory_order_relaxed); }
  103. void Unref() {
  104. if (GPR_UNLIKELY(ref_.fetch_sub(1, std::memory_order_acq_rel) == 1)) {
  105. delete this;
  106. }
  107. }
  108. ServerContextBase* ctx_ = nullptr;
  109. const char* method_ = nullptr;
  110. const Type type_;
  111. std::atomic<intptr_t> ref_{1};
  112. std::vector<std::unique_ptr<experimental::Interceptor>> interceptors_;
  113. friend class internal::InterceptorBatchMethodsImpl;
  114. friend class grpc::ServerContextBase;
  115. };
  116. } // namespace experimental
  117. } // namespace grpc
  118. #endif // GRPCPP_SUPPORT_SERVER_INTERCEPTOR_H