GRPC C++  1.17.0
channel.h
Go to the documentation of this file.
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 #include <mutex>
24 
25 #include <grpc/grpc.h>
26 #include <grpcpp/impl/call.h>
31 
32 struct grpc_channel;
33 
34 namespace grpc {
35 
36 namespace experimental {
41 } // namespace experimental
42 
44 class Channel final : public ChannelInterface,
45  public internal::CallHook,
46  public std::enable_shared_from_this<Channel>,
47  private GrpcLibraryCodegen {
48  public:
49  ~Channel();
50 
53  grpc_connectivity_state GetState(bool try_to_connect) override;
54 
56  grpc::string GetLoadBalancingPolicyName() const;
57 
60  grpc::string GetServiceConfigJSON() const;
61 
62  private:
63  template <class InputMessage, class OutputMessage>
66  friend std::shared_ptr<Channel> CreateChannelInternal(
67  const grpc::string& host, grpc_channel* c_channel,
68  std::vector<
69  std::unique_ptr<experimental::ClientInterceptorFactoryInterface>>
70  interceptor_creators);
72  Channel(const grpc::string& host, grpc_channel* c_channel,
73  std::vector<
74  std::unique_ptr<experimental::ClientInterceptorFactoryInterface>>
75  interceptor_creators);
76 
77  internal::Call CreateCall(const internal::RpcMethod& method,
78  ClientContext* context,
79  CompletionQueue* cq) override;
80  void PerformOpsOnCall(internal::CallOpSetInterface* ops,
81  internal::Call* call) override;
82  void* RegisterMethod(const char* method) override;
83 
84  void NotifyOnStateChangeImpl(grpc_connectivity_state last_observed,
85  gpr_timespec deadline, CompletionQueue* cq,
86  void* tag) override;
87  bool WaitForStateChangeImpl(grpc_connectivity_state last_observed,
88  gpr_timespec deadline) override;
89 
90  CompletionQueue* CallbackCQ() override;
91 
92  internal::Call CreateCallInternal(const internal::RpcMethod& method,
93  ClientContext* context, CompletionQueue* cq,
94  size_t interceptor_pos) override;
95 
96  const grpc::string host_;
97  grpc_channel* const c_channel_; // owned
98 
99  // mu_ protects callback_cq_ (the per-channel callbackable completion queue)
100  std::mutex mu_;
101 
102  // callback_cq_ references the callbackable completion queue associated
103  // with this channel (if any). It is set on the first call to CallbackCQ().
104  // It is _not owned_ by the channel; ownership belongs with its internal
105  // shutdown callback tag (invoked when the CQ is fully shutdown).
106  CompletionQueue* callback_cq_ = nullptr;
107 
108  std::vector<std::unique_ptr<experimental::ClientInterceptorFactoryInterface>>
109  interceptor_creators_;
110 };
111 
112 } // namespace grpc
113 
114 #endif // GRPCPP_CHANNEL_H
std::string string
Definition: config.h:35
struct grpc_channel grpc_channel
The Channel interface allows creation of Call objects.
Definition: grpc_types.h:62
Classes that require gRPC to be initialized should inherit from this class.
Definition: grpc_library.h:38
A ClientContext allows the person implementing a service client to:
Definition: client_context.h:165
Descriptor of an RPC method.
Definition: rpc_method.h:29
grpc_connectivity_state
Connectivity state of a channel.
Definition: connectivity_state.h:27
An InterceptedChannel is available to client Interceptors.
Definition: intercepted_channel.h:34
An Alarm posts the user provided tag to its associated completion queue upon expiry or cancellation...
Definition: alarm.h:33
Codegen interface for grpc::Channel.
Definition: channel_interface.h:60
A thin wrapper around grpc_completion_queue (see src/core/lib/surface/completion_queue.h).
Definition: completion_queue.h:95
An abstract collection of call ops, used to generate the grpc_call_op structure to pass down to the l...
Definition: call_op_set_interface.h:34
Definition: channel_interface.h:45
Analogous to struct timespec.
Definition: gpr_types.h:47
This is an interface that Channel and Server implement to allow them to hook performing ops...
Definition: call_hook.h:30
void ChannelResetConnectionBackoff(Channel *channel)
Resets the channel&#39;s connection backoff.
Straightforward wrapping of the C call object.
Definition: call.h:36
Channels represent a connection to an endpoint. Created by CreateChannel.
Definition: channel.h:44