• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 //
3 // Copyright 2024 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_GENERIC_CALLBACK_GENERIC_SERVICE_H
20 #define GRPCPP_GENERIC_CALLBACK_GENERIC_SERVICE_H
21 
22 #include <grpc/support/port_platform.h>
23 #include <grpcpp/impl/server_callback_handlers.h>
24 #include <grpcpp/support/byte_buffer.h>
25 #include <grpcpp/support/server_callback.h>
26 
27 struct grpc_server;
28 
29 namespace grpc {
30 
31 /// \a ServerGenericBidiReactor is the reactor class for bidi streaming RPCs
32 /// invoked on a CallbackGenericService. It is just a ServerBidi reactor with
33 /// ByteBuffer arguments.
34 using ServerGenericBidiReactor = ServerBidiReactor<ByteBuffer, ByteBuffer>;
35 
36 class GenericCallbackServerContext final : public grpc::CallbackServerContext {
37  public:
method()38   const std::string& method() const { return method_; }
host()39   const std::string& host() const { return host_; }
40 
41  private:
42   friend class grpc::Server;
43 
44   std::string method_;
45   std::string host_;
46 };
47 
48 /// \a CallbackGenericService is the base class for generic services implemented
49 /// using the callback API and registered through the ServerBuilder using
50 /// RegisterCallbackGenericService.
51 class CallbackGenericService {
52  public:
CallbackGenericService()53   CallbackGenericService() {}
~CallbackGenericService()54   virtual ~CallbackGenericService() {}
55 
56   /// The "method handler" for the generic API. This function should be
57   /// overridden to provide a ServerGenericBidiReactor that implements the
58   /// application-level interface for this RPC. Unimplemented by default.
CreateReactor(GenericCallbackServerContext *)59   virtual ServerGenericBidiReactor* CreateReactor(
60       GenericCallbackServerContext* /*ctx*/) {
61     class Reactor : public ServerGenericBidiReactor {
62      public:
63       Reactor() { this->Finish(Status(StatusCode::UNIMPLEMENTED, "")); }
64       void OnDone() override { delete this; }
65     };
66     return new Reactor;
67   }
68 
69  private:
70   friend class grpc::Server;
71 
Handler()72   internal::CallbackBidiHandler<ByteBuffer, ByteBuffer>* Handler() {
73     return new internal::CallbackBidiHandler<ByteBuffer, ByteBuffer>(
74         [this](grpc::CallbackServerContext* ctx) {
75           return CreateReactor(static_cast<GenericCallbackServerContext*>(ctx));
76         });
77   }
78 
79   grpc::Server* server_{nullptr};
80 };
81 
82 }  // namespace grpc
83 
84 #endif  // GRPCPP_GENERIC_CALLBACK_GENERIC_SERVICE_H
85