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