• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <functional>
23 
24 #include <grpcpp/support/async_stream.h>
25 #include <grpcpp/support/async_unary_call.h>
26 #include <grpcpp/support/byte_buffer.h>
27 #include <grpcpp/support/status.h>
28 
29 namespace grpc {
30 
31 class CompletionQueue;
32 typedef ClientAsyncReaderWriter<ByteBuffer, ByteBuffer>
33     GenericClientAsyncReaderWriter;
34 typedef ClientAsyncResponseReader<ByteBuffer> GenericClientAsyncResponseReader;
35 
36 /// Generic stubs provide a type-unsafe interface to call gRPC methods
37 /// by name.
38 class GenericStub final {
39  public:
GenericStub(std::shared_ptr<ChannelInterface> channel)40   explicit GenericStub(std::shared_ptr<ChannelInterface> channel)
41       : channel_(channel) {}
42 
43   /// Setup a call to a named method \a method using \a context, but don't
44   /// start it. Let it be started explicitly with StartCall and a tag.
45   /// The return value only indicates whether or not registration of the call
46   /// succeeded (i.e. the call won't proceed if the return value is nullptr).
47   std::unique_ptr<GenericClientAsyncReaderWriter> PrepareCall(
48       ClientContext* context, const grpc::string& method, CompletionQueue* cq);
49 
50   /// Setup a unary call to a named method \a method using \a context, and don't
51   /// start it. Let it be started explicitly with StartCall.
52   /// The return value only indicates whether or not registration of the call
53   /// succeeded (i.e. the call won't proceed if the return value is nullptr).
54   std::unique_ptr<GenericClientAsyncResponseReader> PrepareUnaryCall(
55       ClientContext* context, const grpc::string& method,
56       const ByteBuffer& request, CompletionQueue* cq);
57 
58   /// DEPRECATED for multi-threaded use
59   /// Begin a call to a named method \a method using \a context.
60   /// A tag \a tag will be delivered to \a cq when the call has been started
61   /// (i.e, initial metadata has been sent).
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).
64   std::unique_ptr<GenericClientAsyncReaderWriter> Call(
65       ClientContext* context, const grpc::string& method, CompletionQueue* cq,
66       void* tag);
67 
68   /// NOTE: class experimental_type is not part of the public API of this class
69   /// TODO(vjpai): Move these contents to the public API of GenericStub when
70   ///              they are no longer experimental
71   class experimental_type {
72    public:
experimental_type(GenericStub * stub)73     explicit experimental_type(GenericStub* stub) : stub_(stub) {}
74 
75     void UnaryCall(ClientContext* context, const grpc::string& method,
76                    const ByteBuffer* request, ByteBuffer* response,
77                    std::function<void(Status)> on_completion);
78 
79    private:
80     GenericStub* stub_;
81   };
82 
83   /// NOTE: The function experimental() is not stable public API. It is a view
84   /// to the experimental components of this class. It may be changed or removed
85   /// at any time.
experimental()86   experimental_type experimental() { return experimental_type(this); }
87 
88  private:
89   std::shared_ptr<ChannelInterface> channel_;
90 };
91 
92 }  // namespace grpc
93 
94 #endif  // GRPCPP_GENERIC_GENERIC_STUB_H
95