async_generic_service.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. #ifndef GRPCPP_GENERIC_ASYNC_GENERIC_SERVICE_H
  19. #define GRPCPP_GENERIC_ASYNC_GENERIC_SERVICE_H
  20. #include <grpc/support/port_platform.h>
  21. #include <grpcpp/impl/server_callback_handlers.h>
  22. #include <grpcpp/support/async_stream.h>
  23. #include <grpcpp/support/byte_buffer.h>
  24. #include <grpcpp/support/server_callback.h>
  25. struct grpc_server;
  26. namespace grpc {
  27. typedef ServerAsyncReaderWriter<ByteBuffer, ByteBuffer>
  28. GenericServerAsyncReaderWriter;
  29. typedef ServerAsyncResponseWriter<ByteBuffer> GenericServerAsyncResponseWriter;
  30. typedef ServerAsyncReader<ByteBuffer, ByteBuffer> GenericServerAsyncReader;
  31. typedef ServerAsyncWriter<ByteBuffer> GenericServerAsyncWriter;
  32. class GenericServerContext final : public ServerContext {
  33. public:
  34. const TString& method() const { return method_; }
  35. const TString& host() const { return host_; }
  36. private:
  37. friend class ServerInterface;
  38. TString method_;
  39. TString host_;
  40. };
  41. // A generic service at the server side accepts all RPC methods and hosts. It is
  42. // typically used in proxies. The generic service can be registered to a server
  43. // which also has other services.
  44. // Sample usage:
  45. // ServerBuilder builder;
  46. // auto cq = builder.AddCompletionQueue();
  47. // AsyncGenericService generic_service;
  48. // builder.RegisterAsyncGenericService(&generic_service);
  49. // auto server = builder.BuildAndStart();
  50. //
  51. // // request a new call
  52. // GenericServerContext context;
  53. // GenericServerAsyncReaderWriter stream;
  54. // generic_service.RequestCall(&context, &stream, cq.get(), cq.get(), tag);
  55. //
  56. // When tag is retrieved from cq->Next(), context.method() can be used to look
  57. // at the method and the RPC can be handled accordingly.
  58. class AsyncGenericService final {
  59. public:
  60. AsyncGenericService() : server_(nullptr) {}
  61. void RequestCall(GenericServerContext* ctx,
  62. GenericServerAsyncReaderWriter* reader_writer,
  63. grpc::CompletionQueue* call_cq,
  64. grpc::ServerCompletionQueue* notification_cq, void* tag);
  65. private:
  66. friend class grpc::Server;
  67. grpc::Server* server_;
  68. };
  69. /// \a ServerGenericBidiReactor is the reactor class for bidi streaming RPCs
  70. /// invoked on a CallbackGenericService. It is just a ServerBidi reactor with
  71. /// ByteBuffer arguments.
  72. using ServerGenericBidiReactor = ServerBidiReactor<ByteBuffer, ByteBuffer>;
  73. class GenericCallbackServerContext final : public grpc::CallbackServerContext {
  74. public:
  75. const TString& method() const { return method_; }
  76. const TString& host() const { return host_; }
  77. private:
  78. friend class grpc::Server;
  79. TString method_;
  80. TString host_;
  81. };
  82. /// \a CallbackGenericService is the base class for generic services implemented
  83. /// using the callback API and registered through the ServerBuilder using
  84. /// RegisterCallbackGenericService.
  85. class CallbackGenericService {
  86. public:
  87. CallbackGenericService() {}
  88. virtual ~CallbackGenericService() {}
  89. /// The "method handler" for the generic API. This function should be
  90. /// overridden to provide a ServerGenericBidiReactor that implements the
  91. /// application-level interface for this RPC. Unimplemented by default.
  92. virtual ServerGenericBidiReactor* CreateReactor(
  93. GenericCallbackServerContext* /*ctx*/) {
  94. class Reactor : public ServerGenericBidiReactor {
  95. public:
  96. Reactor() { this->Finish(Status(StatusCode::UNIMPLEMENTED, "")); }
  97. void OnDone() override { delete this; }
  98. };
  99. return new Reactor;
  100. }
  101. private:
  102. friend class grpc::Server;
  103. internal::CallbackBidiHandler<ByteBuffer, ByteBuffer>* Handler() {
  104. return new internal::CallbackBidiHandler<ByteBuffer, ByteBuffer>(
  105. [this](grpc::CallbackServerContext* ctx) {
  106. return CreateReactor(static_cast<GenericCallbackServerContext*>(ctx));
  107. });
  108. }
  109. grpc::Server* server_{nullptr};
  110. };
  111. } // namespace grpc
  112. #endif // GRPCPP_GENERIC_ASYNC_GENERIC_SERVICE_H