GRPC C++  1.39.1
async_generic_service.h
Go to the documentation of this file.
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 
19 #ifndef GRPCPP_IMPL_CODEGEN_ASYNC_GENERIC_SERVICE_H
20 #define GRPCPP_IMPL_CODEGEN_ASYNC_GENERIC_SERVICE_H
21 
23 
28 
29 struct grpc_server;
30 
31 namespace grpc {
32 
33 typedef ServerAsyncReaderWriter<ByteBuffer, ByteBuffer>
38 
39 class GenericServerContext final : public ServerContext {
40  public:
41  const std::string& method() const { return method_; }
42  const std::string& host() const { return host_; }
43 
44  private:
45  friend class ServerInterface;
46 
47  std::string method_;
48  std::string host_;
49 };
50 
51 // A generic service at the server side accepts all RPC methods and hosts. It is
52 // typically used in proxies. The generic service can be registered to a server
53 // which also has other services.
54 // Sample usage:
55 // ServerBuilder builder;
56 // auto cq = builder.AddCompletionQueue();
57 // AsyncGenericService generic_service;
58 // builder.RegisterAsyncGenericService(&generic_service);
59 // auto server = builder.BuildAndStart();
60 //
61 // // request a new call
62 // GenericServerContext context;
63 // GenericServerAsyncReaderWriter stream;
64 // generic_service.RequestCall(&context, &stream, cq.get(), cq.get(), tag);
65 //
66 // When tag is retrieved from cq->Next(), context.method() can be used to look
67 // at the method and the RPC can be handled accordingly.
68 class AsyncGenericService final {
69  public:
70  AsyncGenericService() : server_(nullptr) {}
71 
73  GenericServerAsyncReaderWriter* reader_writer,
74  ::grpc::CompletionQueue* call_cq,
75  ::grpc::ServerCompletionQueue* notification_cq, void* tag);
76 
77  private:
78  friend class grpc::Server;
79  grpc::Server* server_;
80 };
81 
86 
88  public:
89  const std::string& method() const { return method_; }
90  const std::string& host() const { return host_; }
91 
92  private:
93  friend class ::grpc::Server;
94 
95  std::string method_;
96  std::string host_;
97 };
98 
103  public:
106 
111  GenericCallbackServerContext* /*ctx*/) {
112  class Reactor : public ServerGenericBidiReactor {
113  public:
114  Reactor() { this->Finish(Status(StatusCode::UNIMPLEMENTED, "")); }
115  void OnDone() override { delete this; }
116  };
117  return new Reactor;
118  }
119 
120  private:
121  friend class grpc::Server;
122 
125  [this](::grpc::CallbackServerContext* ctx) {
126  return CreateReactor(static_cast<GenericCallbackServerContext*>(ctx));
127  });
128  }
129 
130  grpc::Server* server_{nullptr};
131 };
132 
133 } // namespace grpc
134 
135 #endif // GRPCPP_IMPL_CODEGEN_ASYNC_GENERIC_SERVICE_H
Definition: async_generic_service.h:68
void RequestCall(GenericServerContext *ctx, GenericServerAsyncReaderWriter *reader_writer, ::grpc::CompletionQueue *call_cq, ::grpc::ServerCompletionQueue *notification_cq, void *tag)
Definition: async_generic_service.cc:25
AsyncGenericService()
Definition: async_generic_service.h:70
CallbackGenericService is the base class for generic services implemented using the callback API and ...
Definition: async_generic_service.h:102
virtual ServerGenericBidiReactor * CreateReactor(GenericCallbackServerContext *)
The "method handler" for the generic API.
Definition: async_generic_service.h:110
virtual ~CallbackGenericService()
Definition: async_generic_service.h:105
CallbackGenericService()
Definition: async_generic_service.h:104
Definition: server_context.h:578
A thin wrapper around grpc_completion_queue (see src/core/lib/surface/completion_queue....
Definition: completion_queue.h:102
Definition: async_generic_service.h:87
const std::string & host() const
Definition: async_generic_service.h:90
const std::string & method() const
Definition: async_generic_service.h:89
Definition: async_generic_service.h:39
const std::string & method() const
Definition: async_generic_service.h:41
const std::string & host() const
Definition: async_generic_service.h:42
Async server-side API for doing client-streaming RPCs, where the incoming message stream from the cli...
Definition: async_stream.h:697
Async server-side API for doing bidirectional streaming RPCs, where the incoming message stream comin...
Definition: async_stream.h:1010
ServerBidiReactor is the interface for a bidirectional streaming RPC.
Definition: server_callback.h:268
A specific type of completion queue used by the processing of notifications by servers.
Definition: completion_queue.h:429
A ServerContext or CallbackServerContext allows the code implementing a service handler to:
Definition: server_context.h:538
Represents a gRPC server.
Definition: server.h:59
Definition: server_interface.h:59
Definition: server_callback_handlers.h:673
::google::protobuf::util::Status Status
Definition: config_protobuf.h:91
An Alarm posts the user-provided tag to its associated completion queue or invokes the user-provided ...
Definition: alarm.h:33
ServerAsyncReader< ByteBuffer, ByteBuffer > GenericServerAsyncReader
Definition: async_generic_service.h:36
ServerAsyncReaderWriter< ByteBuffer, ByteBuffer > GenericServerAsyncReaderWriter
Definition: async_generic_service.h:34
ServerAsyncWriter< ByteBuffer > GenericServerAsyncWriter
Definition: async_generic_service.h:37
ServerAsyncResponseWriter< ByteBuffer > GenericServerAsyncResponseWriter
Definition: async_generic_service.h:35
@ UNIMPLEMENTED
Operation is not implemented or not supported/enabled in this service.
Definition: status_code_enum.h:115
Definition: server.h:450