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_GENERIC_GENERIC_STUB_H 20 #define GRPCPP_GENERIC_GENERIC_STUB_H 21 22 #include <grpcpp/client_context.h> 23 #include <grpcpp/impl/generic_stub_internal.h> 24 #include <grpcpp/impl/rpc_method.h> 25 #include <grpcpp/support/async_stream.h> 26 #include <grpcpp/support/async_unary_call.h> 27 #include <grpcpp/support/byte_buffer.h> 28 #include <grpcpp/support/stub_options.h> 29 30 namespace grpc { 31 32 class CompletionQueue; 33 34 typedef ClientAsyncReaderWriter<ByteBuffer, ByteBuffer> 35 GenericClientAsyncReaderWriter; 36 typedef ClientAsyncResponseReader<ByteBuffer> GenericClientAsyncResponseReader; 37 38 /// Generic stubs provide a type-unaware interface to call gRPC methods 39 /// by name. In practice, the Request and Response types should be basic 40 /// types like grpc::ByteBuffer or proto::MessageLite (the base protobuf). 41 template <class RequestType, class ResponseType> 42 class TemplatedGenericStub final 43 : public internal::TemplatedGenericStubCallbackInternal<RequestType, 44 ResponseType> { 45 public: 46 using internal::TemplatedGenericStubCallbackInternal< 47 RequestType, ResponseType>::TemplatedGenericStubCallbackInternal; 48 49 /// Setup a call to a named method \a method using \a context, but don't 50 /// start it. Let it be started explicitly with StartCall and a tag. 51 /// The return value only indicates whether or not registration of the call 52 /// succeeded (i.e. the call won't proceed if the return value is nullptr). 53 std::unique_ptr<ClientAsyncReaderWriter<RequestType, ResponseType>> PrepareCall(ClientContext * context,const std::string & method,grpc::CompletionQueue * cq)54 PrepareCall(ClientContext* context, const std::string& method, 55 grpc::CompletionQueue* cq) { 56 return CallInternal(channel_.get(), context, method, /*options=*/{}, cq, 57 false, nullptr); 58 } 59 60 /// Setup a unary call to a named method \a method using \a context, and don't 61 /// start it. Let it be started explicitly with StartCall. 62 /// The return value only indicates whether or not registration of the call 63 /// succeeded (i.e. the call won't proceed if the return value is nullptr). PrepareUnaryCall(ClientContext * context,const std::string & method,const RequestType & request,grpc::CompletionQueue * cq)64 std::unique_ptr<ClientAsyncResponseReader<ResponseType>> PrepareUnaryCall( 65 ClientContext* context, const std::string& method, 66 const RequestType& request, grpc::CompletionQueue* cq) { 67 return std::unique_ptr<ClientAsyncResponseReader<ResponseType>>( 68 internal::ClientAsyncResponseReaderHelper::Create<ResponseType>( 69 channel_.get(), cq, 70 grpc::internal::RpcMethod(method.c_str(), 71 /*suffix_for_stats=*/nullptr, 72 grpc::internal::RpcMethod::NORMAL_RPC), 73 context, request)); 74 } 75 76 using internal::TemplatedGenericStubCallbackInternal< 77 RequestType, ResponseType>::PrepareUnaryCall; 78 79 /// DEPRECATED for multi-threaded use 80 /// Begin a call to a named method \a method using \a context. 81 /// A tag \a tag will be delivered to \a cq when the call has been started 82 /// (i.e, initial metadata has been sent). 83 /// The return value only indicates whether or not registration of the call 84 /// succeeded (i.e. the call won't proceed if the return value is nullptr). Call(ClientContext * context,const std::string & method,grpc::CompletionQueue * cq,void * tag)85 std::unique_ptr<ClientAsyncReaderWriter<RequestType, ResponseType>> Call( 86 ClientContext* context, const std::string& method, 87 grpc::CompletionQueue* cq, void* tag) { 88 return CallInternal(channel_.get(), context, method, /*options=*/{}, cq, 89 true, tag); 90 } 91 92 private: 93 using internal::TemplatedGenericStubCallbackInternal<RequestType, 94 ResponseType>::channel_; 95 96 std::unique_ptr<ClientAsyncReaderWriter<RequestType, ResponseType>> CallInternal(grpc::ChannelInterface * channel,ClientContext * context,const std::string & method,StubOptions options,grpc::CompletionQueue * cq,bool start,void * tag)97 CallInternal(grpc::ChannelInterface* channel, ClientContext* context, 98 const std::string& method, StubOptions options, 99 grpc::CompletionQueue* cq, bool start, void* tag) { 100 return std::unique_ptr<ClientAsyncReaderWriter<RequestType, ResponseType>>( 101 internal::ClientAsyncReaderWriterFactory<RequestType, ResponseType>:: 102 Create(channel, cq, 103 grpc::internal::RpcMethod( 104 method.c_str(), options.suffix_for_stats(), 105 grpc::internal::RpcMethod::BIDI_STREAMING), 106 context, start, tag)); 107 } 108 }; 109 110 typedef TemplatedGenericStub<grpc::ByteBuffer, grpc::ByteBuffer> GenericStub; 111 112 } // namespace grpc 113 114 #endif // GRPCPP_GENERIC_GENERIC_STUB_H 115