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 RefCountedPtr<Subchannel> CreateSubchannel(
41 const grpc_channel_args* args) override {
42 grpc_channel_args* new_args =
43 grpc_default_authority_add_if_not_present(args);
44 RefCountedPtr<Subchannel> s =
45 Subchannel::Create(MakeOrphanable<Chttp2Connector>(), new_args);
46 grpc_channel_args_destroy(new_args);
53 grpc_channel* CreateChannel(const char* target, const grpc_channel_args* args,
54 grpc_error_handle* error) {
55 if (target == nullptr) {
56 gpr_log(GPR_ERROR, "cannot create channel with NULL target name");
57 if (error != nullptr) {
58 *error = GRPC_ERROR_CREATE_FROM_STATIC_STRING("channel target is NULL");
62 // Add channel arg containing the server URI.
63 grpc_core::UniquePtr<char> canonical_target =
64 ResolverRegistry::AddDefaultPrefixIfNeeded(target);
65 grpc_arg arg = grpc_channel_arg_string_create(
66 const_cast<char*>(GRPC_ARG_SERVER_URI), canonical_target.get());
67 const char* to_remove[] = {GRPC_ARG_SERVER_URI};
68 grpc_channel_args* new_args =
69 grpc_channel_args_copy_and_add_and_remove(args, to_remove, 1, &arg, 1);
70 grpc_channel* channel = grpc_channel_create(
71 target, new_args, GRPC_CLIENT_CHANNEL, nullptr, nullptr, error);
72 grpc_channel_args_destroy(new_args);
78 } // namespace grpc_core
82 grpc_core::Chttp2InsecureClientChannelFactory* g_factory;
83 gpr_once g_factory_once = GPR_ONCE_INIT;
86 g_factory = new grpc_core::Chttp2InsecureClientChannelFactory();
91 /* Create a client channel:
92 Asynchronously: - resolve target
93 - connect to it (trying alternatives as presented)
94 - perform handshakes */
95 grpc_channel* grpc_insecure_channel_create(const char* target,
96 const grpc_channel_args* args,
98 grpc_core::ExecCtx exec_ctx;
100 "grpc_insecure_channel_create(target=%s, args=%p, reserved=%p)", 3,
101 (target, args, reserved));
102 GPR_ASSERT(reserved == nullptr);
103 // Add channel arg containing the client channel factory.
104 gpr_once_init(&g_factory_once, FactoryInit);
105 grpc_arg arg = grpc_core::ClientChannelFactory::CreateChannelArg(g_factory);
106 const char* arg_to_remove = arg.key;
107 grpc_channel_args* new_args = grpc_channel_args_copy_and_add_and_remove(
108 args, &arg_to_remove, 1, &arg, 1);
109 grpc_error_handle error = GRPC_ERROR_NONE;
111 grpc_channel* channel = grpc_core::CreateChannel(target, new_args, &error);
113 grpc_channel_args_destroy(new_args);
114 if (channel == nullptr) {
116 grpc_status_code status = GRPC_STATUS_INTERNAL;
117 if (grpc_error_get_int(error, GRPC_ERROR_INT_GRPC_STATUS, &integer)) {
118 status = static_cast<grpc_status_code>(integer);
120 GRPC_ERROR_UNREF(error);
121 channel = grpc_lame_client_channel_create(
122 target, status, "Failed to create client channel");