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