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 22 #include <grpc/impl/codegen/port_platform.h> 23 24 #include <grpcpp/impl/codegen/async_stream.h> 25 #include <grpcpp/impl/codegen/byte_buffer.h> 26 #include <grpcpp/impl/codegen/server_callback.h> 27 #include <grpcpp/impl/codegen/server_callback_handlers.h> 28 29 struct grpc_server; 30 31 namespace grpc { 32 33 typedef ServerAsyncReaderWriter<ByteBuffer, ByteBuffer> 34 GenericServerAsyncReaderWriter; 35 typedef ServerAsyncResponseWriter<ByteBuffer> GenericServerAsyncResponseWriter; 36 typedef ServerAsyncReader<ByteBuffer, ByteBuffer> GenericServerAsyncReader; 37 typedef ServerAsyncWriter<ByteBuffer> GenericServerAsyncWriter; 38 39 class GenericServerContext final : public ServerContext { 40 public: method()41 const std::string& method() const { return method_; } host()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: AsyncGenericService()70 AsyncGenericService() : server_(nullptr) {} 71 72 void RequestCall(GenericServerContext* ctx, 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 82 #ifndef GRPC_CALLBACK_API_NONEXPERIMENTAL 83 namespace experimental { 84 #endif 85 86 /// \a ServerGenericBidiReactor is the reactor class for bidi streaming RPCs 87 /// invoked on a CallbackGenericService. It is just a ServerBidi reactor with 88 /// ByteBuffer arguments. 89 using ServerGenericBidiReactor = ServerBidiReactor<ByteBuffer, ByteBuffer>; 90 91 class GenericCallbackServerContext final : public grpc::CallbackServerContext { 92 public: method()93 const std::string& method() const { return method_; } host()94 const std::string& host() const { return host_; } 95 96 private: 97 friend class ::grpc::Server; 98 99 std::string method_; 100 std::string host_; 101 }; 102 103 /// \a CallbackGenericService is the base class for generic services implemented 104 /// using the callback API and registered through the ServerBuilder using 105 /// RegisterCallbackGenericService. 106 class CallbackGenericService { 107 public: CallbackGenericService()108 CallbackGenericService() {} ~CallbackGenericService()109 virtual ~CallbackGenericService() {} 110 111 /// The "method handler" for the generic API. This function should be 112 /// overridden to provide a ServerGenericBidiReactor that implements the 113 /// application-level interface for this RPC. Unimplemented by default. CreateReactor(GenericCallbackServerContext *)114 virtual ServerGenericBidiReactor* CreateReactor( 115 GenericCallbackServerContext* /*ctx*/) { 116 class Reactor : public ServerGenericBidiReactor { 117 public: 118 Reactor() { this->Finish(Status(StatusCode::UNIMPLEMENTED, "")); } 119 void OnDone() override { delete this; } 120 }; 121 return new Reactor; 122 } 123 124 private: 125 friend class grpc::Server; 126 Handler()127 internal::CallbackBidiHandler<ByteBuffer, ByteBuffer>* Handler() { 128 return new internal::CallbackBidiHandler<ByteBuffer, ByteBuffer>( 129 [this](::grpc::CallbackServerContext* ctx) { 130 return CreateReactor(static_cast<GenericCallbackServerContext*>(ctx)); 131 }); 132 } 133 134 grpc::Server* server_{nullptr}; 135 }; 136 137 #ifndef GRPC_CALLBACK_API_NONEXPERIMENTAL 138 } // namespace experimental 139 #endif 140 } // namespace grpc 141 142 #endif // GRPCPP_IMPL_CODEGEN_ASYNC_GENERIC_SERVICE_H 143