• 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 #include <grpc/support/port_platform.h>
20 
21 #include <grpc/grpc.h>
22 
23 #include <string.h>
24 
25 #include <grpc/support/alloc.h>
26 #include <grpc/support/string_util.h>
27 
28 #include "src/core/ext/filters/client_channel/client_channel.h"
29 #include "src/core/ext/filters/client_channel/resolver_registry.h"
30 #include "src/core/ext/transport/chttp2/client/authority.h"
31 #include "src/core/ext/transport/chttp2/client/chttp2_connector.h"
32 #include "src/core/lib/channel/channel_args.h"
33 #include "src/core/lib/surface/api_trace.h"
34 #include "src/core/lib/surface/channel.h"
35 
client_channel_factory_ref(grpc_client_channel_factory * cc_factory)36 static void client_channel_factory_ref(
37     grpc_client_channel_factory* cc_factory) {}
38 
client_channel_factory_unref(grpc_client_channel_factory * cc_factory)39 static void client_channel_factory_unref(
40     grpc_client_channel_factory* cc_factory) {}
41 
client_channel_factory_create_subchannel(grpc_client_channel_factory * cc_factory,const grpc_subchannel_args * args)42 static grpc_subchannel* client_channel_factory_create_subchannel(
43     grpc_client_channel_factory* cc_factory, const grpc_subchannel_args* args) {
44   grpc_subchannel_args final_sc_args;
45   memcpy(&final_sc_args, args, sizeof(*args));
46   final_sc_args.args = grpc_default_authority_add_if_not_present(args->args);
47   grpc_connector* connector = grpc_chttp2_connector_create();
48   grpc_subchannel* s = grpc_subchannel_create(connector, &final_sc_args);
49   grpc_connector_unref(connector);
50   grpc_channel_args_destroy(const_cast<grpc_channel_args*>(final_sc_args.args));
51   return s;
52 }
53 
client_channel_factory_create_channel(grpc_client_channel_factory * cc_factory,const char * target,grpc_client_channel_type type,const grpc_channel_args * args)54 static grpc_channel* client_channel_factory_create_channel(
55     grpc_client_channel_factory* cc_factory, const char* target,
56     grpc_client_channel_type type, const grpc_channel_args* args) {
57   if (target == nullptr) {
58     gpr_log(GPR_ERROR, "cannot create channel with NULL target name");
59     return nullptr;
60   }
61   // Add channel arg containing the server URI.
62   grpc_core::UniquePtr<char> canonical_target =
63       grpc_core::ResolverRegistry::AddDefaultPrefixIfNeeded(target);
64   grpc_arg arg = grpc_channel_arg_string_create(
65       const_cast<char*>(GRPC_ARG_SERVER_URI), canonical_target.get());
66   const char* to_remove[] = {GRPC_ARG_SERVER_URI};
67   grpc_channel_args* new_args =
68       grpc_channel_args_copy_and_add_and_remove(args, to_remove, 1, &arg, 1);
69   grpc_channel* channel =
70       grpc_channel_create(target, new_args, GRPC_CLIENT_CHANNEL, nullptr);
71   grpc_channel_args_destroy(new_args);
72   return channel;
73 }
74 
75 static const grpc_client_channel_factory_vtable client_channel_factory_vtable =
76     {client_channel_factory_ref, client_channel_factory_unref,
77      client_channel_factory_create_subchannel,
78      client_channel_factory_create_channel};
79 
80 static grpc_client_channel_factory client_channel_factory = {
81     &client_channel_factory_vtable};
82 
83 /* Create a client channel:
84    Asynchronously: - resolve target
85                    - connect to it (trying alternatives as presented)
86                    - perform handshakes */
grpc_insecure_channel_create(const char * target,const grpc_channel_args * args,void * reserved)87 grpc_channel* grpc_insecure_channel_create(const char* target,
88                                            const grpc_channel_args* args,
89                                            void* reserved) {
90   grpc_core::ExecCtx exec_ctx;
91   GRPC_API_TRACE(
92       "grpc_insecure_channel_create(target=%s, args=%p, reserved=%p)", 3,
93       (target, args, reserved));
94   GPR_ASSERT(reserved == nullptr);
95   // Add channel arg containing the client channel factory.
96   grpc_arg arg =
97       grpc_client_channel_factory_create_channel_arg(&client_channel_factory);
98   grpc_channel_args* new_args = grpc_channel_args_copy_and_add(args, &arg, 1);
99   // Create channel.
100   grpc_channel* channel = client_channel_factory_create_channel(
101       &client_channel_factory, target, GRPC_CLIENT_CHANNEL_TYPE_REGULAR,
102       new_args);
103   // Clean up.
104   grpc_channel_args_destroy(new_args);
105 
106   return channel != nullptr ? channel
107                             : grpc_lame_client_channel_create(
108                                   target, GRPC_STATUS_INTERNAL,
109                                   "Failed to create client channel");
110 }
111