• 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 GRPC_CORE_LIB_SURFACE_CHANNEL_H
20 #define GRPC_CORE_LIB_SURFACE_CHANNEL_H
21 
22 #include <grpc/support/port_platform.h>
23 
24 #include <map>
25 
26 #include "src/core/lib/channel/channel_stack.h"
27 #include "src/core/lib/channel/channel_stack_builder.h"
28 #include "src/core/lib/channel/channelz.h"
29 #include "src/core/lib/gprpp/manual_constructor.h"
30 #include "src/core/lib/surface/channel_stack_type.h"
31 #include "src/core/lib/transport/metadata.h"
32 
33 grpc_channel* grpc_channel_create(const char* target,
34                                   const grpc_channel_args* args,
35                                   grpc_channel_stack_type channel_stack_type,
36                                   grpc_transport* optional_transport,
37                                   grpc_resource_user* resource_user = nullptr);
38 
39 /** The same as grpc_channel_destroy, but doesn't create an ExecCtx, and so
40  * is safe to use from within core. */
41 void grpc_channel_destroy_internal(grpc_channel* channel);
42 
43 grpc_channel* grpc_channel_create_with_builder(
44     grpc_channel_stack_builder* builder,
45     grpc_channel_stack_type channel_stack_type);
46 
47 /** Create a call given a grpc_channel, in order to call \a method.
48     Progress is tied to activity on \a pollset_set. The returned call object is
49     meant to be used with \a grpc_call_start_batch_and_execute, which relies on
50     callbacks to signal completions. \a method and \a host need
51     only live through the invocation of this function. If \a parent_call is
52     non-NULL, it must be a server-side call. It will be used to propagate
53     properties from the server call to this new client call, depending on the
54     value of \a propagation_mask (see propagation_bits.h for possible values) */
55 grpc_call* grpc_channel_create_pollset_set_call(
56     grpc_channel* channel, grpc_call* parent_call, uint32_t propagation_mask,
57     grpc_pollset_set* pollset_set, const grpc_slice& method,
58     const grpc_slice* host, grpc_millis deadline, void* reserved);
59 
60 /** Get a (borrowed) pointer to this channels underlying channel stack */
61 grpc_channel_stack* grpc_channel_get_channel_stack(grpc_channel* channel);
62 
63 grpc_core::channelz::ChannelNode* grpc_channel_get_channelz_node(
64     grpc_channel* channel);
65 
66 size_t grpc_channel_get_call_size_estimate(grpc_channel* channel);
67 void grpc_channel_update_call_size_estimate(grpc_channel* channel, size_t size);
68 
69 namespace grpc_core {
70 
71 struct RegisteredCall {
72   grpc_mdelem path;
73   grpc_mdelem authority;
74 
75   explicit RegisteredCall(const char* method, const char* host);
76   // TODO(vjpai): delete copy constructor once all supported compilers allow
77   //              std::map value_type to be MoveConstructible.
78   RegisteredCall(const RegisteredCall& other);
79   RegisteredCall(RegisteredCall&& other) noexcept;
80 
81   ~RegisteredCall();
82 };
83 
84 struct CallRegistrationTable {
85   grpc_core::Mutex mu;
86   std::map<std::pair<const char*, const char*>, RegisteredCall>
87       map /* GUARDED_BY(mu) */;
88   int method_registration_attempts /* GUARDED_BY(mu) */ = 0;
89 };
90 
91 }  // namespace grpc_core
92 
93 struct grpc_channel {
94   int is_client;
95   grpc_compression_options compression_options;
96 
97   gpr_atm call_size_estimate;
98   grpc_resource_user* resource_user;
99 
100   // TODO(vjpai): Once the grpc_channel is allocated via new rather than malloc,
101   //              expand the members of the CallRegistrationTable directly into
102   //              the grpc_channel. For now it is kept separate so that all the
103   //              manual constructing can be done with a single call rather than
104   //              a separate manual construction for each field.
105   grpc_core::ManualConstructor<grpc_core::CallRegistrationTable>
106       registration_table;
107   grpc_core::RefCountedPtr<grpc_core::channelz::ChannelNode> channelz_node;
108 
109   char* target;
110 };
111 #define CHANNEL_STACK_FROM_CHANNEL(c) ((grpc_channel_stack*)((c) + 1))
112 
grpc_channel_compression_options(const grpc_channel * channel)113 inline grpc_compression_options grpc_channel_compression_options(
114     const grpc_channel* channel) {
115   return channel->compression_options;
116 }
117 
grpc_channel_get_channel_stack(grpc_channel * channel)118 inline grpc_channel_stack* grpc_channel_get_channel_stack(
119     grpc_channel* channel) {
120   return CHANNEL_STACK_FROM_CHANNEL(channel);
121 }
122 
grpc_channel_get_channelz_node(grpc_channel * channel)123 inline grpc_core::channelz::ChannelNode* grpc_channel_get_channelz_node(
124     grpc_channel* channel) {
125   return channel->channelz_node.get();
126 }
127 
128 #ifndef NDEBUG
grpc_channel_internal_ref(grpc_channel * channel,const char * reason)129 inline void grpc_channel_internal_ref(grpc_channel* channel,
130                                       const char* reason) {
131   GRPC_CHANNEL_STACK_REF(CHANNEL_STACK_FROM_CHANNEL(channel), reason);
132 }
grpc_channel_internal_unref(grpc_channel * channel,const char * reason)133 inline void grpc_channel_internal_unref(grpc_channel* channel,
134                                         const char* reason) {
135   GRPC_CHANNEL_STACK_UNREF(CHANNEL_STACK_FROM_CHANNEL(channel), reason);
136 }
137 #define GRPC_CHANNEL_INTERNAL_REF(channel, reason) \
138   grpc_channel_internal_ref(channel, reason)
139 #define GRPC_CHANNEL_INTERNAL_UNREF(channel, reason) \
140   grpc_channel_internal_unref(channel, reason)
141 #else
grpc_channel_internal_ref(grpc_channel * channel)142 inline void grpc_channel_internal_ref(grpc_channel* channel) {
143   GRPC_CHANNEL_STACK_REF(CHANNEL_STACK_FROM_CHANNEL(channel), "unused");
144 }
grpc_channel_internal_unref(grpc_channel * channel)145 inline void grpc_channel_internal_unref(grpc_channel* channel) {
146   GRPC_CHANNEL_STACK_UNREF(CHANNEL_STACK_FROM_CHANNEL(channel), "unused");
147 }
148 #define GRPC_CHANNEL_INTERNAL_REF(channel, reason) \
149   grpc_channel_internal_ref(channel)
150 #define GRPC_CHANNEL_INTERNAL_UNREF(channel, reason) \
151   grpc_channel_internal_unref(channel)
152 #endif
153 
154 /** Return the channel's compression options. */
155 grpc_compression_options grpc_channel_compression_options(
156     const grpc_channel* channel);
157 
158 #endif /* GRPC_CORE_LIB_SURFACE_CHANNEL_H */
159