39917d2eb74c4a0d35579299de2897e71c709f9b
[platform/upstream/grpc.git] / include / grpcpp / channel_impl.h
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_CHANNEL_IMPL_H
20 #define GRPCPP_CHANNEL_IMPL_H
21
22 #include <memory>
23 #include <mutex>
24
25 #include <grpc/grpc.h>
26 #include <grpcpp/impl/call.h>
27 #include <grpcpp/impl/codegen/channel_interface.h>
28 #include <grpcpp/impl/codegen/client_interceptor.h>
29 #include <grpcpp/impl/codegen/completion_queue.h>
30 #include <grpcpp/impl/codegen/config.h>
31 #include <grpcpp/impl/codegen/grpc_library.h>
32 #include <grpcpp/impl/codegen/sync.h>
33
34 struct grpc_channel;
35
36 namespace grpc {
37
38 std::shared_ptr<::grpc_impl::Channel> CreateChannelInternal(
39     const grpc::string& host, grpc_channel* c_channel,
40     std::vector<
41         std::unique_ptr<experimental::ClientInterceptorFactoryInterface>>
42         interceptor_creators);
43 }  // namespace grpc
44 namespace grpc_impl {
45
46 namespace experimental {
47 /// Resets the channel's connection backoff.
48 /// TODO(roth): Once we see whether this proves useful, either create a gRFC
49 /// and change this to be a method of the Channel class, or remove it.
50 void ChannelResetConnectionBackoff(Channel* channel);
51 }  // namespace experimental
52
53 /// Channels represent a connection to an endpoint. Created by \a CreateChannel.
54 class Channel final : public ::grpc::ChannelInterface,
55                       public ::grpc::internal::CallHook,
56                       public std::enable_shared_from_this<Channel>,
57                       private ::grpc::GrpcLibraryCodegen {
58  public:
59   ~Channel();
60
61   /// Get the current channel state. If the channel is in IDLE and
62   /// \a try_to_connect is set to true, try to connect.
63   grpc_connectivity_state GetState(bool try_to_connect) override;
64
65   /// Returns the LB policy name, or the empty string if not yet available.
66   grpc::string GetLoadBalancingPolicyName() const;
67
68   /// Returns the service config in JSON form, or the empty string if
69   /// not available.
70   grpc::string GetServiceConfigJSON() const;
71
72  private:
73   template <class InputMessage, class OutputMessage>
74   friend class ::grpc::internal::BlockingUnaryCallImpl;
75   friend void experimental::ChannelResetConnectionBackoff(Channel* channel);
76   friend std::shared_ptr<Channel> grpc::CreateChannelInternal(
77       const grpc::string& host, grpc_channel* c_channel,
78       std::vector<std::unique_ptr<
79           ::grpc::experimental::ClientInterceptorFactoryInterface>>
80           interceptor_creators);
81   friend class ::grpc::internal::InterceptedChannel;
82   Channel(const grpc::string& host, grpc_channel* c_channel,
83           std::vector<std::unique_ptr<
84               ::grpc::experimental::ClientInterceptorFactoryInterface>>
85               interceptor_creators);
86
87   ::grpc::internal::Call CreateCall(const ::grpc::internal::RpcMethod& method,
88                                     ::grpc::ClientContext* context,
89                                     ::grpc::CompletionQueue* cq) override;
90   void PerformOpsOnCall(::grpc::internal::CallOpSetInterface* ops,
91                         ::grpc::internal::Call* call) override;
92   void* RegisterMethod(const char* method) override;
93
94   void NotifyOnStateChangeImpl(grpc_connectivity_state last_observed,
95                                gpr_timespec deadline,
96                                ::grpc::CompletionQueue* cq, void* tag) override;
97   bool WaitForStateChangeImpl(grpc_connectivity_state last_observed,
98                               gpr_timespec deadline) override;
99
100   ::grpc::CompletionQueue* CallbackCQ() override;
101
102   ::grpc::internal::Call CreateCallInternal(
103       const ::grpc::internal::RpcMethod& method, ::grpc::ClientContext* context,
104       ::grpc::CompletionQueue* cq, size_t interceptor_pos) override;
105
106   const grpc::string host_;
107   grpc_channel* const c_channel_;  // owned
108
109   // mu_ protects callback_cq_ (the per-channel callbackable completion queue)
110   grpc::internal::Mutex mu_;
111
112   // callback_cq_ references the callbackable completion queue associated
113   // with this channel (if any). It is set on the first call to CallbackCQ().
114   // It is _not owned_ by the channel; ownership belongs with its internal
115   // shutdown callback tag (invoked when the CQ is fully shutdown).
116   ::grpc::CompletionQueue* callback_cq_ = nullptr;
117
118   std::vector<
119       std::unique_ptr<::grpc::experimental::ClientInterceptorFactoryInterface>>
120       interceptor_creators_;
121 };
122
123 }  // namespace grpc_impl
124
125 #endif  // GRPCPP_CHANNEL_IMPL_H