3 * Copyright 2015 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <grpc/support/port_platform.h>
21 #include <grpc/grpc.h>
25 #include <grpc/support/alloc.h>
26 #include <grpc/support/string_util.h>
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"
38 class Chttp2InsecureClientChannelFactory : public ClientChannelFactory {
40 Subchannel* CreateSubchannel(const grpc_channel_args* args) override {
41 grpc_channel_args* new_args =
42 grpc_default_authority_add_if_not_present(args);
43 grpc_connector* connector = grpc_chttp2_connector_create();
44 Subchannel* s = Subchannel::Create(connector, new_args);
45 grpc_connector_unref(connector);
46 grpc_channel_args_destroy(new_args);
53 grpc_channel* CreateChannel(const char* target, const grpc_channel_args* args) {
54 if (target == nullptr) {
55 gpr_log(GPR_ERROR, "cannot create channel with NULL target name");
58 // Add channel arg containing the server URI.
59 UniquePtr<char> canonical_target =
60 ResolverRegistry::AddDefaultPrefixIfNeeded(target);
61 grpc_arg arg = grpc_channel_arg_string_create(
62 const_cast<char*>(GRPC_ARG_SERVER_URI), canonical_target.get());
63 const char* to_remove[] = {GRPC_ARG_SERVER_URI};
64 grpc_channel_args* new_args =
65 grpc_channel_args_copy_and_add_and_remove(args, to_remove, 1, &arg, 1);
66 grpc_channel* channel =
67 grpc_channel_create(target, new_args, GRPC_CLIENT_CHANNEL, nullptr);
68 grpc_channel_args_destroy(new_args);
74 } // namespace grpc_core
78 grpc_core::Chttp2InsecureClientChannelFactory* g_factory;
79 gpr_once g_factory_once = GPR_ONCE_INIT;
82 g_factory = grpc_core::New<grpc_core::Chttp2InsecureClientChannelFactory>();
87 /* Create a client channel:
88 Asynchronously: - resolve target
89 - connect to it (trying alternatives as presented)
90 - perform handshakes */
91 grpc_channel* grpc_insecure_channel_create(const char* target,
92 const grpc_channel_args* args,
94 grpc_core::ExecCtx exec_ctx;
96 "grpc_insecure_channel_create(target=%s, args=%p, reserved=%p)", 3,
97 (target, args, reserved));
98 GPR_ASSERT(reserved == nullptr);
99 // Add channel arg containing the client channel factory.
100 gpr_once_init(&g_factory_once, FactoryInit);
101 grpc_arg arg = grpc_core::ClientChannelFactory::CreateChannelArg(g_factory);
102 grpc_channel_args* new_args = grpc_channel_args_copy_and_add(args, &arg, 1);
104 grpc_channel* channel = grpc_core::CreateChannel(target, new_args);
106 grpc_channel_args_destroy(new_args);
107 return channel != nullptr ? channel
108 : grpc_lame_client_channel_create(
109 target, GRPC_STATUS_INTERNAL,
110 "Failed to create client channel");