3 * Copyright 2015 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #ifndef GRPCPP_IMPL_CODEGEN_ASYNC_GENERIC_SERVICE_H
20 #define GRPCPP_IMPL_CODEGEN_ASYNC_GENERIC_SERVICE_H
22 #include <grpcpp/impl/codegen/async_stream.h>
23 #include <grpcpp/impl/codegen/byte_buffer.h>
24 #include <grpcpp/impl/codegen/server_callback.h>
30 typedef ServerAsyncReaderWriter<ByteBuffer, ByteBuffer>
31 GenericServerAsyncReaderWriter;
32 typedef ServerAsyncResponseWriter<ByteBuffer> GenericServerAsyncResponseWriter;
33 typedef ServerAsyncReader<ByteBuffer, ByteBuffer> GenericServerAsyncReader;
34 typedef ServerAsyncWriter<ByteBuffer> GenericServerAsyncWriter;
36 class GenericServerContext final : public ServerContext {
38 const grpc::string& method() const { return method_; }
39 const grpc::string& host() const { return host_; }
43 friend class ServerInterface;
48 ServerContext::Clear();
55 // A generic service at the server side accepts all RPC methods and hosts. It is
56 // typically used in proxies. The generic service can be registered to a server
57 // which also has other services.
59 // ServerBuilder builder;
60 // auto cq = builder.AddCompletionQueue();
61 // AsyncGenericService generic_service;
62 // builder.RegisterAsyncGenericService(&generic_service);
63 // auto server = builder.BuildAndStart();
65 // // request a new call
66 // GenericServerContext context;
67 // GenericServerAsyncReaderWriter stream;
68 // generic_service.RequestCall(&context, &stream, cq.get(), cq.get(), tag);
70 // When tag is retrieved from cq->Next(), context.method() can be used to look
71 // at the method and the RPC can be handled accordingly.
72 class AsyncGenericService final {
74 AsyncGenericService() : server_(nullptr) {}
76 void RequestCall(GenericServerContext* ctx,
77 GenericServerAsyncReaderWriter* reader_writer,
78 CompletionQueue* call_cq,
79 ServerCompletionQueue* notification_cq, void* tag);
86 namespace experimental {
88 /// \a ServerGenericBidiReactor is the reactor class for bidi streaming RPCs
89 /// invoked on a CallbackGenericService. The API difference relative to
90 /// ServerBidiReactor is that the argument to OnStarted is a
91 /// GenericServerContext rather than a ServerContext. All other reaction and
92 /// operation initiation APIs are the same as ServerBidiReactor.
93 class ServerGenericBidiReactor
94 : public ServerBidiReactor<ByteBuffer, ByteBuffer> {
96 /// Similar to ServerBidiReactor::OnStarted except for argument type.
98 /// \param[in] context The context object associated with this RPC.
99 virtual void OnStarted(GenericServerContext* context) {}
102 void OnStarted(ServerContext* ctx) final {
103 OnStarted(static_cast<GenericServerContext*>(ctx));
107 } // namespace experimental
110 class UnimplementedGenericBidiReactor
111 : public experimental::ServerGenericBidiReactor {
113 void OnDone() override { delete this; }
114 void OnStarted(GenericServerContext*) override {
115 this->Finish(Status(StatusCode::UNIMPLEMENTED, ""));
118 } // namespace internal
120 namespace experimental {
122 /// \a CallbackGenericService is the base class for generic services implemented
123 /// using the callback API and registered through the ServerBuilder using
124 /// RegisterCallbackGenericService.
125 class CallbackGenericService {
127 CallbackGenericService() {}
128 virtual ~CallbackGenericService() {}
130 /// The "method handler" for the generic API. This function should be
131 /// overridden to return a ServerGenericBidiReactor that implements the
132 /// application-level interface for this RPC.
133 virtual ServerGenericBidiReactor* CreateReactor() {
134 return new internal::UnimplementedGenericBidiReactor;
138 friend class ::grpc::Server;
140 internal::CallbackBidiHandler<ByteBuffer, ByteBuffer>* Handler() {
141 return new internal::CallbackBidiHandler<ByteBuffer, ByteBuffer>(
142 [this] { return CreateReactor(); });
145 Server* server_{nullptr};
147 } // namespace experimental
150 #endif // GRPCPP_IMPL_CODEGEN_ASYNC_GENERIC_SERVICE_H