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 <grpcpp/impl/codegen/async_stream.h> 23 #include <grpcpp/impl/codegen/byte_buffer.h> 24 25 struct grpc_server; 26 27 namespace grpc { 28 29 typedef ServerAsyncReaderWriter<ByteBuffer, ByteBuffer> 30 GenericServerAsyncReaderWriter; 31 typedef ServerAsyncResponseWriter<ByteBuffer> GenericServerAsyncResponseWriter; 32 typedef ServerAsyncReader<ByteBuffer, ByteBuffer> GenericServerAsyncReader; 33 typedef ServerAsyncWriter<ByteBuffer> GenericServerAsyncWriter; 34 35 class GenericServerContext final : public ServerContext { 36 public: method()37 const grpc::string& method() const { return method_; } host()38 const grpc::string& host() const { return host_; } 39 40 private: 41 friend class Server; 42 friend class ServerInterface; 43 44 grpc::string method_; 45 grpc::string host_; 46 }; 47 48 // A generic service at the server side accepts all RPC methods and hosts. It is 49 // typically used in proxies. The generic service can be registered to a server 50 // which also has other services. 51 // Sample usage: 52 // ServerBuilder builder; 53 // auto cq = builder.AddCompletionQueue(); 54 // AsyncGenericService generic_service; 55 // builder.RegisterAsyncGenericService(&generic_service); 56 // auto server = builder.BuildAndStart(); 57 // 58 // // request a new call 59 // GenericServerContext context; 60 // GenericServerAsyncReaderWriter stream; 61 // generic_service.RequestCall(&context, &stream, cq.get(), cq.get(), tag); 62 // 63 // When tag is retrieved from cq->Next(), context.method() can be used to look 64 // at the method and the RPC can be handled accordingly. 65 class AsyncGenericService final { 66 public: AsyncGenericService()67 AsyncGenericService() : server_(nullptr) {} 68 69 void RequestCall(GenericServerContext* ctx, 70 GenericServerAsyncReaderWriter* reader_writer, 71 CompletionQueue* call_cq, 72 ServerCompletionQueue* notification_cq, void* tag); 73 74 private: 75 friend class Server; 76 Server* server_; 77 }; 78 79 } // namespace grpc 80 81 #endif // GRPCPP_IMPL_CODEGEN_ASYNC_GENERIC_SERVICE_H 82